Commit 3ab323085d6c526f2f72405ea30fdf398d46fc4a

Authored by Joenio
2 parents cf86f45e dfd6263e

Merge branch 'fix_create_software_without_community' into 'master'

Fix create software without community

See merge request !79
src/noosfero-spb/software_communities/controllers/software_communities_plugin_controller.rb
... ... @@ -5,11 +5,9 @@ class SoftwareCommunitiesPluginController < 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 < 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,13 +9,13 @@ class SoftwareCommunitiesPluginMyprofileController < 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 21 update_new_software_errors
... ... @@ -26,7 +26,7 @@ class SoftwareCommunitiesPluginMyprofileController < 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')
... ... @@ -70,17 +70,25 @@ class SoftwareCommunitiesPluginMyprofileController < MyProfileController
70 70  
71 71 def add_software_erros
72 72 @errors = []
73   - @errors |= @community.errors.full_messages if @community
  73 + if @community
  74 + error = @community.errors.delete(:identifier)
  75 + @errors |= [_("Domain %s") % error.first ] if error
  76 + @errors |= @community.errors.full_messages
  77 + end
74 78 @errors |= @software_info.errors.full_messages if @software_info
75 79 @errors |= @license_info.errors.full_messages if @license_info
76 80 end
77 81  
78 82 def control_software_creation
79   - valid_models = request.post? && (@community.valid? && @software_info.valid? && @license_info.valid?)
80   - if valid_models
81   - send_software_to_moderation
82   - else
83   - add_software_erros
  83 + if request.post?
  84 + valid_models = @community.valid?
  85 + valid_models &= @software_info.valid?
  86 + valid_models &= @license_info.valid?
  87 + if valid_models
  88 + send_software_to_moderation
  89 + else
  90 + add_software_erros
  91 + end
84 92 end
85 93 end
86 94  
... ... @@ -91,7 +99,7 @@ class SoftwareCommunitiesPluginMyprofileController < MyProfileController
91 99 }
92 100 end
93 101  
94   - def constroy_software
  102 + def create_software
95 103 @software_info = @profile.software_info
96 104 another_license_version = nil
97 105 another_license_link = nil
... ... @@ -136,7 +144,7 @@ class SoftwareCommunitiesPluginMyprofileController < MyProfileController
136 144  
137 145 add_admin_to_community
138 146  
139   - if !environment.admins.include?(current_user.person)
  147 + if !environment.admins.include?(current_user.person)
140 148 session[:notice] = _('Your new software request will be evaluated by an'\
141 149 'administrator. You will be notified.')
142 150 redirect_to user.admin_url
... ...
src/noosfero-spb/software_communities/lib/create_software.rb
... ... @@ -5,12 +5,14 @@ class CreateSoftware < 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, :another_license_version,
  9 + :another_license_link
9 10  
10 11 alias :environment :target
11 12 alias :environment= :target=
12 13  
13   - DATA_FIELDS = ['name', 'finality', 'license_info', 'repository_link']
  14 + DATA_FIELDS = ['name', 'identifier', 'finality', 'license_info', 'repository_link',
  15 + 'another_license_version', 'another_license_link']
14 16 DATA_FIELDS.each do |field|
15 17 settings_items field.to_sym
16 18 end
... ... @@ -21,15 +23,21 @@ class CreateSoftware < Task
21 23 template_id = software_template.id
22 24 end
23 25  
  26 + identifier = self.identifier
  27 + identifier ||= self.name.to_slug
  28 +
24 29 community = Community.create!(:name => self.name,
  30 + :identifier => identifier,
25 31 :template_id => template_id)
26 32  
27 33 community.environment = self.environment
28 34 community.add_admin(self.requestor)
29 35  
30   - software = SoftwareInfo.create!(:finality => self.finality,
  36 + software = SoftwareInfo.new(:finality => self.finality,
31 37 :repository_link => self.repository_link, :community_id => community.id,
32 38 :license_info => self.license_info)
  39 + software.verify_license_info(self.another_license_version, self.another_license_link)
  40 + software.save!
33 41 end
34 42  
35 43 def title
... ...
src/noosfero-spb/software_communities/lib/database_helper.rb
... ... @@ -34,12 +34,7 @@ class DatabaseHelper < 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 < 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 < 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 < 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 < 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
... ... @@ -22,8 +22,8 @@ class SoftwareCommunitiesPlugin &lt; Noosfero::Plugin
22 22 end
23 23  
24 24 def profile_tabs
25   - if context.profile.community?
26   - return profile_tabs_software if context.profile.software?
  25 + if context.profile.community? && context.profile.software?
  26 + return profile_tabs_software
27 27 end
28 28 end
29 29  
... ... @@ -107,7 +107,6 @@ class SoftwareCommunitiesPlugin &lt; Noosfero::Plugin
107 107 is_admin ||= user_rating.organization.admins.include?(current_user.person)
108 108  
109 109 if is_admin and profile.software?
110   -
111 110 render :file => 'organization_ratings_extra_fields_show_data',
112 111 :locals => {:user_rating => user_rating}
113 112 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,13 +166,16 @@ 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   - :license_info => license_info
  175 + :identifier => identifier,
  176 + :license_info => license_info,
  177 + :another_license_version => another_license_version,
  178 + :another_license_link => another_license_link
176 179 )
177 180 )
178 181 else
... ... @@ -189,15 +192,15 @@ class SoftwareInfo &lt; ActiveRecord::Base
189 192 community.template_id = software_template.id
190 193 end
191 194  
192   - software_info.license_info = license_info
193   - software_info.save
194   - community.software_info = software_info
195 195 community.save!
196 196 community.add_admin(requestor)
  197 +
  198 + software_info.community = community
  199 + software_info.license_info = license_info
  200 + software_info.verify_license_info(another_license_version, another_license_link)
  201 + software_info.save!
