Commit df0c5164d0ba0cbd0ecb40486495eff6bfcc5209

Authored by Victor Costa
2 parents 02848296 577d405e

Merge branch 'session_theme' into 'master'

create the possibility to change theme by url parameter



See merge request !936
app/controllers/application_controller.rb
... ... @@ -13,6 +13,13 @@ class ApplicationController < ActionController::Base
13 13 before_filter :verify_members_whitelist, :if => [:private_environment?, :user]
14 14 before_filter :redirect_to_current_user
15 15  
  16 + before_filter :set_session_theme
  17 + def set_session_theme
  18 + if params[:theme]
  19 + session[:theme] = environment.theme_ids.include?(params[:theme]) ? params[:theme] : nil
  20 + end
  21 + end
  22 +
16 23 def require_login_for_environment
17 24 login_required
18 25 end
... ...
app/controllers/my_profile/profile_themes_controller.rb
... ... @@ -63,12 +63,12 @@ class ProfileThemesController < ThemesController
63 63 end
64 64  
65 65 def start_test
66   - session[:theme] = params[:id]
  66 + session[:user_theme] = params[:id]
67 67 redirect_to :controller => 'content_viewer', :profile => profile.identifier, :action => 'view_page'
68 68 end
69 69  
70 70 def stop_test
71   - session[:theme] = nil
  71 + session[:user_theme] = nil
72 72 redirect_to :action => 'index'
73 73 end
74 74  
... ...
app/helpers/application_helper.rb
... ... @@ -344,7 +344,7 @@ module ApplicationHelper
344 344 end
345 345  
346 346 def is_testing_theme
347   - !controller.session[:theme].nil?
  347 + !controller.session[:user_theme].nil?
348 348 end
349 349  
350 350 def theme_owner
... ...
app/helpers/theme_loader_helper.rb
... ... @@ -2,8 +2,8 @@ module ThemeLoaderHelper
2 2 def current_theme
3 3 @current_theme ||=
4 4 begin
5   - if session[:theme]
6   - session[:theme]
  5 + if session[:user_theme]
  6 + session[:user_theme]
7 7 else
8 8 # utility for developers: set the theme to 'random' in development mode and
9 9 # you will get a different theme every request. This is interesting for
... ... @@ -11,8 +11,8 @@ module ThemeLoaderHelper
11 11 if Rails.env.development? && environment.theme == 'random'
12 12 @random_theme ||= Dir.glob('public/designs/themes/*').map { |f| File.basename(f) }.rand
13 13 @random_theme
14   - elsif Rails.env.development? && respond_to?(:params) && params[:theme] && File.exists?(Rails.root.join('public/designs/themes', params[:theme]))
15   - params[:theme]
  14 + elsif Rails.env.development? && respond_to?(:params) && params[:user_theme] && File.exists?(Rails.root.join('public/designs/themes', params[:user_theme]))
  15 + params[:user_theme]
16 16 else
17 17 if profile && !profile.theme.nil?
18 18 profile.theme
... ... @@ -34,8 +34,10 @@ module ThemeLoaderHelper
34 34 end
35 35  
36 36 def theme_path
37   - if session[:theme]
  37 + if session[:user_theme]
38 38 '/user_themes/' + current_theme
  39 + elsif session[:theme]
  40 + '/designs/themes/' + session[:theme]
39 41 else
40 42 '/designs/themes/' + current_theme
41 43 end
... ...
app/models/environment.rb
... ... @@ -750,6 +750,10 @@ class Environment < ApplicationRecord
750 750 end
751 751 end
752 752  
  753 + def theme_ids
  754 + settings[:themes] || []
  755 + end
  756 +
753 757 def themes=(values)
754 758 settings[:themes] = values
755 759 end
... ...
app/models/person_notifier.rb
... ... @@ -82,7 +82,7 @@ class PersonNotifier
82 82 helper ActionTrackerHelper
83 83  
84 84 def session
85   - {:theme => nil}
  85 + {:user_theme => nil}
86 86 end
87 87  
88 88 def content_summary(person, notifications, tasks)
... ...
test/functional/application_controller_test.rb
... ... @@ -224,7 +224,7 @@ class ApplicationControllerTest < ActionController::TestCase
224 224 end
225 225  
226 226 should 'display theme test panel when testing theme' do
227   - @request.session[:theme] = 'my-test-theme'
  227 + @request.session[:user_theme] = 'my-test-theme'
228 228 theme = mock
229 229 profile = mock
230 230 theme.expects(:owner).returns(profile).at_least_once
... ... @@ -576,4 +576,36 @@ class ApplicationControllerTest < ActionController::TestCase
576 576 assert_redirected_to :controller => 'test', :action => 'index', :profile => profile.identifier
577 577 end
578 578  
  579 + should 'set session theme if a params theme is passed as parameter' do
  580 + current_theme = 'my-test-theme'
  581 + environment = Environment.default
  582 + Theme.stubs(:system_themes).returns([Theme.new(current_theme)])
  583 + environment.themes = [current_theme]
  584 + environment.save!
  585 + assert_nil @request.session[:theme]
  586 + get :index, :theme => current_theme
  587 + assert_equal current_theme, @request.session[:theme]
  588 + end
  589 +
  590 + should 'set session theme only in environment available themes' do
  591 + environment = Environment.default
  592 + assert_nil @request.session[:theme]
  593 + environment.stubs(:theme_ids).returns(['another_theme'])
  594 + get :index, :theme => 'my-test-theme'
  595 + assert_nil @request.session[:theme]
  596 + end
  597 +
  598 + should 'unset session theme if not environment available themes is defined' do
  599 + environment = Environment.default
  600 + current_theme = 'my-test-theme'
  601 + Theme.stubs(:system_themes).returns([Theme.new(current_theme)])
  602 + environment.themes = [current_theme]
  603 + environment.save!
  604 + get :index, :theme => current_theme
  605 + assert_equal current_theme, @request.session[:theme]
  606 +
  607 + get :index, :theme => 'another_theme'
  608 + assert_nil @request.session[:theme]
  609 + end
  610 +
