Commit 4ba30302ae02ea2056c169585de84139641c4a5a

Authored by David Silva
1 parent 09b2dd37

Refactoring transactions on mpog_software_plugin.

Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com>
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
controllers/mpog_software_plugin_myprofile_controller.rb
... ... @@ -42,7 +42,7 @@ class MpogSoftwarePluginMyprofileController &lt; MyProfileController
42 42  
43 43 return unless request.post?
44 44 @software_info = constroy_software
45   - software_info_insert_models.call(@list_libraries, 'libraries')
  45 + software_info_insert_models.call(@list_library, 'libraries')
46 46 software_info_insert_models.call(@list_languages, 'software_languages')
47 47 software_info_insert_models.call(@list_databases, 'software_databases')
48 48 software_info_insert_models.call(@list_operating_systems, 'operating_systems')
... ... @@ -130,7 +130,7 @@ class MpogSoftwarePluginMyprofileController &lt; MyProfileController
130 130 end
131 131  
132 132 def create_list_model_helpers
133   - @list_libraries = LibraryHelper.list_libraries(params[:library])
  133 + @list_libraries = LibraryHelper.list_library(params[:library])
134 134 @list_languages = SoftwareLanguageHelper.list_language(params[:language])
135 135 @list_databases = DatabaseHelper.list_database(params[:database])
136 136 @list_operating_systems = OperatingSystemHelper.list_operating_system(params[:operating_system])
... ...
lib/library_helper.rb
1   -class LibraryHelper < DynamicTableHelper
2   - MODEL_NAME ="library"
3   -
4   - def self.list_libraries new_libraries
  1 +module LibraryHelper
  2 + def self.list_library new_libraries
5 3 return [] if new_libraries.nil? or new_libraries.length == 0
6 4 list_libraries = []
7 5  
... ... @@ -18,7 +16,7 @@ class LibraryHelper &lt; DynamicTableHelper
18 16 list_libraries
19 17 end
20 18  
21   - def self.valid_list_libraries? list_libraries
  19 + def self.valid_list_library? list_libraries
22 20 return true if list_libraries.nil? or list_libraries.length == 0
23 21  
24 22 list_libraries.each do |library|
... ...
lib/mpog_software_plugin.rb
... ... @@ -18,14 +18,16 @@ class MpogSoftwarePlugin &lt; Noosfero::Plugin
18 18 end
19 19  
20 20 def profile_editor_extras
21   - if context.profile.person?
  21 + profile = context.profile
  22 +
  23 + if profile.person?
22 24 expanded_template('person_editor_extras.html.erb')
23   - elsif context.profile.respond_to?(:software_info) &&
24   - !context.profile.software_info.nil?
  25 + elsif profile.respond_to?(:software_info) &&
  26 + !profile.software_info.nil?
25 27  
26   - if context.profile.software_info.first_edit?
27   - context.profile.software_info.first_edit = false
28   - context.profile.software_info.save!
  28 + if profile.software_info.first_edit?
  29 + profile.software_info.first_edit = false
  30 + profile.software_info.save!
29 31 expanded_template('first_edit_software_community_extras.html.erb')
30 32 end
31 33 end
... ... @@ -34,11 +36,12 @@ class MpogSoftwarePlugin &lt; Noosfero::Plugin
34 36 def profile_editor_transaction_extras
35 37 single_hash_transactions = { :user => 'user',
36 38 :software_info => 'software_info',
37   - :version => 'license', :language => 'language',
38   - :operating_system => 'operating_system',
  39 + :version => 'license', :language => 'generic_model',
  40 + :operating_system => 'generic_model',
39 41 :software_categories => 'software_categories',
40 42 :instituton => 'instituton',
41   - :library => 'libraries' }
  43 + :library => 'generic_model',
  44 + :database => 'generic_model' }
42 45  
43 46 single_hash_transactions.each do |model, transaction|
44 47 call_model_transaction(model, transaction)
... ... @@ -72,7 +75,7 @@ class MpogSoftwarePlugin &lt; Noosfero::Plugin
72 75  
73 76 def profile_tabs
74 77 if context.profile.community?
75   - profile_tabs_software if context.profile.software?
  78 + return profile_tabs_software if context.profile.software?
76 79 profile_tabs_institution
77 80 end
78 81 end
... ... @@ -190,21 +193,22 @@ class MpogSoftwarePlugin &lt; Noosfero::Plugin
190 193  
191 194 def profile_required_list
192 195 fields = {}
193   - fields[:person_fields] = %w('cell_phone'
194   - 'contact_phone'
195   - 'comercial_phone'
196   - 'country'
197   - 'city'
198   - 'state'
199   - 'organization_website'
200   - 'image',
201   - 'identifier'
202   - 'name')
203   -
204   - fields[:user_fields] = %w('secondary_email' 'email')
  196 + fields[:person_fields] = %w(cell_phone
  197 + contact_phone
  198 + comercial_phone
  199 + country
  200 + city
  201 + state
  202 + organization_website
  203 + image
  204 + identifier
  205 + name)
  206 +
  207 + fields[:user_fields] = %w(secondary_email email)
205 208 fields
206 209 end
207 210  
  211 +
