Commit d273adf7bf40b1e996ca99d7e9cc1012be119f8a

Authored by Leandro Santos
Committed by Joenio Costa
1 parent 202b94a2

Refactoring of environment model:

* A lot of environment methods were not using settings_items in yours
  definition. In some cases use the code settings_items instead of
  define the method entirelly is more appropriated
* The method boxes_limit was defined using LayoutTemplate? class in one
  case and in the other case, using the database.

(ActionItem1568)
app/models/environment.rb
@@ -161,15 +161,47 @@ class Environment < ActiveRecord::Base @@ -161,15 +161,47 @@ class Environment < ActiveRecord::Base
161 # ################################################# 161 # #################################################
162 162
163 # store the Environment settings as YAML-serialized Hash. 163 # store the Environment settings as YAML-serialized Hash.
164 - serialize :settings 164 + acts_as_having_settings :field => :settings
165 165
166 - def homepage  
167 - settings[:homepage]  
168 - end 166 + # the environment's terms of use: every user must accept them before registering.
  167 + settings_items :terms_of_use, :type => String
  168 +
  169 + # the environment's terms of enterprise use: every enterprise member must accept them before
  170 + # registering or activating enterprises.
  171 + settings_items :terms_of_enterprise_use, :type => String
169 172
170 - # returns a Hash containing the Environment configuration  
171 - def settings  
172 - self[:settings] ||= {} 173 + # returns the approval method used for this environment. Possible values are:
  174 + #
  175 + # Defaults to <tt>:admim</tt>.
  176 + settings_items :organization_approval_method, :type => Symbol, :default => :admin
  177 +
  178 + # Whether this environment should force having 'www.' in its domain name or
  179 + # not. Defauls to false.
  180 + #
  181 + # Sets the value of #force_www. <tt>value</tt> must be a boolean.
  182 + #
  183 + # See also #default_hostname
  184 + settings_items :force_www, :default => false
  185 +
  186 + settings_items :message_for_friend_invitation, :type => String, :default => InviteFriend.mail_template
  187 + settings_items :message_for_member_invitation, :type => String, :default => InviteMember.mail_template
  188 + settings_items :activation_blocked_text, :type => String
  189 + settings_items :message_for_disabled_enterprise, :type => String
  190 + settings_items :location, :type => String
  191 + settings_items :layout_template, :type => String, :default => 'default'
  192 + settings_items :homepage, :type => String
  193 + settings_items :description, :type => String
  194 + settings_items :category_types, :type => Array, :default => ['Category']
  195 + settings_items :enable_ssl
  196 + settings_items :theme, :type => String, :default => 'default'
  197 + settings_items :icon_theme, :type => String, :default => 'default'
  198 + settings_items :local_docs, :type => Array, :default => []
  199 + settings_items :news_amount_by_folder, :type => Integer, :default => 4
  200 + settings_items :help_message_to_add_enterprise, :type => String, :default => ''
  201 + settings_items :tip_message_enterprise_activation_question, :type => String, :default => ''
  202 +
  203 + def news_amount_by_folder=(amount)
  204 + settings[:news_amount_by_folder] = amount.to_i
