software_info.rb
8.01 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
class SoftwareInfo < ActiveRecord::Base
acts_as_having_settings :field => :settings
SEARCHABLE_SOFTWARE_FIELDS = {
:acronym => 1,
:finality => 2,
}
SEARCHABLE_SOFTWARE_CLASSES = [
SoftwareInfo,
Community,
ProgrammingLanguage,
DatabaseDescription
]
scope :search_by_query, lambda { |query = "", env = Environment.default|
filtered_query = query.gsub(/[\|\(\)\\\/\s\[\]'"*%&!:]/,' ').split.map{|w| w += ":*"}.join('|')
search_fields = SoftwareInfo.pg_search_plugin_fields
if query.empty?
SoftwareInfo.joins(:community).where("profiles.visible = ? AND environment_id = ? ", true, env.id)
else
searchable_software_objects = SoftwareInfo.transform_list_in_methods_list(SEARCHABLE_SOFTWARE_CLASSES)
includes(searchable_software_objects).where("to_tsvector('simple', #{search_fields}) @@ to_tsquery('#{filtered_query}')").where("profiles.visible = ? AND environment_id = ?", true, env.id)
end
}
def self.transform_list_in_methods_list list
methods_list = []
list.each do |element|
if SoftwareInfo.instance_methods.include?(element.to_s.underscore.to_sym)
methods_list << element.to_s.underscore.to_sym
elsif SoftwareInfo.instance_methods.include?(element.to_s.underscore.pluralize.to_sym)
methods_list << element.to_s.underscore.pluralize.to_sym
end
end
methods_list
end
def self.pg_search_plugin_fields
SEARCHABLE_SOFTWARE_CLASSES.collect { |one_class|
self.get_searchable_fields(one_class)
}.join(" || ' ' || ")
end
def self.get_searchable_fields one_class
searchable_fields = one_class::SEARCHABLE_SOFTWARE_FIELDS.keys.map(&:to_s).sort.map {|f| "coalesce(#{one_class.table_name}.#{f}, '')"}.join(" || ' ' || ")
searchable_fields
end
SEARCH_FILTERS = {
:order => %w[],
:display => %w[full]
}
def self.default_search_display
'full'
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, :dependent => :destroy
belongs_to :license_info
has_one :software_categories
validates_length_of :finality, :maximum => 4000
validates_length_of :objectives, :maximum => 4000
validates_length_of :features, :maximum => 4000
validates_presence_of :finality, :community, :license_info
validate :validate_acronym
settings_items :another_license_version, :default => 'Another'
settings_items :another_license_link, :default => '#'
settings_items :sisp, :default => false
serialize :agency_identification
serialize :software_requirements
serialize :hardware_requirements
serialize :documentation
serialize :system_applications
serialize :active_versions
serialize :estimated_cost
serialize :responsible
serialize :responsible_for_acquirement
serialize :system_info
serialize :development_info
serialize :maintenance
serialize :standards_adherence
serialize :platform
# used on find_by_contents
def self.like_search name
joins(:community).where(
"name ILIKE ? OR acronym ILIKE ? OR finality ILIKE ?",
"%#{name}%", "%#{name}%", "%#{name}%"
)
end
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 license_info
license_another = LicenseInfo.find_by_version("Another")
if license_another && self.license_info_id == license_another.id
license_another.version = self.another_license_version
license_another.link = self.another_license_link
license_another
else
super
end
end
def another_license(version, link)
license_another = LicenseInfo.find_by_version("Another")
if license_another
self.another_license_version = version
self.another_license_link = link
self.license_info = license_another
self.save!
end
end
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)
identifier = attributes.delete(:identifier)
image_builder = attributes.delete(:image_builder)
license_info = attributes.delete(:license_info)
another_license_version = attributes.delete(:another_license_version)
another_license_link = attributes.delete(:another_license_link)
software_info = SoftwareInfo.new(attributes)
if !requestor.is_admin?
CreateSoftware.create!(
attributes.merge(
:requestor => requestor,
:environment => environment,
:name => name,
:identifier => identifier,
:license_info => license_info,
:another_license_version => another_license_version,
:another_license_link => another_license_link
)
)
else
software_template = Community["software"]
community_hash = {:name => name}
community_hash[:identifier] = identifier
community_hash[:image_builder] = image_builder if image_builder
community = Community.new(community_hash)
community.environment = environment
if (!software_template.blank? && software_template.is_template)
community.template_id = software_template.id
end
community.save!
community.add_admin(requestor)
software_info.community = community
software_info.license_info = license_info
software_info.verify_license_info(another_license_version, another_license_link)
software_info.save!
end
software_info
end
def verify_license_info another_license_version, another_license_link
license_another = LicenseInfo.find_by_version("Another")
if license_another && self.license_info_id == license_another.id
version = another_license_version
link = another_license_link
self.another_license(version, link)
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
if self.operating_systems.empty?
self.errors.add(:operating_system, _(": at least one must be filled"))
end
end
def valid_software_info
if self.software_languages.empty?
self.errors.add(:software_languages, _(": at least one must be filled"))
end
end
def valid_databases
if self.software_databases.empty?
self.errors.add(:software_databases, _(": at least one must be filled"))
end
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