Commit ff8462b851634aaa0383f1bfefcfb3986c40f62d

Authored by Luciano Prestes
Committed by Arthur Esposte
1 parent ca5c471f

Refactor software_communities.

Signed-off-by: Thiago Ribeiro <thiagitosouza@gmail.com>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
src/noosfero-spb/software_communities/controllers/software_communities_plugin_controller.rb
... ... @@ -5,11 +5,9 @@ class SoftwareCommunitiesPluginController &lt; ApplicationController
5 5 def get_license_data
6 6 return render :json=>{} if !request.xhr? || params[:query].nil?
7 7  
8   - data = if params[:query].empty?
9   - LicenseInfo.all
10   - else
11   - LicenseInfo.where("version ILIKE ?", "%#{params[:query]}%").select("id, version")
12   - end
  8 + data = LicenseHelper.find_licenses(params[:query]) if params[:query]
  9 + data ||= LicenseInfo.all
  10 +
13 11 render :json=> data.collect { |license|
14 12 {:id=>license.id, :label=>license.version}
15 13 }
... ... @@ -44,11 +42,7 @@ class SoftwareCommunitiesPluginController &lt; ApplicationController
44 42 protected
45 43  
46 44 def get_model_by_params_field
47   - case params[:field]
48   - when "software_language"
49   - return ProgrammingLanguage
50   - else
51   - return DatabaseDescription
52   - end
  45 + return DatabaseDescription unless params[:field] == "software_language"
  46 + return ProgrammingLanguage
53 47 end
54 48 end
... ...
src/noosfero-spb/software_communities/controllers/software_communities_plugin_myprofile_controller.rb
... ... @@ -9,16 +9,16 @@ class SoftwareCommunitiesPluginMyprofileController &lt; MyProfileController
9 9  
10 10 @community = Community.new(params[:community])
11 11 @community.environment = environment
12   - @software_info = SoftwareInfo.new(params[:software_info])
13 12  
14   - @license_info = if params[:license].blank? or params[:license][:license_infos_id].blank?
15   - LicenseInfo.new
16   - else
17   - LicenseInfo.find(params[:license][:license_infos_id])
18   - end
  13 + @license_info = LicenseInfo.find_by_id(params[:license][:license_infos_id]) if params[:license]
  14 + @license_info ||= LicenseInfo.new
  15 +
  16 + @software_info = SoftwareInfo.new(params[:software_info])
  17 + @software_info.community = @community
  18 + @software_info.license_info = @license_info
19 19  
20 20 control_software_creation
21   - update_new_software_errors
  21 + update_software_highlight_errors
22 22 end
23 23  
24 24 def edit_software
... ... @@ -26,7 +26,7 @@ class SoftwareCommunitiesPluginMyprofileController &lt; MyProfileController
26 26  
27 27 return unless request.post?
28 28  
29   - @software_info = constroy_software
  29 + @software_info = create_software
30 30 software_info_insert_models.call(@list_libraries, 'libraries')
31 31 software_info_insert_models.call(@list_languages, 'software_languages')
32 32 software_info_insert_models.call(@list_databases, 'software_databases')
... ... @@ -45,7 +45,7 @@ class SoftwareCommunitiesPluginMyprofileController &lt; MyProfileController
45 45 session[:notice] = _('Software updated successfully')
46 46 end
47 47 rescue ActiveRecord::RecordInvalid => invalid
48   - update_new_software_errors
  48 + update_software_highlight_errors
49 49 session[:notice] = _('Could not update software')
50 50 end
51 51 end
... ... @@ -58,17 +58,25 @@ class SoftwareCommunitiesPluginMyprofileController &lt; MyProfileController
58 58  
59 59 def add_software_erros
60 60 @errors = []
61   - @errors |= @community.errors.full_messages if @community
  61 + if @community
  62 + error = @community.errors.delete(:identifier)
  63 + @errors |= [_("Domain %s") % error.first ] if error
  64 + @errors |= @community.errors.full_messages
  65 + end