173 end 205 end
174 206
175 # Enables a feature identified by its name 207 # Enables a feature identified by its name
@@ -201,71 +233,16 @@ class Environment &lt; ActiveRecord::Base @@ -201,71 +233,16 @@ class Environment &lt; ActiveRecord::Base
201 end 233 end
202 end 234 end
203 235
204 - # the environment's terms of use: every user must accept them before  
205 - # registering.  
206 - def terms_of_use  
207 - self.settings['terms_of_use']  
208 - end  
209 -  
210 - # sets the environment's terms of use.  
211 - def terms_of_use=(value)  
212 - self.settings['terms_of_use'] = value.blank? ? nil : value  
213 - end  
214 -  
215 # returns <tt>true</tt> if this Environment has terms of use to be 236 # returns <tt>true</tt> if this Environment has terms of use to be
216 # accepted by users before registration. 237 # accepted by users before registration.
217 def has_terms_of_use? 238 def has_terms_of_use?
218 - ! self.settings['terms_of_use'].nil?  
219 - end  
220 -  
221 - # the environment's terms of enterprise use: every enterprise member must accept them before  
222 - # registering or activating enterprises.  
223 - def terms_of_enterprise_use  
224 - self.settings['terms_of_enterprise_use']  
225 - end  
226 -  
227 - # sets the environment's terms of enterprise use.  
228 - def terms_of_enterprise_use=(value)  
229 - self.settings['terms_of_enterprise_use'] = value 239 + ! self.terms_of_use.blank?
230 end 240 end
231 241
232 # returns <tt>true</tt> if this Environment has terms of enterprise use to be 242 # returns <tt>true</tt> if this Environment has terms of enterprise use to be
233 # accepted by users before registration or activation of enterprises. 243 # accepted by users before registration or activation of enterprises.
234 def has_terms_of_enterprise_use? 244 def has_terms_of_enterprise_use?
235 - ! self.settings['terms_of_enterprise_use'].blank?  
236 - end  
237 -  
238 - def activation_blocked_text  
239 - self.settings['activation_blocked_text']  
240 - end  
241 -  
242 - def activation_blocked_text= value  
243 - self.settings['activation_blocked_text'] = value  
244 - end  
245 -  
246 - def message_for_disabled_enterprise  
247 - self.settings['message_for_disabled_enterprise']  
248 - end  
249 -  
250 - def message_for_disabled_enterprise=(value)  
251 - self.settings['message_for_disabled_enterprise'] = value  
252 - end  
253 -  
254 - # the environment's default location  
255 - def location  
256 - self.settings['location']  
257 - end  
258 -  
259 - # sets the environment's location.  
260 - def location=(value)  
261 - self.settings['location'] = value  
262 - end  
263 -  
264 - # returns the approval method used for this environment. Possible values are:  
265 - #  
266 - # Defaults to <tt>:admim</tt>.  
267 - def organization_approval_method  
268 - self.settings['organization_approval_method'] || :admin 245 + ! self.terms_of_enterprise_use.blank?
269 end 246 end
270 247
271 # Sets the organization_approval_method. Only accepts the following values: 248 # Sets the organization_approval_method. Only accepts the following values:
@@ -292,17 +269,7 @@ class Environment &lt; ActiveRecord::Base @@ -292,17 +269,7 @@ class Environment &lt; ActiveRecord::Base
292 ].map(&:to_sym) 269 ].map(&:to_sym)
293 raise ArgumentError unless accepted_values.include?(actual_value) 270 raise ArgumentError unless accepted_values.include?(actual_value)
294 271
295 - self.settings['organization_approval_method'] = actual_value  
296 - end  
297 -  
298 - # the description of the environment. Normally used in the homepage.  
299 - def description  
300 - self.settings[:description]  
301 - end  
302 -  
303 - # sets the #description of the environment  
304 - def description=(value)  
305 - self.settings[:description] = value 272 + self.settings[:organization_approval_method] = actual_value
306 end 273 end
307 274
308 def terminology 275 def terminology
@@ -370,22 +337,6 @@ class Environment &lt; ActiveRecord::Base @@ -370,22 +337,6 @@ class Environment &lt; ActiveRecord::Base
370 end 337 end
371 end 338 end
372 339
373 - def message_for_friend_invitation  
374 - self.settings['message_for_friend_invitation'] || InviteFriend.mail_template  
375 - end  
376 -  
377 - def message_for_friend_invitation=(value)  
378 - self.settings['message_for_friend_invitation'] = value  
379 - end  
380 -  
381 - def message_for_member_invitation  
382 - self.settings['message_for_member_invitation'] || InviteMember.mail_template  
383 - end  
384 -  
385 - def message_for_member_invitation=(value)  
386 - self.settings['message_for_member_invitation'] = value  
387 - end  
388 -  
389 def custom_enterprise_fields 340 def custom_enterprise_fields
390 self.settings[:custom_enterprise_fields].nil? ? {} : self.settings[:custom_enterprise_fields] 341 self.settings[:custom_enterprise_fields].nil? ? {} : self.settings[:custom_enterprise_fields]
391 end 342 end
@@ -440,27 +391,6 @@ class Environment &lt; ActiveRecord::Base @@ -440,27 +391,6 @@ class Environment &lt; ActiveRecord::Base
440 required_fields 391 required_fields
441 end 392 end
442 393
443 - def category_types  
444 - self.settings[:category_types].nil? ? ['Category'] : self.settings[:category_types]  
445 - end  
446 -  
447 - def category_types=(values)  
448 - self.settings[:category_types] = values  
449 - end  
450 -  
451 - # Whether this environment should force having 'www.' in its domain name or  
452 - # not. Defauls to false.  
453 - #  
454 - # See also #default_hostname  
455 - def force_www  
456 - settings[:force_www] || false  
457 - end  
458 -  
459 - # Sets the value of #force_www. <tt>value</tt> must be a boolean.  
460 - def force_www=(value)  
461 - settings[:force_www] = value  
462 - end  
463 -  
464 # ################################################# 394 # #################################################
465 # Validations 395 # Validations
466 # ################################################# 396 # #################################################
@@ -512,14 +442,6 @@ class Environment &lt; ActiveRecord::Base @@ -512,14 +442,6 @@ class Environment &lt; ActiveRecord::Base
512 result 442 result
513 end 443 end
514 444
515 - def enable_ssl  
516 - settings[:enable_ssl]  
517 - end  
518 -  
519 - def enable_ssl=(value)  
520 - settings[:enable_ssl] = value  
521 - end  
522 -  
523 def to_s 445 def to_s
524 self.name || '?' 446 self.name || '?'
525 end 447 end
@@ -542,10 +464,6 @@ class Environment &lt; ActiveRecord::Base @@ -542,10 +464,6 @@ class Environment &lt; ActiveRecord::Base
542 end 464 end
543 end 465 end
544 466
545 - def theme  
546 - self[:theme] || 'default'  
547 - end  
548 -  
549 def themes 467 def themes
550 if settings[:themes] 468 if settings[:themes]
551 Theme.system_themes.select { |theme| settings[:themes].include?(theme.id) } 469 Theme.system_themes.select { |theme| settings[:themes].include?(theme.id) }
@@ -558,21 +476,6 @@ class Environment &lt; ActiveRecord::Base @@ -558,21 +476,6 @@ class Environment &lt; ActiveRecord::Base
558 settings[:themes] = values.map(&:id) 476 settings[:themes] = values.map(&:id)
559 end 477 end
560 478
561 - def icon_theme  
562 - settings[:icon_theme] || 'default'  
563 - end  
564 - def icon_theme=(theme)  
565 - settings[:icon_theme] = theme  
566 - end  
567 -  
568 - def layout_template  
569 - settings[:layout_template] || 'default'  
570 - end  
571 -  
572 - def layout_template=(value)  
573 - settings[:layout_template] = value  
574 - end  
575 -  
576 def community_template 479 def community_template
577 Community.find_by_id settings[:community_template_id] 480 Community.find_by_id settings[:community_template_id]
578 end 481 end
@@ -641,30 +544,6 @@ class Environment &lt; ActiveRecord::Base @@ -641,30 +544,6 @@ class Environment &lt; ActiveRecord::Base
641 settings[:portal_folders] = folders ? folders.map(&:id) : nil 544 settings[:portal_folders] = folders ? folders.map(&:id) : nil
642 end 545 end
643 546
644 - def news_amount_by_folder  
645 - (settings[:news_amount_by_folder] || 4)  
646 - end  
647 -  
648 - def news_amount_by_folder=(amount)  
649 - settings[:news_amount_by_folder] = amount.to_i  
650 - end  
651 -  
652 - def help_message_to_add_enterprise  
653 - self.settings['help_message_to_add_enterprise'] || ''  
654 - end  
655 -  
656 - def help_message_to_add_enterprise=(value)  
657 - self.settings['help_message_to_add_enterprise'] = value  
658 - end  
659 -  
660 - def tip_message_enterprise_activation_question  
661 - self.settings['tip_message_enterprise_activation_question'] || ''  
662 - end  
663 -  
664 - def tip_message_enterprise_activation_question=(value)  
665 - self.settings['tip_message_enterprise_activation_question'] = value  
666 - end  
667 -  
668 def portal_news_cache_key 547 def portal_news_cache_key
669 "home-page-news/#{cache_key}" 548 "home-page-news/#{cache_key}"
670 end 549 end
@@ -697,12 +576,4 @@ class Environment &lt; ActiveRecord::Base @@ -697,12 +576,4 @@ class Environment &lt; ActiveRecord::Base
697 end 576 end
698 end 577 end
699 578
700 - def local_docs  
701 - settings[:local_docs] || []  
702 - end  
703 -  
704 - def local_docs=(value)  
705 - settings[:local_docs] = value  
706 - end  
707 -  
708 end 579 end
app/models/profile.rb
@@ -605,10 +605,6 @@ private :generate_url, :url_options @@ -605,10 +605,6 @@ private :generate_url, :url_options
605 605
606 settings_items :layout_template, :type => String, :default => 'default' 606 settings_items :layout_template, :type => String, :default => 'default'
607 607
608 - def boxes_limit  
609 - LayoutTemplate.find(layout_template).number_of_boxes  
610 - end  
611 -  
612 has_many :blogs, :source => 'articles', :class_name => 'Blog' 608 has_many :blogs, :source => 'articles', :class_name => 'Blog'
613 609
614 def blog 610 def blog
app/views/account/_signup_form.rhtml
@@ -41,7 +41,7 @@ @@ -41,7 +41,7 @@
41 <%= render :partial => 'profile_editor/person_form', :locals => {:f => f} %> 41 <%= render :partial => 'profile_editor/person_form', :locals => {:f => f} %>
42 <% end %> 42 <% end %>
43 43
44 -<% if @terms_of_use %> 44 +<% unless @terms_of_use.blank? %>
45 <div id='terms-of-use-box' class='formfieldline'> 45 <div id='terms-of-use-box' class='formfieldline'>
46 <%= _("By clicking on 'I accept the terms of use' below you are agreeing to the %s") % 46 <%= _("By clicking on 'I accept the terms of use' below you are agreeing to the %s") %
47 link_to_function(_('Terms of use'), nil) do |page| 47 link_to_function(_('Terms of use'), nil) do |page|
app/views/account/blocked.rhtml
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 <% end %> 9 <% end %>
10 </p> 10 </p>
11 11
12 -<% if @environment.activation_blocked_text.nil? %> 12 +<% if @environment.activation_blocked_text.blank? %>
13 <%= _('Your enterprise has been blocked') %> 13 <%= _('Your enterprise has been blocked') %>
14 <% else %> 14 <% else %>
15 <%= @environment.activation_blocked_text %> 15 <%= @environment.activation_blocked_text %>
lib/acts_as_having_boxes.rb
@@ -29,8 +29,9 @@ module ActsAsHavingBoxes @@ -29,8 +29,9 @@ module ActsAsHavingBoxes
29 # returns 3 unless the class table has a boxes_limit column. In that case 29 # returns 3 unless the class table has a boxes_limit column. In that case
30 # return the value of the column. 30 # return the value of the column.
31 def boxes_limit 31 def boxes_limit
32 - self[:boxes_limit] || 3 32 + LayoutTemplate.find(layout_template).number_of_boxes || 3
33 end 33 end
  34 +
