Commit b588a1a6ba577d9b473183b48716186df33b7c74
1 parent
cc4ec8ba
Exists in
master
and in
23 other branches
ActionItem789: making themes available only in its environment
* Listing environment themes and approved themes for selection * Adding settings 'themes' for the environment * Adding method approved_themes to theme.rb
Showing
6 changed files
with
89 additions
and
28 deletions
Show diff stats
app/controllers/my_profile/themes_controller.rb
| @@ -9,7 +9,7 @@ class ThemesController < MyProfileController | @@ -9,7 +9,7 @@ class ThemesController < MyProfileController | ||
| 9 | end | 9 | end |
| 10 | 10 | ||
| 11 | def index | 11 | def index |
| 12 | - @themes = Theme.system_themes + Theme.public_themes | 12 | + @themes = profile.environment.themes + Theme.approved_themes(profile) |
| 13 | @current_theme = profile.theme | 13 | @current_theme = profile.theme |
| 14 | 14 | ||
| 15 | @layout_templates = LayoutTemplate.all | 15 | @layout_templates = LayoutTemplate.all |
app/models/environment.rb
| @@ -315,6 +315,18 @@ class Environment < ActiveRecord::Base | @@ -315,6 +315,18 @@ class Environment < ActiveRecord::Base | ||
| 315 | self[:theme] || 'default' | 315 | self[:theme] || 'default' |
| 316 | end | 316 | end |
| 317 | 317 | ||
| 318 | + def themes | ||
| 319 | + if settings[:themes] | ||
| 320 | + Theme.system_themes.select { |theme| settings[:themes].include?(theme.id) } | ||
| 321 | + else | ||
| 322 | + [] | ||
| 323 | + end | ||
| 324 | + end | ||
| 325 | + | ||
| 326 | + def themes=(values) | ||
| 327 | + settings[:themes] = values.map(&:id) | ||
| 328 | + end | ||
| 329 | + | ||
| 318 | def layout_template | 330 | def layout_template |
| 319 | settings[:layout_template] || 'default' | 331 | settings[:layout_template] || 'default' |
| 320 | end | 332 | end |
app/models/theme.rb
| @@ -2,7 +2,7 @@ class Theme | @@ -2,7 +2,7 @@ class Theme | ||
| 2 | 2 | ||
| 3 | class << self | 3 | class << self |
| 4 | def system_themes | 4 | def system_themes |
| 5 | - Dir.glob(RAILS_ROOT + '/public/designs/themes/*').map do |item| | 5 | + Dir.glob(File.join(system_themes_dir, '*')).map do |item| |
| 6 | File.basename(item) | 6 | File.basename(item) |
| 7 | end.map do |item| | 7 | end.map do |item| |
| 8 | new(item) | 8 | new(item) |
| @@ -13,6 +13,10 @@ class Theme | @@ -13,6 +13,10 @@ class Theme | ||
| 13 | File.join(RAILS_ROOT, 'public', 'user_themes') | 13 | File.join(RAILS_ROOT, 'public', 'user_themes') |
| 14 | end | 14 | end |
| 15 | 15 | ||
| 16 | + def system_themes_dir | ||
| 17 | + File.join(RAILS_ROOT, 'public', 'designs', 'themes') | ||
| 18 | + end | ||
| 19 | + | ||
| 16 | def create(id, attributes = {}) | 20 | def create(id, attributes = {}) |
| 17 | if find(id) || system_themes.map(&:id).include?(id) | 21 | if find(id) || system_themes.map(&:id).include?(id) |
| 18 | raise DuplicatedIdentifier | 22 | raise DuplicatedIdentifier |
| @@ -37,12 +41,12 @@ class Theme | @@ -37,12 +41,12 @@ class Theme | ||
| 37 | end | 41 | end |
| 38 | end | 42 | end |
| 39 | 43 | ||
| 40 | - def public_themes | ||
| 41 | - Dir.glob(File.join(user_themes_dir, '*', 'theme.yml')).select do |desc| | ||
| 42 | - config = YAML.load_file(desc) | ||
| 43 | - config['public'] | 44 | + def approved_themes(owner) |
| 45 | + Dir.glob(File.join(system_themes_dir, '*')).select do |item| | ||
| 46 | + config = YAML.load_file(File.join(item, 'theme.yml')) | ||
| 47 | + (config['owner_type'] == owner.class.base_class.name) && (config['owner_id'] == owner.id) || config['public'] | ||
| 44 | end.map do |desc| | 48 | end.map do |desc| |
| 45 | - Theme.find(File.basename(File.dirname(desc))) | 49 | + new(File.basename(desc)) |
| 46 | end | 50 | end |
| 47 | end | 51 | end |
| 48 | 52 | ||
| @@ -59,6 +63,7 @@ class Theme | @@ -59,6 +63,7 @@ class Theme | ||
| 59 | attributes.each do |k,v| | 63 | attributes.each do |k,v| |
| 60 | self.send("#{k}=", v) | 64 | self.send("#{k}=", v) |
| 61 | end | 65 | end |
| 66 | + config['id'] = id | ||
| 62 | end | 67 | end |
| 63 | 68 | ||
| 64 | def name | 69 | def name |
test/functional/themes_controller_test.rb
| @@ -24,40 +24,46 @@ class ThemesControllerTest < Test::Unit::TestCase | @@ -24,40 +24,46 @@ class ThemesControllerTest < Test::Unit::TestCase | ||
| 24 | 24 | ||
| 25 | TMP_THEMES_DIR = RAILS_ROOT + '/test/tmp/themes_controller' | 25 | TMP_THEMES_DIR = RAILS_ROOT + '/test/tmp/themes_controller' |
| 26 | 26 | ||
| 27 | - should 'display list of themes for selection' do | ||
| 28 | - Theme.expects(:system_themes).returns([Theme.new('first'), Theme.new('second')]) | 27 | + should 'display themes that can be applied' do |
| 28 | + env = Environment.default | ||
| 29 | + Theme.stubs(:approved_themes).with(@profile).returns([Theme.new('t1', :name => 't1')]) | ||
| 30 | + t2 = Theme.create('t2') | ||
| 31 | + t3 = Theme.create('t3') | ||
| 32 | + env.themes = [t2] | ||
| 33 | + env.save | ||
| 34 | + | ||
| 35 | + Theme.stubs(:system_themes).returns([t2, t3]) | ||
| 29 | get :index, :profile => 'testinguser' | 36 | get :index, :profile => 'testinguser' |
| 30 | 37 | ||
| 31 | - %w[ first second ].each do |item| | 38 | + %w[ t1 t2 ].each do |item| |
| 32 | assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/#{item}" } | 39 | assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/#{item}" } |
| 33 | end | 40 | end |
| 34 | - end | ||
| 35 | - | ||
| 36 | - should 'not display themes for selection if it is not public' do | ||
| 37 | - Theme.create('first', :owner => profile, :public => true) | ||
| 38 | - Theme.create('second', :owner => profile, :public => false) | ||
| 39 | - get :index, :profile => 'testinguser' | ||
| 40 | 41 | ||
| 41 | - assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/first" } | ||
| 42 | - assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/second" } | 42 | + assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/t3" } |
| 43 | end | 43 | end |
| 44 | 44 | ||
| 45 | should 'highlight current theme' do | 45 | should 'highlight current theme' do |
| 46 | - profile.update_attributes(:theme => 'first') | ||
| 47 | - Theme.expects(:system_themes).returns([Theme.new('first'), Theme.new('second')]) | 46 | + env = Environment.default |
| 47 | + t1 = Theme.create('one', :name => 'one') | ||
| 48 | + t2 = Theme.create('two', :name => 'two') | ||
| 49 | + env.themes = [t1, t2] | ||
| 50 | + env.save | ||
| 51 | + | ||
| 52 | + Theme.stubs(:system_themes).returns([t1, t2]) | ||
| 53 | + profile.update_attributes(:theme => 'one') | ||
| 48 | get :index, :profile => 'testinguser' | 54 | get :index, :profile => 'testinguser' |
| 49 | 55 | ||
| 50 | assert_tag :attributes => { :class => 'selected theme' }, :descendant => { :content => /(current)/ } | 56 | assert_tag :attributes => { :class => 'selected theme' }, :descendant => { :content => /(current)/ } |
| 51 | - assert_no_tag :attributes => { :class => 'selected theme' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/first" } } | 57 | + assert_no_tag :attributes => { :class => 'selected theme' }, :descendant => { :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/one" } } |
| 52 | end | 58 | end |
| 53 | 59 | ||
| 54 | should 'display list of my themes for edition' do | 60 | should 'display list of my themes for edition' do |
| 55 | - Theme.create('first', :owner => profile) | ||
| 56 | - Theme.create('second', :owner => profile) | 61 | + Theme.create('three', :owner => profile) |
| 62 | + Theme.create('four', :owner => profile) | ||
| 57 | 63 | ||
| 58 | get :index, :profile => 'testinguser' | 64 | get :index, :profile => 'testinguser' |
| 59 | 65 | ||
| 60 | - %w[ first second ].each do |item| | 66 | + %w[ three four ].each do |item| |
| 61 | assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/edit/#{item}" } | 67 | assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/edit/#{item}" } |
| 62 | end | 68 | end |
| 63 | end | 69 | end |
test/unit/environment_test.rb
| @@ -397,6 +397,31 @@ class EnvironmentTest < Test::Unit::TestCase | @@ -397,6 +397,31 @@ class EnvironmentTest < Test::Unit::TestCase | ||
| 397 | assert_equal 'default', Environment.new.theme | 397 | assert_equal 'default', Environment.new.theme |
| 398 | end | 398 | end |
| 399 | 399 | ||
| 400 | + should 'have a list of themes' do | ||
| 401 | + env = Environment.default | ||
| 402 | + t1 = mock | ||
| 403 | + t2 = mock | ||
| 404 | + | ||
| 405 | + t1.stubs(:id).returns('theme_1') | ||
| 406 | + t2.stubs(:id).returns('theme_2') | ||
| 407 | + | ||
| 408 | + Theme.expects(:system_themes).returns([t1, t2]) | ||
| 409 | + env.themes = [t1, t2] | ||
| 410 | + env.save! | ||
| 411 | + assert_equal [t1, t2], Environment.default.themes | ||
| 412 | + end | ||
| 413 | + | ||
| 414 | + should 'set themes to environment' do | ||
| 415 | + env = Environment.default | ||
| 416 | + t1 = mock | ||
| 417 | + | ||
| 418 | + t1.stubs(:id).returns('theme_1') | ||
| 419 | + | ||
| 420 | + env.themes = [t1] | ||
| 421 | + env.save | ||
| 422 | + assert_equal [t1.id], Environment.default.settings[:themes] | ||
| 423 | + end | ||
| 424 | + | ||
| 400 | should 'create templates' do | 425 | should 'create templates' do |
| 401 | e = Environment.create!(:name => 'test_env') | 426 | e = Environment.create!(:name => 'test_env') |
| 402 | e.reload | 427 | e.reload |
test/unit/theme_test.rb
| @@ -27,6 +27,12 @@ class ThemeTest < ActiveSupport::TestCase | @@ -27,6 +27,12 @@ class ThemeTest < ActiveSupport::TestCase | ||
| 27 | assert_equal 'the-id', Theme.new('the-id').name | 27 | assert_equal 'the-id', Theme.new('the-id').name |
| 28 | end | 28 | end |
| 29 | 29 | ||
| 30 | + should 'save id on theme.yml' do | ||
| 31 | + Theme.create('other_theme') | ||
| 32 | + t = Theme.find('other_theme') | ||
| 33 | + assert t.config['id'] | ||
| 34 | + end | ||
| 35 | + | ||
| 30 | should 'create theme' do | 36 | should 'create theme' do |
| 31 | t = Theme.create('mytheme') | 37 | t = Theme.create('mytheme') |
| 32 | assert_equal t, Theme.find('mytheme') | 38 | assert_equal t, Theme.find('mytheme') |
| @@ -149,12 +155,19 @@ class ThemeTest < ActiveSupport::TestCase | @@ -149,12 +155,19 @@ class ThemeTest < ActiveSupport::TestCase | ||
| 149 | assert_equivalent [ 'one.png', 'two.png' ], theme.image_files | 155 | assert_equivalent [ 'one.png', 'two.png' ], theme.image_files |
| 150 | end | 156 | end |
| 151 | 157 | ||
| 152 | - should 'be able to find public themes' do | 158 | + should 'be able to find approved themes' do |
| 159 | + Theme.stubs(:system_themes_dir).returns(TMP_THEMES_DIR) | ||
| 160 | + | ||
| 153 | profile = create_user('testinguser').person | 161 | profile = create_user('testinguser').person |
| 154 | - t1 = Theme.create('mytheme', :owner => profile, :public => false) | ||
| 155 | - t2 = Theme.create('mytheme2', :owner => profile, :public => true) | 162 | + profile2 = create_user('testinguser2').person |
| 163 | + t1 = Theme.new('mytheme1', :name => 'mytheme1', :owner => profile, :public => false); t1.save | ||
| 164 | + t2 = Theme.new('mytheme2', :name => 'mytheme2', :owner => profile2, :public => true); t2.save | ||
| 165 | + t3 = Theme.new('mytheme3', :name => 'mytheme3', :public => false); t3.save | ||
| 156 | 166 | ||
| 157 | - assert_equal [t2], Theme.public_themes | 167 | + [Theme.find(t2.id), Theme.find(t1.id)].each do |theme| |
| 168 | + assert Theme.approved_themes(profile).include?(theme) | ||
| 169 | + end | ||
| 170 | + assert ! Theme.approved_themes(profile).include?(Theme.find(t3.id)) | ||
| 158 | end | 171 | end |
| 159 | 172 | ||
| 160 | should 'set theme to public' do | 173 | should 'set theme to public' do |