62 66 @errors |= @software_info.errors.full_messages if @software_info
63 67 @errors |= @license_info.errors.full_messages if @license_info
64 68 end
65 69  
66 70 def control_software_creation
67   - valid_models = request.post? && (@community.valid? && @software_info.valid? && @license_info.valid?)
68   - if valid_models
69   - send_software_to_moderation
70   - else
71   - add_software_erros
  71 + if request.post?
  72 + valid_models = @community.valid?
  73 + valid_models &= @software_info.valid?
  74 + valid_models &= @license_info.valid?
  75 + if valid_models
  76 + send_software_to_moderation
  77 + else
  78 + add_software_erros
  79 + end
72 80 end
73 81 end
74 82  
... ... @@ -79,7 +87,7 @@ class SoftwareCommunitiesPluginMyprofileController &lt; MyProfileController
79 87 }
80 88 end
81 89  
82   - def constroy_software
  90 + def create_software
83 91 @software_info = @profile.software_info
84 92 params[:software][:public_software] ||= false unless @software_info.public_software?
85 93 @license = LicenseInfo.find(params[:license][:license_infos_id])
... ... @@ -126,7 +134,7 @@ class SoftwareCommunitiesPluginMyprofileController &lt; MyProfileController
126 134  
127 135 add_admin_to_community
128 136  
129   - if !environment.admins.include?(current_user.person)
  137 + if !environment.admins.include?(current_user.person)
130 138 session[:notice] = _('Your new software request will be evaluated by an'\
131 139 'administrator. You will be notified.')
132 140 redirect_to user.admin_url
... ... @@ -176,15 +184,7 @@ class SoftwareCommunitiesPluginMyprofileController &lt; MyProfileController
176 184 end
177 185 end
178 186  
179   - def update_new_software_errors
180   - if request.post?
181   - @community.valid? if @community
182   - @software_info.valid? if @software_info
183   - @license_info.valid? if @license_info
184   - add_software_erros
185   - end
186   -
187   -
  187 + def update_software_highlight_errors
188 188 @error_community_name = @community.errors.include?(:name) ? "highlight-error" : "" if @community
189 189 @error_software_acronym = @software_info.errors.include?(:acronym) ? "highlight-error" : "" if @software_info
190 190 @error_software_domain = @community.errors.include?(:identifier) ? "highlight-error" : "" if @community
... ...
src/noosfero-spb/software_communities/lib/create_software.rb
... ... @@ -5,12 +5,12 @@ class CreateSoftware &lt; Task
5 5 validates_presence_of :name
6 6  
7 7 attr_accessible :name, :finality, :repository_link, :requestor, :environment,
8   - :reject_explanation, :license_info
  8 + :reject_explanation, :license_info, :identifier
9 9  
10 10 alias :environment :target
11 11 alias :environment= :target=
12 12  
13   - DATA_FIELDS = ['name', 'finality', 'license_info', 'repository_link']
  13 + DATA_FIELDS = ['name', 'identifier', 'finality', 'license_info', 'repository_link']
14 14 DATA_FIELDS.each do |field|
15 15 settings_items field.to_sym
16 16 end
... ... @@ -21,7 +21,11 @@ class CreateSoftware &lt; Task
21 21 template_id = software_template.id
22 22 end
23 23  
  24 + identifier = self.identifier
  25 + identifier ||= self.name.to_slug
  26 +
24 27 community = Community.create!(:name => self.name,
  28 + :identifier => identifier,
25 29 :template_id => template_id)
26 30  
27 31 community.environment = self.environment
... ...
src/noosfero-spb/software_communities/lib/database_helper.rb
... ... @@ -34,12 +34,7 @@ class DatabaseHelper &lt; DynamicTableHelper
34 34  
35 35 def self.valid_list_database? list_databases
36 36 return false if list_databases.nil? or list_databases.length == 0
37   -
38   - list_databases.each do |database|
39   - return false unless database.valid?
40   - end
41   -
42   - true
  37 + return !list_databases.any?{|database| !database.valid?}
