diff --git a/app/models/community.rb b/app/models/community.rb index 58d16b3..557a399 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -64,7 +64,7 @@ class Community < Organization self.identifier = value.to_slug end - def template + def default_template environment.community_template end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 23ed771..52bd36e 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -154,13 +154,14 @@ class Enterprise < Organization true end - def template - if enabled? - environment.enterprise_template - else - environment.inactive_enterprise_template - end + def default_template + environment.enterprise_template + end + + def template_with_inactive_enterprise + !enabled? ? environment.inactive_enterprise_template : template_without_inactive_enterprise end + alias_method_chain :template, :inactive_enterprise def control_panel_settings_button {:title => __('Enterprise Info and settings'), :icon => 'edit-profile-enterprise'} diff --git a/app/models/environment.rb b/app/models/environment.rb index 3df675c..9c526cc 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -719,12 +719,12 @@ class Environment < ActiveRecord::Base def create_templates pre = self.name.to_slug + '_' - ent_id = Enterprise.create!(:name => 'Enterprise template', :identifier => pre + 'enterprise_template', :environment => self, :visible => false).id - inactive_enterprise_tmpl = Enterprise.create!(:name => 'Inactive Enterprise template', :identifier => pre + 'inactive_enterprise_template', :environment => self, :visible => false) - com_id = Community.create!(:name => 'Community template', :identifier => pre + 'community_template', :environment => self, :visible => false).id + ent_id = Enterprise.create!(:name => 'Enterprise template', :identifier => pre + 'enterprise_template', :environment => self, :visible => false, :is_template => true).id + inactive_enterprise_tmpl = Enterprise.create!(:name => 'Inactive Enterprise template', :identifier => pre + 'inactive_enterprise_template', :environment => self, :visible => false, :is_template => true) + com_id = Community.create!(:name => 'Community template', :identifier => pre + 'community_template', :environment => self, :visible => false, :is_template => true).id pass = Digest::MD5.hexdigest rand.to_s user = User.create!(:login => (pre + 'person_template'), :email => (pre + 'template@template.noo'), :password => pass, :password_confirmation => pass, :environment => self).person - user.update_attributes(:visible => false, :name => "Person template") + user.update_attributes(:visible => false, :name => "Person template", :is_template => true) usr_id = user.id self.settings[:enterprise_template_id] = ent_id self.inactive_enterprise_template = inactive_enterprise_tmpl diff --git a/app/models/organization.rb b/app/models/organization.rb index 0e02b93..22e9b81 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -77,6 +77,7 @@ class Organization < Profile state country tag_list + template_id ] def self.fields diff --git a/app/models/person.rb b/app/models/person.rb index bbba1fb..c333496 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -285,7 +285,7 @@ class Person < Profile end end - def template + def default_template environment.person_template end diff --git a/app/models/profile.rb b/app/models/profile.rb index 3e028e0..1a8126a 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -65,6 +65,7 @@ class Profile < ActiveRecord::Base #FIXME: these will work only if the subclass is already loaded named_scope :enterprises, lambda { {:conditions => (Enterprise.send(:subclasses).map(&:name) << 'Enterprise').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} } named_scope :communities, lambda { {:conditions => (Community.send(:subclasses).map(&:name) << 'Community').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} } + named_scope :templates, :conditions => {:is_template => true} def members Person.members_of(self) @@ -98,6 +99,7 @@ class Profile < ActiveRecord::Base has_many :action_tracker_notifications, :foreign_key => 'profile_id' has_many :tracked_notifications, :through => :action_tracker_notifications, :source => :action_tracker, :order => 'updated_at DESC' has_many :scraps_received, :class_name => 'Scrap', :foreign_key => :receiver_id, :order => "updated_at DESC", :dependent => :destroy + belongs_to :template, :class_name => 'Profile', :foreign_key => 'template_id' # FIXME ugly workaround def self.human_attribute_name(attrib) @@ -274,8 +276,14 @@ class Profile < ActiveRecord::Base validates_format_of :identifier, :with => IDENTIFIER_FORMAT, :if => lambda { |profile| !profile.identifier.blank? } validates_exclusion_of :identifier, :in => RESERVED_IDENTIFIERS validates_uniqueness_of :identifier, :scope => :environment_id - validates_length_of :nickname, :maximum => 16, :allow_nil => true + validate :valid_template + + def valid_template + if template_id.present? and !template.is_template + errors.add(:template, _('%{fn} is not a template.').fix_i18n) + end + end before_create :set_default_environment def set_default_environment @@ -322,10 +330,15 @@ class Profile < ActiveRecord::Base end # this method should be overwritten to provide the correct template - def template + def default_template nil end + def template_with_default + template_without_default || default_template + end + alias_method_chain :template, :default + def apply_template(template, options = {:copy_articles => true}) copy_blocks_from(template) copy_articles_from(template) if options[:copy_articles] diff --git a/db/migrate/20120710033223_add_template_and_is_template_fields.rb b/db/migrate/20120710033223_add_template_and_is_template_fields.rb new file mode 100644 index 0000000..6c4ab02 --- /dev/null +++ b/db/migrate/20120710033223_add_template_and_is_template_fields.rb @@ -0,0 +1,11 @@ +class AddTemplateAndIsTemplateFields < ActiveRecord::Migration + def self.up + add_column :profiles, :is_template, :boolean, :default => false + add_column :profiles, :template_id, :integer + end + + def self.down + remove_column :profiles, :is_template + remove_column :profiles, :template_id + end +end diff --git a/db/migrate/20120710062802_fill_is_template_field_on_basic_templates.rb b/db/migrate/20120710062802_fill_is_template_field_on_basic_templates.rb new file mode 100644 index 0000000..9a89800 --- /dev/null +++ b/db/migrate/20120710062802_fill_is_template_field_on_basic_templates.rb @@ -0,0 +1,9 @@ +class FillIsTemplateFieldOnBasicTemplates < ActiveRecord::Migration + def self.up + update("update profiles set is_template = 't' where identifier like '%_template'") + end + + def self.down + say('This migration can\'t be reverted.') + end +end diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index 798b658..37f6cdc 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -85,6 +85,12 @@ class CommunityTest < ActiveSupport::TestCase end should 'have a community template' do + template = fast_create(Community, :is_template => true) + p = Community.create!(:name => 'test_com', :identifier => 'test_com', :template => template) + assert_equal template, p.template + end + + should 'have a default community template' do env = Environment.create!(:name => 'test env') p = Community.create!(:name => 'test_com', :identifier => 'test_com', :environment => env) assert_kind_of Community, p.template diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index 875e4bb..2e6310e 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -278,11 +278,30 @@ class EnterpriseTest < ActiveSupport::TestCase end should 'have a enterprise template' do + template = fast_create(Enterprise, :is_template => true) + p = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :template_id => template.id) + assert_equal template, p.template + end + + should 'have a default enterprise template' do env = Environment.create!(:name => 'test env') p = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :environment_id => env.id) assert_kind_of Enterprise, p.template end + should 'have inactive_template even when there is a template set' do + another_template = fast_create(Enterprise, :is_template => true) + inactive_template = fast_create(Enterprise, :name => 'inactive enteprise template', :identifier => 'inactive_enterprise_template', :is_template => true) + + e = Environment.default + e.enable('enterprises_are_disabled_when_created') + e.inactive_enterprise_template = inactive_template + e.save! + + ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :template => another_template, :environment => e) + assert_equal inactive_template, ent.template + end + should 'contact us enabled by default' do e = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :environment_id => Environment.default.id) assert e.enable_contact_us diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 3116485..b1b9eac 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -357,6 +357,14 @@ class PersonTest < ActiveSupport::TestCase end should 'have a person template' do + template = fast_create(Person, :is_template => true) + p = create_user('test_user').person + p.template_id = template.id + p.save! + assert_equal template, p.template + end + + should 'have a default person template' do env = Environment.create!(:name => 'test env') p = create_user('test_user', :environment => env).person assert_kind_of Person, p.template diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 59e5bc6..303932a 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1410,6 +1410,42 @@ class ProfileTest < ActiveSupport::TestCase assert_equal "header customized", person.custom_header end + should 'not have a profile as a template if it is not defined as a template' do + template = fast_create(Profile) + profile = Profile.new(:template => template) + !profile.valid? + assert profile.errors.invalid?(:template) + + template.is_template = true + template.save! + profile.valid? + assert !profile.errors.invalid?(:template) + end + + should 'be able to have a template' do + template = fast_create(Profile, :is_template => true) + profile = fast_create(Profile, :template_id => template.id) + assert_equal template, profile.template + end + + should 'have a default template' do + template = fast_create(Profile, :is_template => true) + profile = fast_create(Profile) + profile.stubs(:default_template).returns(template) + + assert_equal template, profile.template + end + + should 'return a list of templates' do + t1 = fast_create(Profile, :is_template => true) + t2 = fast_create(Profile, :is_template => true) + profile = fast_create(Profile) + + assert_includes Profile.templates, t1 + assert_includes Profile.templates, t2 + assert_not_includes Profile.templates, profile + end + should 'provide URL to leave' do profile = build(Profile, :identifier => 'testprofile') assert_equal({ :profile => 'testprofile', :controller => 'profile', :action => 'leave', :reload => false}, profile.leave_url) -- libgit2 0.21.2