579 611 end
... ...
test/functional/profile_themes_controller_test.rb
... ... @@ -231,7 +231,7 @@ class ProfileThemesControllerTest < ActionController::TestCase
231 231 theme = Theme.create('theme-under-test', :owner => profile)
232 232 post :start_test, :profile => 'testinguser', :id => 'theme-under-test'
233 233  
234   - assert_equal 'theme-under-test', session[:theme]
  234 + assert_equal 'theme-under-test', session[:user_theme]
235 235 assert_redirected_to :controller => 'content_viewer', :profile => 'testinguser', :action => 'view_page'
236 236 end
237 237  
... ... @@ -239,7 +239,7 @@ class ProfileThemesControllerTest < ActionController::TestCase
239 239 theme = Theme.create('theme-under-test', :owner => profile)
240 240 post :stop_test, :profile => 'testinguser', :id => 'theme-under-test'
241 241  
242   - assert_nil session[:theme]
  242 + assert_nil session[:user_theme]
243 243 assert_redirected_to :action => 'index'
244 244 end
245 245  
... ...
test/unit/application_helper_test.rb
... ... @@ -466,7 +466,7 @@ class ApplicationHelperTest < ActionView::TestCase
466 466 should 'use theme passed via param when in development mode' do
467 467 stubs(:environment).returns(build(Environment, :theme => 'environment-theme'))
468 468 Rails.env.stubs(:development?).returns(true)
469   - self.stubs(:params).returns({:theme => 'skyblue'})
  469 + self.stubs(:params).returns({:user_theme => 'skyblue'})
470 470 assert_equal 'skyblue', current_theme
471 471 end
472 472  
... ...
test/unit/content_viewer_helper_test.rb
... ... @@ -101,14 +101,14 @@ class ContentViewerHelperTest < ActionView::TestCase
101 101 end
102 102  
103 103 should 'theme provides addthis custom icon' do
104   - stubs(:session).returns({:theme => 'base'})
  104 + stubs(:session).returns({:user_theme => 'base'})
105 105 File.expects(:exists?).with(anything).returns(true)
106 106 Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
107 107 assert_match 'addthis.gif', addthis_image_tag
108 108 end
109 109  
110 110 should 'use default addthis icon if theme has no addthis.gif image' do
111   - stubs(:session).returns({:theme => 'base'})
  111 + stubs(:session).returns({:user_theme => 'base'})
112 112 File.expects(:exists?).with(anything).returns(false)
113 113 Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
114 114 assert_match 'bt-bookmark.gif', addthis_image_tag
... ...
test/unit/environment_test.rb
... ... @@ -457,6 +457,22 @@ class EnvironmentTest < ActiveSupport::TestCase
457 457 assert_equal ['new-theme', 'other-theme'], Environment.default.themes.map(&:id)
458 458 end
459 459  
  460 + should 'return the theme ids' do
  461 + env = Environment.default
  462 + t1 = 'theme_1'
  463 + t2 = 'theme_2'
  464 + Theme.stubs(:system_themes).returns([Theme.new(t1), Theme.new(t2)])
  465 + env.themes = [t1, t2]
  466 + env.save!
  467 + assert_equivalent [t1, t2], Environment.default.theme_ids
  468 + end
  469 + should 'theme_ids be an empty array if there is no settings themes defined' do
  470 + env = Environment.default
  471 + env.settings[:themes] = nil
  472 + env.save!
  473 + assert Environment.default.theme_ids.empty?
  474 + end
  475 +
460 476 should 'create templates' do
461 477 e = create(Environment, :name => 'test_env')
462 478 e.reload
... ...
test/unit/theme_loader_helper_test.rb
... ... @@ -20,7 +20,7 @@ class ThemeLoaderHelperTest < ActionView::TestCase
20 20 end
21 21  
22 22 should 'override theme with testing theme from session' do
23   - stubs(:session).returns(:theme => 'theme-under-test')
  23 + stubs(:session).returns(:user_theme => 'theme-under-test')
24 24 assert_equal 'theme-under-test', current_theme
25 25 end
26 26  
... ... @@ -30,7 +30,17 @@ class ThemeLoaderHelperTest < ActionView::TestCase
30 30 end
31 31  
32 32 should 'point to user theme path when testing theme' do
33   - stubs(:session).returns({:theme => 'theme-under-test'})
  33 + stubs(:session).returns({:user_theme => 'theme-under-test'})
34 34 assert_equal '/user_themes/theme-under-test', theme_path
35 35 end
36   -end
37 36 \ No newline at end of file
  37 +
  38 + should 'point to session theme is defined' do
  39 + session = mock
  40 + stubs(:session).returns(session)
  41 + my_session_theme = 'session_theme'
  42 + session.stubs(:[]).with(:user_theme).returns(nil)
  43 + session.stubs(:[]).with(:theme).returns(my_session_theme)
  44 + assert_equal '/designs/themes/' + my_session_theme, theme_path
  45 + end
  46 +
  47 +end
... ...