43 38 end
44 39  
45 40 def self.database_as_tables(list_databases, disabled=false)
... ... @@ -51,14 +46,10 @@ class DatabaseHelper &lt; DynamicTableHelper
51 46  
52 47 def self.database_html_structure(database_data, disabled)
53 48 database_id = database_data[:database_description_id]
54   - database_name = if database_data[:database_description_id].blank?
55   - ""
56   - else
57   - DatabaseDescription.find(
  49 + database_name = database_id.blank? ? "" : DatabaseDescription.find(
58 50 database_data[:database_description_id],
59 51 :select=>"name"
60 52 ).name
61   - end
62 53  
63 54 data = {
64 55 model_name: MODEL_NAME,
... ... @@ -83,4 +74,4 @@ class DatabaseHelper &lt; DynamicTableHelper
83 74 def self.add_dynamic_table
84 75 database_as_tables(nil).first.call
85 76 end
86   -end
87 77 \ No newline at end of file
  78 +end
... ...
src/noosfero-spb/software_communities/lib/library_helper.rb
... ... @@ -20,12 +20,7 @@ class LibraryHelper &lt; DynamicTableHelper
20 20  
21 21 def self.valid_list_library? list_libraries
22 22 return true if list_libraries.nil? or list_libraries.length == 0
23   -
24   - list_libraries.each do |library|
25   - return false unless library.valid?
26   - end
27   -
28   - true
  23 + return !list_libraries.any?{|library| !library.valid?}
29 24 end
30 25  
31 26 def self.libraries_as_tables list_libraries, disabled=false
... ... @@ -59,4 +54,4 @@ class LibraryHelper &lt; DynamicTableHelper
59 54 def self.add_dynamic_table
60 55 libraries_as_tables(nil).first.call
61 56 end
62   -end
63 57 \ No newline at end of file
  58 +end
... ...
src/noosfero-spb/software_communities/lib/license_helper.rb
1 1 module LicenseHelper
2   - def self.getListLicenses
3   - LicenseInfo.all
  2 + def self.find_licenses query
  3 + licenses = LicenseInfo.where("version ILIKE ?", "%#{query}%").select("id, version")
  4 + licenses.reject!{|license| license.version == "Another"}
  5 + license_another = LicenseInfo.find_by_version("Another")
  6 + licenses << license_another if license_another
  7 + licenses
4 8 end
5   -end
6 9 \ No newline at end of file
  10 +end
... ...
src/noosfero-spb/software_communities/lib/operating_system_helper.rb
... ... @@ -25,12 +25,8 @@ class OperatingSystemHelper &lt; DynamicTableHelper
25 25 end
26 26  
27 27 def self.valid_list_operating_system? list_operating_system
28   - return !(list_operating_system.nil? || list_operating_system.length == 0)
29   -
30   - list_operating_system.each do |operating_system|
31   - return false unless operating_system.valid?
32   - end
33   - true
  28 + return false if (list_operating_system.nil? || list_operating_system.length == 0)
  29 + return !list_operating_system.any?{|os| !os.valid?}
34 30 end
35 31  
36 32 def self.operating_system_as_tables(list_operating_system, disabled=false)
... ...
src/noosfero-spb/software_communities/lib/software_communities_plugin.rb
... ... @@ -18,8 +18,8 @@ class SoftwareCommunitiesPlugin &lt; Noosfero::Plugin
18 18 end
19 19  
20 20 def profile_tabs
21   - if context.profile.community?
22   - return profile_tabs_software if context.profile.software?
  21 + if context.profile.community? && context.profile.software?
  22 + return profile_tabs_software
23 23 end
24 24 end
25 25  
... ... @@ -102,7 +102,6 @@ class SoftwareCommunitiesPlugin &lt; Noosfero::Plugin
102 102 is_admin ||= user_rating.organization.admins.include?(current_user.person)
103 103  
104 104 if is_admin and profile.software?
105   -
106 105 render :file => 'organization_ratings_extra_fields_show_data',
107 106 :locals => {:user_rating => user_rating}
108 107 end
... ...
src/noosfero-spb/software_communities/lib/software_helper.rb
... ... @@ -23,22 +23,6 @@ module SoftwareHelper
23 23 end
24 24  
25 25 def self.all_table_is_empty? table, ignored_fields=[]
26   - filled_fields = []
27   -
28   - table.each do |key, value|
29   - unless ignored_fields.include? key
30   - filled_fields << if value.empty?
31   - false
32   - else
33   - true
34   - end
35   - end
36   - end
37   -
38   - if filled_fields.include? true
39   - false
40   - else
41   - true
42   - end
  26 + return !table.keys.any?{|key| ignored_fields.include?(key) ? false : !table[key].empty?}
43 27 end
44 28 end
... ...
src/noosfero-spb/software_communities/lib/software_info.rb
... ... @@ -84,7 +84,7 @@ class SoftwareInfo &lt; ActiveRecord::Base
84 84 validates_length_of :finality, :maximum => 120
85 85 validates_length_of :objectives, :maximum => 4000
86 86 validates_length_of :features, :maximum => 4000
87   - validates_presence_of :finality
  87 + validates_presence_of :finality, :community
88 88  
89 89 validate :validate_acronym
90 90  
... ... @@ -166,12 +166,13 @@ class SoftwareInfo &lt; ActiveRecord::Base
166 166 another_license_link = attributes.delete(:another_license_link)
167 167  
168 168 software_info = SoftwareInfo.new(attributes)
169   - if !environment.admins.include? requestor
  169 + unless environment.admins.include? requestor
170 170 CreateSoftware.create!(
171 171 attributes.merge(
172 172 :requestor => requestor,
173 173 :environment => environment,
174 174 :name => name,
  175 + :identifier => identifier,
175 176 :license_info => license_info
176 177 )
177 178 )
... ... @@ -189,15 +190,16 @@ class SoftwareInfo &lt; ActiveRecord::Base
189 190 community.template_id = software_template.id
190 191 end
191 192  
192   - software_info.license_info = license_info
193   - software_info.save
194   - community.software_info = software_info
195 193 community.save!
196 194 community.add_admin(requestor)
  195 +
  196 + software_info.community = community
  197 + software_info.license_info = license_info
  198 + software_info.save!
197 199 end
198 200  
199 201 software_info.verify_license_info(another_license_version, another_license_link)
200   - software_info.save!
  202 + software_info.save
201 203 software_info
202 204 end
203 205  
... ...
src/noosfero-spb/software_communities/lib/software_language_helper.rb
... ... @@ -50,10 +50,9 @@ class SoftwareLanguageHelper &lt; DynamicTableHelper
50 50  
51 51 def self.language_html_structure(language_data, disabled)
52 52 language_id = language_data[:programming_language_id]
53   - language_name = if language_data[:programming_language_id].blank?
54   - ""
55   - else
56   - ProgrammingLanguage.find(
  53 + language_name = ""
  54 + unless language_data[:programming_language_id].blank?
  55 + language_name = ProgrammingLanguage.find(
57 56 language_data[:programming_language_id],
58 57 :select=>"name"
59 58 ).name
... ...
src/noosfero-spb/software_communities/test/functional/software_communities_plugin_myprofile_controller_test.rb
... ... @@ -151,4 +151,57 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
151 151 assert_equal SoftwareInfo.last.license_info.version, another_license_version
152 152 assert_equal SoftwareInfo.last.license_info.link, another_license_link
153 153 end
  154 +
  155 + should "show error messages on create software_info" do
  156 + post(
  157 + :new_software,
  158 + :community => {},
  159 + :software_info => {},
  160 + :license => {},
  161 + :profile => @person.identifier
  162 + )
  163 + assert_includes @response.body, "Domain can't be blank"
  164 + assert_includes @response.body, "Name can't be blank"
  165 + assert_includes @response.body, "Finality can't be blank"
  166 + assert_includes @response.body, "Version can't be blank"
  167 + end
  168 +
  169 + should "show domain not available error" do
  170 + @environment.add_admin(@person)
  171 +
  172 + post(
  173 + :new_software,
  174 + :community => {:name =>"New Software", :identifier => "new-software"},
  175 + :software_info => {:finality => "something", :repository_link => ""},
  176 + :license =>{:license_infos_id => LicenseInfo.last.id},
  177 + :profile => @person.identifier
  178 + )
  179 + post(
  180 + :new_software,
  181 + :community => {:name =>"New Software", :identifier => "new-software"},
  182 + :software_info => {:finality => "something", :repository_link => ""},
  183 + :license =>{:license_infos_id => LicenseInfo.last.id},
  184 + :profile => @person.identifier
  185 + )
  186 +
  187 + assert_includes @response.body, "Domain is not available"
  188 + end
  189 +
  190 + should "create software with admin moderation" do
  191 + @environment.enable('admin_must_approve_new_communities')
  192 +
  193 + post(
  194 + :new_software,
  195 + :community => {:name =>"New Software", :identifier => "new-software"},
  196 + :software_info => {:finality => "something", :repository_link => ""},
  197 + :license =>{:license_infos_id => LicenseInfo.last.id},
  198 + :profile => @person.identifier
  199 + )
  200 +
  201 + @environment.add_admin(@person)
  202 + Task.last.send('finish', @person)
  203 +
  204 + assert_equal "New Software", Task.last.data[:name]
  205 + assert_equal "New Software", SoftwareInfo.last.community.name
  206 + end
154 207 end
... ...
src/noosfero-spb/software_communities/test/unit/software_info_validation_test.rb
... ... @@ -49,6 +49,7 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase
49 49  
50 50 @software_info.features = "Do a lot of things"
51 51 @software_info.objectives = "All tests should pass !"
  52 + @software_info.community = @community
52 53 end
53 54  
54 55 def teardown
... ...
src/noosfero-spb/software_communities/views/_main_software_editor_extras.html.erb
... ... @@ -1,32 +0,0 @@
1   -<h2><%= _('Software Information') %></h2>
2   -
3   -<%= label_tag("name", _('Name'), {:class => 'formlabel'}) %>
4   -
5   -<div id='software-name-field' class='formfield'>
6   - <span id='software-hostname'><%= context.profile.environment.default_hostname %>/</span>
7   - <%= text_field_tag(:name, context.profile.software_info.community.name) %>
8   -</div>
9   -
10   -<h3> <%= _("Finality") %> </h3>
11   -<div id="finality">
12   - <%= text_field_tag(:finality, context.profile.software_info.finality) %>
13   -</div>
14   -
15   -<h3> <%= _("Licenses") %> </h3>
16   -<div id='licenses'>
17   - <%= select_tag(:id, options_for_select(LicenseHelper.getListLicenses.collect{|l| [l.version, l.id]}, :selected => context.profile.software_info.license_info.id), :onchange => "get_license_link('version')") %>
18   - <br />
19   -
20   - <h4> <%= _("License link") %> </h4>
21   - <% LicenseHelper.getListLicenses.each do | license | %>
22   - <input type="hidden" id = "version_<%=license.id %>" value = "<%=license.link%>">
23   - <% end %>
24   -
25   - <a id = "version_link" href="<%= context.profile.software_info.license_info.link %>" target="BLANK"> <%= context.profile.software_info.license_info.link %> </a>
26   -</div>
27   -
28   -<div class="formfieldline formfield type-text">
29   - <%= label_tag "repository_url", _("Link to Repository: ") %>
30   - <%= text_field_tag(:reository_url) %>
31   -</div>
32   -
src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_license_info_fields.html.erb
1   -<% LicenseHelper.getListLicenses.each do | license | %>
2   - <input type="hidden" id = "version_<%=license.id %>" value = "<%=license.link%>">
3   -<% end %>
4   -
5 1 <%= text_field_tag "license_info[version]", license_version, :id=>"license_info_version", :class=>"license_info_version", :placeholder=>_('Autocomplete field, type some license') %>
6 2 <%= hidden_field_tag "license[license_infos_id]", license_id, :id=>"license_info_id", :class=>"license_info_id", :data => {:label=>license_version} %>
7 3  
... ...
src/noosfero-spb/software_communities/views/software_communities_plugin_myprofile/_main_software_editor_extras.html.erb
... ... @@ -5,7 +5,7 @@
5 5 </div>
6 6  
7 7 <div class= <%= @error_software_acronym %> >
8   - <%= label_tag("software[acronym]", _('Short Name'), {:class => 'formlabel mandatory'}) %>
  8 + <%= label_tag("software[acronym]", _('Short Name'), {:class => 'formlabel'}) %>
9 9 <%= text_field_tag("software[acronym]", @profile.software_info.acronym, :id => 'software_acronym_id', :maxlength=>"10") %>
10 10 </div>
11 11  
... ...