Commit fdf61a5a17b7b5bd0cfefcd349f780809a4eca98

Authored by Rodrigo Souto
Committed by Daniela Feitosa
1 parent 0d563e00

[multiple-templates] Adding templates field and relations to profile

(ActionItem2378)
app/models/community.rb
... ... @@ -64,7 +64,7 @@ class Community < Organization
64 64 self.identifier = value.to_slug
65 65 end
66 66  
67   - def template
  67 + def default_template
68 68 environment.community_template
69 69 end
70 70  
... ...
app/models/enterprise.rb
... ... @@ -154,13 +154,14 @@ class Enterprise < Organization
154 154 true
155 155 end
156 156  
157   - def template
158   - if enabled?
159   - environment.enterprise_template
160   - else
161   - environment.inactive_enterprise_template
162   - end
  157 + def default_template
  158 + environment.enterprise_template
  159 + end
  160 +
  161 + def template_with_inactive_enterprise
  162 + !enabled? ? environment.inactive_enterprise_template : template_without_inactive_enterprise
163 163 end
  164 + alias_method_chain :template, :inactive_enterprise
164 165  
165 166 def control_panel_settings_button
166 167 {:title => __('Enterprise Info and settings'), :icon => 'edit-profile-enterprise'}
... ...
app/models/environment.rb
... ... @@ -719,12 +719,12 @@ class Environment < ActiveRecord::Base
719 719  
720 720 def create_templates
721 721 pre = self.name.to_slug + '_'
722   - ent_id = Enterprise.create!(:name => 'Enterprise template', :identifier => pre + 'enterprise_template', :environment => self, :visible => false).id
723   - inactive_enterprise_tmpl = Enterprise.create!(:name => 'Inactive Enterprise template', :identifier => pre + 'inactive_enterprise_template', :environment => self, :visible => false)
724   - com_id = Community.create!(:name => 'Community template', :identifier => pre + 'community_template', :environment => self, :visible => false).id
  722 + ent_id = Enterprise.create!(:name => 'Enterprise template', :identifier => pre + 'enterprise_template', :environment => self, :visible => false, :is_template => true).id
  723 + inactive_enterprise_tmpl = Enterprise.create!(:name => 'Inactive Enterprise template', :identifier => pre + 'inactive_enterprise_template', :environment => self, :visible => false, :is_template => true)
  724 + com_id = Community.create!(:name => 'Community template', :identifier => pre + 'community_template', :environment => self, :visible => false, :is_template => true).id
725 725 pass = Digest::MD5.hexdigest rand.to_s
726 726 user = User.create!(:login => (pre + 'person_template'), :email => (pre + 'template@template.noo'), :password => pass, :password_confirmation => pass, :environment => self).person
727   - user.update_attributes(:visible => false, :name => "Person template")
  727 + user.update_attributes(:visible => false, :name => "Person template", :is_template => true)
728 728 usr_id = user.id
729 729 self.settings[:enterprise_template_id] = ent_id
730 730 self.inactive_enterprise_template = inactive_enterprise_tmpl
... ...
app/models/organization.rb
... ... @@ -77,6 +77,7 @@ class Organization < Profile
77 77 state
78 78 country
79 79 tag_list
  80 + template_id
80 81 ]
81 82  
82 83 def self.fields
... ...
app/models/person.rb
... ... @@ -285,7 +285,7 @@ class Person < Profile
285 285 end
286 286 end
287 287  
288   - def template
  288 + def default_template
289 289 environment.person_template
290 290 end
291 291  
... ...
app/models/profile.rb
... ... @@ -65,6 +65,7 @@ class Profile < ActiveRecord::Base
65 65 #FIXME: these will work only if the subclass is already loaded
66 66 named_scope :enterprises, lambda { {:conditions => (Enterprise.send(:subclasses).map(&:name) << 'Enterprise').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} }
67 67 named_scope :communities, lambda { {:conditions => (Community.send(:subclasses).map(&:name) << 'Community').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} }
  68 + named_scope :templates, :conditions => {:is_template => true}
68 69  
69 70 def members
70 71 Person.members_of(self)
... ... @@ -98,6 +99,7 @@ class Profile &lt; ActiveRecord::Base
98 99 has_many :action_tracker_notifications, :foreign_key => 'profile_id'
99 100 has_many :tracked_notifications, :through => :action_tracker_notifications, :source => :action_tracker, :order => 'updated_at DESC'
100 101 has_many :scraps_received, :class_name => 'Scrap', :foreign_key => :receiver_id, :order => "updated_at DESC", :dependent => :destroy
  102 + belongs_to :template, :class_name => 'Profile', :foreign_key => 'template_id'
101 103  
102 104 # FIXME ugly workaround
103 105 def self.human_attribute_name(attrib)
... ... @@ -274,8 +276,14 @@ class Profile &lt; ActiveRecord::Base
274 276 validates_format_of :identifier, :with => IDENTIFIER_FORMAT, :if => lambda { |profile| !profile.identifier.blank? }
275 277 validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS
276 278 validates_uniqueness_of :identifier, :scope => :environment_id
277   -
278 279 validates_length_of :nickname, :maximum => 16, :allow_nil => true
  280 + validate :valid_template
  281 +
  282 + def valid_template
  283 + if template_id.present? and !template.is_template
  284 + errors.add(:template, _('%{fn} is not a template.').fix_i18n)
  285 + end
  286 + end
279 287  
280 288 before_create :set_default_environment
281 289 def set_default_environment
... ... @@ -322,10 +330,15 @@ class Profile &lt; ActiveRecord::Base
322 330 end
323 331  
324 332 # this method should be overwritten to provide the correct template
325   - def template
  333 + def default_template