34 end 35 end
35 36
36 ActiveRecord::Base.extend(ActsAsHavingBoxes::ClassMethods) 37 ActiveRecord::Base.extend(ActsAsHavingBoxes::ClassMethods)
test/unit/environment_test.rb
@@ -76,9 +76,8 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -76,9 +76,8 @@ class EnvironmentTest &lt; Test::Unit::TestCase
76 assert_nil v.terms_of_use 76 assert_nil v.terms_of_use
77 v.terms_of_use = "" 77 v.terms_of_use = ""
78 assert v.save 78 assert v.save
  79 + v.reload
79 assert !v.has_terms_of_use? 80 assert !v.has_terms_of_use?
80 - id = v.id  
81 - assert_nil Environment.find(v.id).terms_of_use  
82 end 81 end
83 82
84 def test_has_terms_of_use 83 def test_has_terms_of_use
@@ -102,6 +101,8 @@ class EnvironmentTest &lt; Test::Unit::TestCase @@ -102,6 +101,8 @@ class EnvironmentTest &lt; Test::Unit::TestCase
102 assert !v.has_terms_of_enterprise_use? 101 assert !v.has_terms_of_enterprise_use?
103 v.terms_of_enterprise_use = 'some terms of enterprise use' 102 v.terms_of_enterprise_use = 'some terms of enterprise use'
104 assert v.has_terms_of_enterprise_use? 103 assert v.has_terms_of_enterprise_use?
  104 + v.terms_of_enterprise_use = ''
  105 + assert !v.has_terms_of_enterprise_use?
105 end 106 end
106 107
107 def test_should_list_top_level_categories 108 def test_should_list_top_level_categories