software_info.rb
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class SoftwareInfo < ActiveRecord::Base
SEARCH_FILTERS = []
SEARCH_DISPLAYS = %w[compact]
def self.default_search_display
'compact'
end
attr_accessible :e_mag, :icp_brasil, :intern, :e_ping, :e_arq, :operating_platform
attr_accessible :demonstration_url, :acronym, :objectives, :features, :license_info
attr_accessible :community_id, :finality, :repository_link, :public_software, :first_edit
has_many :libraries, :dependent => :destroy
has_many :software_databases
has_many :database_descriptions, :through => :software_databases
has_many :software_languages
has_many :operating_systems
has_many :programming_languages, :through => :software_languages
has_many :operating_system_names, :through => :operating_systems
belongs_to :community
belongs_to :license_info
has_one :software_categories
validates_length_of :finality, :maximum => 140
validates_length_of :objectives, :maximum => 4000
validates_length_of :features, :maximum => 4000
validate :validate_acronym
# used on find_by_contents
scope :like_search, lambda{ |name|
joins(:community).where("name ilike ?", "%#{name}%")
}
scope :search, lambda { |name="", database_description_id = "",
programming_language_id = "", operating_system_name_id = "",
license_info_id = "", e_ping = "", e_mag = "", internacionalizable = "",
icp_brasil = "", e_arq = "", software_categories = "" |
like_sql = ""
values = []
unless name.blank?
like_sql << "name ILIKE ? OR identifier ILIKE ? AND "
values << "%#{name}%" << "%#{name}%"
end
like_sql = like_sql[0..like_sql.length-5]
{
:joins => [:community],
:conditions=>[like_sql, *values]
}
}
def validate_name_lenght
if self.community.name.size > 100
self.errors.add(:base, _("Name is too long (maximum is %{count} characters)"))
false
end
true
end
# if create_after_moderation receive a model object, would be possible to reuse core method
def self.create_after_moderation(requestor, attributes = {})
environment = attributes.delete(:environment)
name = attributes.delete(:name)
license_info = attributes.delete(:license_info)
software_info = SoftwareInfo.new(attributes)
if !environment.admins.include? requestor
CreateSoftware.create!(attributes.merge(:requestor => requestor, :environment => environment, :name => name, :license_info => license_info))
else
software_template = Community["software"]
community = Community.new(:name => name)
community.environment = environment
community.template_id = software_template.id if (!software_template.blank? && software_template.is_template)
software_info.license_info = license_info
software_info.save
community.software_info = software_info
community.save!
community.add_admin(requestor)
end
end
def validate_acronym
self.acronym = "" if self.acronym.nil?
if self.acronym.length > 10 && self.errors.messages[:acronym].nil?
self.errors.add(:acronym, _("can't have more than 10 characteres"))
false
elsif self.acronym.match(/\s+/)
self.errors.add(:acronym, _("can't have whitespaces"))
false
end
true
end
def valid_operating_systems
self.errors.add(:operating_system, _(": at least one must be filled")) if self.operating_systems.empty?
end
def valid_software_info
self.errors.add(:software_languages, _(": at least one must be filled")) if self.software_languages.empty?
end
def valid_databases
self.errors.add(:software_databases, _(": at least one must be filled")) if self.software_databases.empty?
end
def visible?
self.community.visible?
end
def name
self.community.name
end
def short_name
self.community.short_name
end
def identifier
self.community.identifier
end
end