326 334 nil
327 335 end
328 336  
  337 + def template_with_default
  338 + template_without_default || default_template
  339 + end
  340 + alias_method_chain :template, :default
  341 +
329 342 def apply_template(template, options = {:copy_articles => true})
330 343 copy_blocks_from(template)
331 344 copy_articles_from(template) if options[:copy_articles]
... ...
db/migrate/20120710033223_add_template_and_is_template_fields.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class AddTemplateAndIsTemplateFields < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :profiles, :is_template, :boolean, :default => false
  4 + add_column :profiles, :template_id, :integer
  5 + end
  6 +
  7 + def self.down
  8 + remove_column :profiles, :is_template
  9 + remove_column :profiles, :template_id
  10 + end
  11 +end
... ...
db/migrate/20120710062802_fill_is_template_field_on_basic_templates.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class FillIsTemplateFieldOnBasicTemplates < ActiveRecord::Migration
  2 + def self.up
  3 + update("update profiles set is_template = 't' where identifier like '%_template'")
  4 + end
  5 +
  6 + def self.down
  7 + say('This migration can\'t be reverted.')
  8 + end
  9 +end
... ...
test/unit/community_test.rb
... ... @@ -85,6 +85,12 @@ class CommunityTest &lt; ActiveSupport::TestCase
85 85 end
86 86  
87 87 should 'have a community template' do
  88 + template = fast_create(Community, :is_template => true)
  89 + p = Community.create!(:name => 'test_com', :identifier => 'test_com', :template => template)
  90 + assert_equal template, p.template
  91 + end
  92 +
  93 + should 'have a default community template' do
88 94 env = Environment.create!(:name => 'test env')
89 95 p = Community.create!(:name => 'test_com', :identifier => 'test_com', :environment => env)
90 96 assert_kind_of Community, p.template
... ...
test/unit/enterprise_test.rb
... ... @@ -278,11 +278,30 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
278 278 end
279 279  
280 280 should 'have a enterprise template' do
  281 + template = fast_create(Enterprise, :is_template => true)
  282 + p = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :template_id => template.id)
  283 + assert_equal template, p.template
  284 + end
  285 +
  286 + should 'have a default enterprise template' do
281 287 env = Environment.create!(:name => 'test env')
282 288 p = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :environment_id => env.id)
283 289 assert_kind_of Enterprise, p.template
284 290 end
285 291  
  292 + should 'have inactive_template even when there is a template set' do
  293 + another_template = fast_create(Enterprise, :is_template => true)
  294 + inactive_template = fast_create(Enterprise, :name => 'inactive enteprise template', :identifier => 'inactive_enterprise_template', :is_template => true)
  295 +
  296 + e = Environment.default
  297 + e.enable('enterprises_are_disabled_when_created')
  298 + e.inactive_enterprise_template = inactive_template
  299 + e.save!
  300 +
  301 + ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :template => another_template, :environment => e)
  302 + assert_equal inactive_template, ent.template
  303 + end
  304 +
286 305 should 'contact us enabled by default' do
287 306 e = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :environment_id => Environment.default.id)
288 307 assert e.enable_contact_us
... ...
test/unit/person_test.rb
... ... @@ -357,6 +357,14 @@ class PersonTest &lt; ActiveSupport::TestCase
357 357 end
358 358  
359 359 should 'have a person template' do
  360 + template = fast_create(Person, :is_template => true)
  361 + p = create_user('test_user').person
  362 + p.template_id = template.id
  363 + p.save!
  364 + assert_equal template, p.template
  365 + end
  366 +
  367 + should 'have a default person template' do
360 368 env = Environment.create!(:name => 'test env')
361 369 p = create_user('test_user', :environment => env).person
362 370 assert_kind_of Person, p.template
... ...
test/unit/profile_test.rb
... ... @@ -1410,6 +1410,42 @@ class ProfileTest &lt; ActiveSupport::TestCase
1410 1410 assert_equal "header customized", person.custom_header
1411 1411 end
1412 1412  
  1413 + should 'not have a profile as a template if it is not defined as a template' do
  1414 + template = fast_create(Profile)
  1415 + profile = Profile.new(:template => template)
  1416 + !profile.valid?
  1417 + assert profile.errors.invalid?(:template)
  1418 +
  1419 + template.is_template = true
  1420 + template.save!
  1421 + profile.valid?
  1422 + assert !profile.errors.invalid?(:template)
  1423 + end
  1424 +
  1425 + should 'be able to have a template' do
  1426 + template = fast_create(Profile, :is_template => true)
  1427 + profile = fast_create(Profile, :template_id => template.id)
  1428 + assert_equal template, profile.template
  1429 + end
  1430 +
  1431 + should 'have a default template' do
  1432 + template = fast_create(Profile, :is_template => true)
  1433 + profile = fast_create(Profile)
  1434 + profile.stubs(:default_template).returns(template)
  1435 +
  1436 + assert_equal template, profile.template
  1437 + end
  1438 +
  1439 + should 'return a list of templates' do
  1440 + t1 = fast_create(Profile, :is_template => true)
  1441 + t2 = fast_create(Profile, :is_template => true)
  1442 + profile = fast_create(Profile)
  1443 +
  1444 + assert_includes Profile.templates, t1
  1445 + assert_includes Profile.templates, t2
  1446 + assert_not_includes Profile.templates, profile
  1447 + end
  1448 +
1413 1449 should 'provide URL to leave' do
1414 1450 profile = build(Profile, :identifier => 'testprofile')
1415 1451 assert_equal({ :profile => 'testprofile', :controller => 'profile', :action => 'leave', :reload => false}, profile.leave_url)
... ...