Commit b588a1a6ba577d9b473183b48716186df33b7c74

Authored by Daniela Feitosa
1 parent cc4ec8ba

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
app/controllers/my_profile/themes_controller.rb
... ... @@ -9,7 +9,7 @@ class ThemesController < MyProfileController
9 9 end
10 10  
11 11 def index
12   - @themes = Theme.system_themes + Theme.public_themes
  12 + @themes = profile.environment.themes + Theme.approved_themes(profile)
13 13 @current_theme = profile.theme
14 14  
15 15 @layout_templates = LayoutTemplate.all
... ...
app/models/environment.rb
... ... @@ -315,6 +315,18 @@ class Environment < ActiveRecord::Base
315 315 self[:theme] || 'default'
316 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 330 def layout_template
319 331 settings[:layout_template] || 'default'
320 332 end
... ...
app/models/theme.rb
... ... @@ -2,7 +2,7 @@ class Theme
2 2  
3 3 class << self
4 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 6 File.basename(item)
7 7 end.map do |item|
8 8 new(item)
... ... @@ -13,6 +13,10 @@ class Theme
13 13 File.join(RAILS_ROOT, 'public', 'user_themes')
14 14 end
15 15  
  16 + def system_themes_dir
  17 + File.join(RAILS_ROOT, 'public', 'designs', 'themes')
  18 + end
  19 +
16 20 def create(id, attributes = {})
17 21 if find(id) || system_themes.map(&:id).include?(id)
18 22 raise DuplicatedIdentifier
... ... @@ -37,12 +41,12 @@ class Theme
37 41 end
38 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 48 end.map do |desc|
45   - Theme.find(File.basename(File.dirname(desc)))
  49 + new(File.basename(desc))
46 50 end
47 51 end
48 52  
... ... @@ -59,6 +63,7 @@ class Theme
59 63 attributes.each do |k,v|
60 64 self.send("#{k}=", v)
61 65 end
  66 + config['id'] = id
62 67 end
63 68  
64 69 def name
... ...
test/functional/themes_controller_test.rb
... ... @@ -24,40 +24,46 @@ class ThemesControllerTest &lt; Test::Unit::TestCase
24 24  
25 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 36 get :index, :profile => 'testinguser'
30 37  
31   - %w[ first second ].each do |item|
  38 + %w[ t1 t2 ].each do |item|
32 39 assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/set/#{item}" }
33 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 43 end
44 44  
45 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 54 get :index, :profile => 'testinguser'
49 55  
50 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 58 end
53 59  
54 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 64 get :index, :profile => 'testinguser'
59 65  
60   - %w[ first second ].each do |item|
  66 + %w[ three four ].each do |item|
61 67 assert_tag :tag => 'a', :attributes => { :href => "/myprofile/testinguser/themes/edit/#{item}" }
62 68 end
63 69 end
... ...
test/unit/environment_test.rb
... ... @@ -397,6 +397,31 @@ class EnvironmentTest &lt; Test::Unit::TestCase
397 397 assert_equal 'default', Environment.new.theme
398 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 425 should 'create templates' do
401 426 e = Environment.create!(:name => 'test_env')
402 427 e.reload
... ...
test/unit/theme_test.rb
... ... @@ -27,6 +27,12 @@ class ThemeTest &lt; ActiveSupport::TestCase
27 27 assert_equal 'the-id', Theme.new('the-id').name
28 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 36 should 'create theme' do
31 37 t = Theme.create('mytheme')
32 38 assert_equal t, Theme.find('mytheme')
... ... @@ -149,12 +155,19 @@ class ThemeTest &lt; ActiveSupport::TestCase
149 155 assert_equivalent [ 'one.png', 'two.png' ], theme.image_files
150 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 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 171 end
159 172  
160 173 should 'set theme to public' do
... ...