208 212 def profile_required_empty_list(person)
209 213 empty_fields = []
210 214 required_list = profile_required_list
... ... @@ -218,27 +222,6 @@ class MpogSoftwarePlugin &lt; Noosfero::Plugin
218 222 empty_fields
219 223 end
220 224  
221   - def operating_system_transaction
222   - OperatingSystem.transaction do
223   - list_operating = OperatingSystemHelper.list_operating_system(
224   - context.params[:operating_system]
225   - )
226   -
227   - if OperatingSystemHelper.valid_list_operating_system?(list_operating)
228   - OperatingSystem.where(
229   - :software_info_id => context.profile.software_info.id
230   - ).destroy_all
231   -
232   - list_operating.each do |operating_system|
233   - operating_system.software_info = context.profile.software_info
234   - operating_system.save!
235   - end
236   - else
237   - raise 'Invalid Operating System fields'
238   - end
239   - end
240   - end
241   -
242 225 def user_transaction
243 226 user_editor_institution_actions
244 227  
... ... @@ -274,80 +257,12 @@ class MpogSoftwarePlugin &lt; Noosfero::Plugin
274 257 end
275 258 end
276 259  
277   - def libraries_transaction
278   - Library.transaction do
279   - list_libraries = LibraryHelper.list_libraries(context.params[:library])
280   -
281   - if LibraryHelper.valid_list_libraries?(list_libraries)
282   - Library.where(
283   - :software_info_id=> context.profile.software_info.id
284   - ).destroy_all
285   -
286   - list_libraries.each do |library|
287   - library.software_info_id = context.profile.software_info.id
288   - library.save!
289   - end
290   - else
291   - raise 'Invalid Library fields'
292   - end
293   - end
294   - end
295   -
296   - def databases_transaction
297   - SoftwareDatabase.transaction do
298   - list_databases = DatabaseHelper.list_database(context.params[:database])
299   -
300   - if DatabaseHelper.valid_list_database?(list_databases)
301   - SoftwareDatabase.where(
302   - :software_info_id => context.profile.software_info.id
303   - ).destroy_all
304   -
305   - list_databases.each do |database|
306   - database.software_info = context.profile.software_info
307   - database.save!
308   - end
309   - else
310   - raise 'Invalid Database fields'
311   - end
312   - end
313   - end
314   -
315 260 def license_transaction
316 261 license = LicenseInfo.find(context.params[:version])
317 262 context.profile.software_info.license_info = license
318 263 context.profile.software_info.save!
319 264 end
320 265  
321   - def language_transaction
322   - SoftwareLanguage.transaction do
323   - list_language = SoftwareLanguageHelper.list_language(
324   - context.params[:language]
325   - )
326   -
327   - if SoftwareLanguageHelper.valid_list_language?(list_language)
328   - SoftwareLanguage.where(
329   - :software_info_id => context.profile.software_info.id
330   - ).destroy_all
331   -
332   - list_language.each do |language|
333   - language.software_info = context.profile.software_info
334   - language.save!
335   - end
336   - else
337   - raise 'Invalid Software Language fields'
338   - end
339   - end
340   - end
341   -
342   - def software_categories_transaction
343   - ControlledVocabulary.transaction do
344   - context.profile
345   - .software_info
346   - .software_categories
347   - .update_attributes!(context.params[:software_categories])
348   - end
349   - end
350   -
351 266 private
352 267  
353 268 # Add and remove the user from it's institutions communities
... ... @@ -386,6 +301,34 @@ class MpogSoftwarePlugin &lt; Noosfero::Plugin
386 301 context.profile.institution.save!
387 302 end
388 303  
  304 + def generic_model_transaction
  305 + models_list = [
  306 + [SoftwareLanguage, SoftwareLanguageHelper, 'language'],
  307 + [SoftwareDatabase, DatabaseHelper, 'database'],
  308 + [OperatingSystem, OperatingSystemHelper, 'operating_system'],
  309 + [Library, LibraryHelper, 'library']
  310 + ]
  311 + models_list.each do |model|
  312 + list_of_model = 'list_'+model[2].to_s
  313 + model[0].transaction do
  314 + list = model[1].send(list_of_model, context.params[model[2].to_sym])
  315 +
  316 + if model[2].send('valid_'+list_of_model+'?', list_of_model)
  317 + model[0].where(
  318 + :software_info_id => context.profile.software_info.id
  319 + ).destroy_all
  320 +
  321 + list.each do |model|
  322 + model.software_info = context.profile.software_info
  323 + model.save!
  324 + end
  325 + else
  326 + raise 'Invalid Software #{model[2]} fields'
  327 + end
  328 + end
  329 + end
  330 + end
  331 +
389 332 def software_info_button
390 333 {
391 334 :title => _('Software Info'),
... ...
test/unit/library_helper_test.rb
... ... @@ -17,12 +17,12 @@ class LibraryHelperTest &lt; ActiveSupport::TestCase
17 17  
18 18 should "return an empty list" do
19 19 empty_list = []
20   - assert_equal [],LibraryHelper.list_libraries(empty_list)
  20 + assert_equal [],LibraryHelper.list_library(empty_list)
21 21 end
22 22  
23 23 should "return a list with current library objects" do
24 24 list_compare = []
25   - lib_table = LibraryHelper.list_libraries(@license_objects)
  25 + lib_table = LibraryHelper.list_library(@license_objects)
26 26 assert_equal list_compare.class, lib_table.class
27 27 end
28 28  
... ... @@ -32,13 +32,13 @@ class LibraryHelperTest &lt; ActiveSupport::TestCase
32 32 end
33 33  
34 34 should "return a list with the same size of the parameter" do
35   - list_compare = LibraryHelper.list_libraries(@license_objects)
  35 + list_compare = LibraryHelper.list_library(@license_objects)
36 36 assert_equal @license_objects.count, list_compare.count
37 37 end
38 38  
39 39 should "return false if list_database are empty or null" do
40 40 list_compare = []
41   - assert_equal true, LibraryHelper.valid_list_libraries?(list_compare)
  41 + assert_equal true, LibraryHelper.valid_list_library?(list_compare)
42 42 end
43 43  
44 44 should "return a html text with license name equals to linux" do
... ...