197 202 end
198 203  
199   - software_info.verify_license_info(another_license_version, another_license_link)
200   - software_info.save!
201 204 software_info
202 205 end
203 206  
... ...
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
... ... @@ -203,9 +203,100 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
203 203 :profile => @person.identifier
204 204 )
205 205  
206   - assert_equal SoftwareInfo.last.license_info_id, license_another.id
207   - assert_equal SoftwareInfo.last.license_info.id, nil
208   - assert_equal SoftwareInfo.last.license_info.version, another_license_version
209   - assert_equal SoftwareInfo.last.license_info.link, another_license_link
  206 + assert_equal license_another.id, SoftwareInfo.last.license_info_id
  207 + assert_equal nil, SoftwareInfo.last.license_info.id
  208 + assert_equal another_license_version, SoftwareInfo.last.license_info.version
  209 + assert_equal another_license_link, SoftwareInfo.last.license_info.link
210 210 end
  211 +
  212 + should "create software_info after finish task with 'Another' license_info" do
  213 + license_another = LicenseInfo.create(:version => "Another", :link => "#")
  214 +
  215 + another_license_version = "Different License"
  216 + another_license_link = "http://diferent.link"
  217 +
  218 + post(
  219 + :new_software,
  220 + :community => { :name => "New Software", :identifier => "new-software" },
  221 + :software_info => { :finality => "something", :repository_link => "" },
  222 + :license => { :license_infos_id => license_another.id,
  223 + :version => another_license_version,
  224 + :link=> another_license_link
  225 + },
  226 + :profile => @person.identifier
  227 + )
  228 +
  229 + @environment.add_admin(@person)
  230 + Task.last.send('finish', @person)
  231 +
  232 + assert_equal license_another.id, SoftwareInfo.last.license_info_id
  233 + assert_equal nil, SoftwareInfo.last.license_info.id
  234 + assert_equal another_license_version, SoftwareInfo.last.license_info.version
  235 + assert_equal another_license_link, SoftwareInfo.last.license_info.link
  236 + end
  237 +
  238 + should "show error messages on create software_info" do
  239 + post(
  240 + :new_software,
  241 + :community => {},
  242 + :software_info => {},
  243 + :license => {},
  244 + :profile => @person.identifier
  245 + )
  246 + assert_includes @response.body, "Domain can't be blank"
  247 + assert_includes @response.body, "Name can't be blank"
  248 + assert_includes @response.body, "Finality can't be blank"
  249 + assert_includes @response.body, "Version can't be blank"
  250 + end
  251 +
  252 + should "show domain not available error" do
  253 + @environment.add_admin(@person)
  254 +
  255 + post(
  256 + :new_software,
  257 + :community => {:name =>"New Software", :identifier => "new-software"},
  258 + :software_info => {:finality => "something", :repository_link => ""},
  259 + :license =>{:license_infos_id => LicenseInfo.last.id},
  260 + :profile => @person.identifier
  261 + )
  262 + post(
  263 + :new_software,
  264 + :community => {:name =>"New Software", :identifier => "new-software"},
  265 + :software_info => {:finality => "something", :repository_link => ""},
  266 + :license =>{:license_infos_id => LicenseInfo.last.id},
  267 + :profile => @person.identifier
  268 + )
  269 +
  270 + assert_includes @response.body, "Domain is not available"
  271 + end
  272 +
  273 + should "create software with admin moderation" do
  274 + @environment.enable('admin_must_approve_new_communities')
  275 +
  276 + post(
  277 + :new_software,
  278 + :community => {:name =>"New Software", :identifier => "new-software"},
  279 + :software_info => {:finality => "something", :repository_link => ""},
  280 + :license =>{:license_infos_id => LicenseInfo.last.id},
  281 + :profile => @person.identifier
  282 + )
  283 +
  284 + @environment.add_admin(@person)
  285 + Task.last.send('finish', @person)
  286 +
  287 + assert_equal "New Software", Task.last.data[:name]
  288 + assert_equal "New Software", SoftwareInfo.last.community.name
  289 + end
  290 +
  291 + should "dont create software without accept task" do
  292 + assert_no_difference 'SoftwareInfo.count' do
  293 + post(
  294 + :new_software,
  295 + :community => {:name =>"New Software", :identifier => "new-software"},
  296 + :software_info => {:finality => "something", :repository_link => ""},
  297 + :license =>{:license_infos_id => LicenseInfo.last.id},
  298 + :profile => @person.identifier
  299 + )
  300 + end
  301 + end
211 302 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  
... ...