From b588a1a6ba577d9b473183b48716186df33b7c74 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Fri, 31 Oct 2008 11:46:19 -0300 Subject: [PATCH] ActionItem789: making themes available only in its environment --- app/controllers/my_profile/themes_controller.rb | 2 +- app/models/environment.rb | 12 ++++++++++++ app/models/theme.rb | 17 +++++++++++------ test/functional/themes_controller_test.rb | 40 +++++++++++++++++++++++----------------- test/unit/environment_test.rb | 25 +++++++++++++++++++++++++ test/unit/theme_test.rb | 21 +++++++++++++++++---- 6 files changed, 89 insertions(+), 28 deletions(-) diff --git a/app/controllers/my_profile/themes_controller.rb b/app/controllers/my_profile/themes_controller.rb index 4f63eea..650fc45 100644 --- a/app/controllers/my_profile/themes_controller.rb +++ b/app/controllers/my_profile/themes_controller.rb @@ -9,7 +9,7 @@ class ThemesController < MyProfileController end def index - @themes = Theme.system_themes + Theme.public_themes + @themes = profile.environment.themes + Theme.approved_themes(profile) @current_theme = profile.theme @layout_templates = LayoutTemplate.all diff --git a/app/models/environment.rb b/app/models/environment.rb index 2ac4a3d..23ab916 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -315,6 +315,18 @@ class Environment < ActiveRecord::Base self[:theme] || 'default' end + def themes + if settings[:themes] + Theme.system_themes.select { |theme| settings[:themes].include?(theme.id) } + else + [] + end + end + + def themes=(values) + settings[:themes] = values.map(&:id) + end + def layout_template settings[:layout_template] || 'default' end diff --git a/app/models/theme.rb b/app/models/theme.rb index f22b8eb..56a3c55 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -2,7 +2,7 @@ class Theme class << self def system_themes - Dir.glob(RAILS_ROOT + '/public/designs/themes/*').map do |item| + Dir.glob(File.join(system_themes_dir, '*')).map do |item| File.basename(item) end.map do |item| new(item) @@ -13,6 +13,10 @@ class Theme File.join(RAILS_ROOT, 'public', 'user_themes') end + def system_themes_dir + File.join(RAILS_ROOT, 'public', 'designs', 'themes') + end + def create(id, attributes = {}) if find(id) || system_themes.map(&:id).include?(id) raise DuplicatedIdentifier @@ -37,12 +41,12 @@ class Theme end end - def public_themes - Dir.glob(File.join(user_themes_dir, '*', 'theme.yml')).select do |desc| - config = YAML.load_file(desc) - config['public'] + def approved_themes(owner) + Dir.glob(File.join(system_themes_dir, '*')).select do |item| + config = YAML.load_file(File.join(item, 'theme.yml')) + (config['owner_type'] == owner.class.base_class.name) && (config['owner_id'] == owner.id) || config['public'] end.map do |desc| - Theme.find(File.basename(File.dirname(desc))) + new(File.basename(desc)) end end @@ -59,6 +63,7 @@ class Theme attributes.each do |k,v| self.send("#{k}=", v) end + config['id'] = id end def name diff --git a/test/functional/themes_controller_test.rb b/test/functional/themes_controller_test.rb index 4589f47..f9c6e02 100644 --- a/test/functional/themes_controller_test.rb +++ b/test/functional/themes_controller_test.rb @@ -24,40 +24,46 @@ class ThemesControllerTest < Test::Unit::TestCase TMP_THEMES_DIR = RAILS_ROOT + '/test/tmp/themes_controller' - should 'display list of themes for selection' do - Theme.expects(:system_themes).returns([Theme.new('first'), Theme.new('second')]) + should 'display themes that can be applied' do + env = Environment.default + Theme.stubs(:approved_themes).with(@profile).returns([Theme.new('t1', :name => 't1')]) + t2 = Theme.create('t2') + t3 = Theme.create('t3') + env.themes = [t2] + env.save + + Theme.stubs(:system_themes).returns([t2, t3]) get :index, :profile => 'testinguser' - %w[ first second ].each do |item| + %w[ t1 t2 ].each do |item| assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/#{item}" } end - end - - should 'not display themes for selection if it is not public' do - Theme.create('first', :owner => profile, :public => true) - Theme.create('second', :owner => profile, :public => false) - get :index, :profile => 'testinguser' - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/first" } - assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/second" } + assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/t3" } end should 'highlight current theme' do - profile.update_attributes(:theme => 'first') - Theme.expects(:system_themes).returns([Theme.new('first'), Theme.new('second')]) + env = Environment.default + t1 = Theme.create('one', :name => 'one') + t2 = Theme.create('two', :name => 'two') + env.themes = [t1, t2] + env.save + + Theme.stubs(:system_themes).returns([t1, t2]) + profile.update_attributes(:theme => 'one') get :index, :profile => 'testinguser' assert_tag :attributes => { :class => 'selected theme' }, :descendant => { :content => /(current)/ } - assert_no_tag :attributes => { :class => 'selected theme' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/first" } } + assert_no_tag :attributes => { :class => 'selected theme' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/one" } } end should 'display list of my themes for edition' do - Theme.create('first', :owner => profile) - Theme.create('second', :owner => profile) + Theme.create('three', :owner => profile) + Theme.create('four', :owner => profile) get :index, :profile => 'testinguser' - %w[ first second ].each do |item| + %w[ three four ].each do |item| assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/edit/#{item}" } end end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 20569b1..4cc7689 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -397,6 +397,31 @@ class EnvironmentTest < Test::Unit::TestCase assert_equal 'default', Environment.new.theme end + should 'have a list of themes' do + env = Environment.default + t1 = mock + t2 = mock + + t1.stubs(:id).returns('theme_1') + t2.stubs(:id).returns('theme_2') + + Theme.expects(:system_themes).returns([t1, t2]) + env.themes = [t1, t2] + env.save! + assert_equal [t1, t2], Environment.default.themes + end + + should 'set themes to environment' do + env = Environment.default + t1 = mock + + t1.stubs(:id).returns('theme_1') + + env.themes = [t1] + env.save + assert_equal [t1.id], Environment.default.settings[:themes] + end + should 'create templates' do e = Environment.create!(:name => 'test_env') e.reload diff --git a/test/unit/theme_test.rb b/test/unit/theme_test.rb index 8a11a9b..2eada77 100644 --- a/test/unit/theme_test.rb +++ b/test/unit/theme_test.rb @@ -27,6 +27,12 @@ class ThemeTest < ActiveSupport::TestCase assert_equal 'the-id', Theme.new('the-id').name end + should 'save id on theme.yml' do + Theme.create('other_theme') + t = Theme.find('other_theme') + assert t.config['id'] + end + should 'create theme' do t = Theme.create('mytheme') assert_equal t, Theme.find('mytheme') @@ -149,12 +155,19 @@ class ThemeTest < ActiveSupport::TestCase assert_equivalent [ 'one.png', 'two.png' ], theme.image_files end - should 'be able to find public themes' do + should 'be able to find approved themes' do + Theme.stubs(:system_themes_dir).returns(TMP_THEMES_DIR) + profile = create_user('testinguser').person - t1 = Theme.create('mytheme', :owner => profile, :public => false) - t2 = Theme.create('mytheme2', :owner => profile, :public => true) + profile2 = create_user('testinguser2').person + t1 = Theme.new('mytheme1', :name => 'mytheme1', :owner => profile, :public => false); t1.save + t2 = Theme.new('mytheme2', :name => 'mytheme2', :owner => profile2, :public => true); t2.save + t3 = Theme.new('mytheme3', :name => 'mytheme3', :public => false); t3.save - assert_equal [t2], Theme.public_themes + [Theme.find(t2.id), Theme.find(t1.id)].each do |theme| + assert Theme.approved_themes(profile).include?(theme) + end + assert ! Theme.approved_themes(profile).include?(Theme.find(t3.id)) end should 'set theme to public' do -- libgit2 0.21.2