From 577d405e3bb4f42dc51724aa7a2e51bd3ed77210 Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Mon, 23 May 2016 12:01:23 -0300 Subject: [PATCH] create the possibility to change theme by url parameter --- app/controllers/application_controller.rb | 7 +++++++ app/controllers/my_profile/profile_themes_controller.rb | 4 ++-- app/helpers/application_helper.rb | 2 +- app/helpers/theme_loader_helper.rb | 12 +++++++----- app/models/environment.rb | 4 ++++ app/models/person_notifier.rb | 2 +- test/functional/application_controller_test.rb | 34 +++++++++++++++++++++++++++++++++- test/functional/profile_themes_controller_test.rb | 4 ++-- test/unit/application_helper_test.rb | 2 +- test/unit/content_viewer_helper_test.rb | 4 ++-- test/unit/environment_test.rb | 16 ++++++++++++++++ test/unit/theme_loader_helper_test.rb | 16 +++++++++++++--- 12 files changed, 89 insertions(+), 18 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 27c8132..c4301a9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,6 +13,13 @@ class ApplicationController < ActionController::Base before_filter :verify_members_whitelist, :if => [:private_environment?, :user] before_filter :redirect_to_current_user + before_filter :set_session_theme + def set_session_theme + if params[:theme] + session[:theme] = environment.theme_ids.include?(params[:theme]) ? params[:theme] : nil + end + end + def require_login_for_environment login_required end diff --git a/app/controllers/my_profile/profile_themes_controller.rb b/app/controllers/my_profile/profile_themes_controller.rb index ccb0761..be0e4bd 100644 --- a/app/controllers/my_profile/profile_themes_controller.rb +++ b/app/controllers/my_profile/profile_themes_controller.rb @@ -63,12 +63,12 @@ class ProfileThemesController < ThemesController end def start_test - session[:theme] = params[:id] + session[:user_theme] = params[:id] redirect_to :controller => 'content_viewer', :profile => profile.identifier, :action => 'view_page' end def stop_test - session[:theme] = nil + session[:user_theme] = nil redirect_to :action => 'index' end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e7fc154..16e1dc7 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -344,7 +344,7 @@ module ApplicationHelper end def is_testing_theme - !controller.session[:theme].nil? + !controller.session[:user_theme].nil? end def theme_owner diff --git a/app/helpers/theme_loader_helper.rb b/app/helpers/theme_loader_helper.rb index 6304640..10d8014 100644 --- a/app/helpers/theme_loader_helper.rb +++ b/app/helpers/theme_loader_helper.rb @@ -2,8 +2,8 @@ module ThemeLoaderHelper def current_theme @current_theme ||= begin - if session[:theme] - session[:theme] + if session[:user_theme] + session[:user_theme] else # utility for developers: set the theme to 'random' in development mode and # you will get a different theme every request. This is interesting for @@ -11,8 +11,8 @@ module ThemeLoaderHelper if Rails.env.development? && environment.theme == 'random' @random_theme ||= Dir.glob('public/designs/themes/*').map { |f| File.basename(f) }.rand @random_theme - elsif Rails.env.development? && respond_to?(:params) && params[:theme] && File.exists?(Rails.root.join('public/designs/themes', params[:theme])) - params[:theme] + elsif Rails.env.development? && respond_to?(:params) && params[:user_theme] && File.exists?(Rails.root.join('public/designs/themes', params[:user_theme])) + params[:user_theme] else if profile && !profile.theme.nil? profile.theme @@ -34,8 +34,10 @@ module ThemeLoaderHelper end def theme_path - if session[:theme] + if session[:user_theme] '/user_themes/' + current_theme + elsif session[:theme] + '/designs/themes/' + session[:theme] else '/designs/themes/' + current_theme end diff --git a/app/models/environment.rb b/app/models/environment.rb index 6b7365b..6d54e6d 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -750,6 +750,10 @@ class Environment < ApplicationRecord end end + def theme_ids + settings[:themes] || [] + end + def themes=(values) settings[:themes] = values end diff --git a/app/models/person_notifier.rb b/app/models/person_notifier.rb index ba75c89..cf7fa9d 100644 --- a/app/models/person_notifier.rb +++ b/app/models/person_notifier.rb @@ -82,7 +82,7 @@ class PersonNotifier helper ActionTrackerHelper def session - {:theme => nil} + {:user_theme => nil} end def content_summary(person, notifications, tasks) diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index c167839..d9d8e9a 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -224,7 +224,7 @@ class ApplicationControllerTest < ActionController::TestCase end should 'display theme test panel when testing theme' do - @request.session[:theme] = 'my-test-theme' + @request.session[:user_theme] = 'my-test-theme' theme = mock profile = mock theme.expects(:owner).returns(profile).at_least_once @@ -576,4 +576,36 @@ class ApplicationControllerTest < ActionController::TestCase assert_redirected_to :controller => 'test', :action => 'index', :profile => profile.identifier end + should 'set session theme if a params theme is passed as parameter' do + current_theme = 'my-test-theme' + environment = Environment.default + Theme.stubs(:system_themes).returns([Theme.new(current_theme)]) + environment.themes = [current_theme] + environment.save! + assert_nil @request.session[:theme] + get :index, :theme => current_theme + assert_equal current_theme, @request.session[:theme] + end + + should 'set session theme only in environment available themes' do + environment = Environment.default + assert_nil @request.session[:theme] + environment.stubs(:theme_ids).returns(['another_theme']) + get :index, :theme => 'my-test-theme' + assert_nil @request.session[:theme] + end + + should 'unset session theme if not environment available themes is defined' do + environment = Environment.default + current_theme = 'my-test-theme' + Theme.stubs(:system_themes).returns([Theme.new(current_theme)]) + environment.themes = [current_theme] + environment.save! + get :index, :theme => current_theme + assert_equal current_theme, @request.session[:theme] + + get :index, :theme => 'another_theme' + assert_nil @request.session[:theme] + end + end diff --git a/test/functional/profile_themes_controller_test.rb b/test/functional/profile_themes_controller_test.rb index 5c52361..e66a662 100644 --- a/test/functional/profile_themes_controller_test.rb +++ b/test/functional/profile_themes_controller_test.rb @@ -231,7 +231,7 @@ class ProfileThemesControllerTest < ActionController::TestCase theme = Theme.create('theme-under-test', :owner => profile) post :start_test, :profile => 'testinguser', :id => 'theme-under-test' - assert_equal 'theme-under-test', session[:theme] + assert_equal 'theme-under-test', session[:user_theme] assert_redirected_to :controller => 'content_viewer', :profile => 'testinguser', :action => 'view_page' end @@ -239,7 +239,7 @@ class ProfileThemesControllerTest < ActionController::TestCase theme = Theme.create('theme-under-test', :owner => profile) post :stop_test, :profile => 'testinguser', :id => 'theme-under-test' - assert_nil session[:theme] + assert_nil session[:user_theme] assert_redirected_to :action => 'index' end diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index 34f2e3f..736a036 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -466,7 +466,7 @@ class ApplicationHelperTest < ActionView::TestCase should 'use theme passed via param when in development mode' do stubs(:environment).returns(build(Environment, :theme => 'environment-theme')) Rails.env.stubs(:development?).returns(true) - self.stubs(:params).returns({:theme => 'skyblue'}) + self.stubs(:params).returns({:user_theme => 'skyblue'}) assert_equal 'skyblue', current_theme end diff --git a/test/unit/content_viewer_helper_test.rb b/test/unit/content_viewer_helper_test.rb index 0ca89c9..370b856 100644 --- a/test/unit/content_viewer_helper_test.rb +++ b/test/unit/content_viewer_helper_test.rb @@ -101,14 +101,14 @@ class ContentViewerHelperTest < ActionView::TestCase end should 'theme provides addthis custom icon' do - stubs(:session).returns({:theme => 'base'}) + stubs(:session).returns({:user_theme => 'base'}) File.expects(:exists?).with(anything).returns(true) Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') assert_match 'addthis.gif', addthis_image_tag end should 'use default addthis icon if theme has no addthis.gif image' do - stubs(:session).returns({:theme => 'base'}) + stubs(:session).returns({:user_theme => 'base'}) File.expects(:exists?).with(anything).returns(false) Environment.any_instance.stubs(:default_hostname).returns('noosfero.org') assert_match 'bt-bookmark.gif', addthis_image_tag diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index c4dc1b8..216fe1e 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -457,6 +457,22 @@ class EnvironmentTest < ActiveSupport::TestCase assert_equal ['new-theme', 'other-theme'], Environment.default.themes.map(&:id) end + should 'return the theme ids' do + env = Environment.default + t1 = 'theme_1' + t2 = 'theme_2' + Theme.stubs(:system_themes).returns([Theme.new(t1), Theme.new(t2)]) + env.themes = [t1, t2] + env.save! + assert_equivalent [t1, t2], Environment.default.theme_ids + end + should 'theme_ids be an empty array if there is no settings themes defined' do + env = Environment.default + env.settings[:themes] = nil + env.save! + assert Environment.default.theme_ids.empty? + end + should 'create templates' do e = create(Environment, :name => 'test_env') e.reload diff --git a/test/unit/theme_loader_helper_test.rb b/test/unit/theme_loader_helper_test.rb index 917cd62..0aace5f 100644 --- a/test/unit/theme_loader_helper_test.rb +++ b/test/unit/theme_loader_helper_test.rb @@ -20,7 +20,7 @@ class ThemeLoaderHelperTest < ActionView::TestCase end should 'override theme with testing theme from session' do - stubs(:session).returns(:theme => 'theme-under-test') + stubs(:session).returns(:user_theme => 'theme-under-test') assert_equal 'theme-under-test', current_theme end @@ -30,7 +30,17 @@ class ThemeLoaderHelperTest < ActionView::TestCase end should 'point to user theme path when testing theme' do - stubs(:session).returns({:theme => 'theme-under-test'}) + stubs(:session).returns({:user_theme => 'theme-under-test'}) assert_equal '/user_themes/theme-under-test', theme_path end -end \ No newline at end of file + + should 'point to session theme is defined' do + session = mock + stubs(:session).returns(session) + my_session_theme = 'session_theme' + session.stubs(:[]).with(:user_theme).returns(nil) + session.stubs(:[]).with(:theme).returns(my_session_theme) + assert_equal '/designs/themes/' + my_session_theme, theme_path + end + +end -- libgit2 0.21.2