diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f0ab8db..fb97c43 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -153,7 +153,7 @@ module ApplicationHelper # should be a current profile (i.e. while viewing some profile's pages, or the # profile info, etc), because if there is no profile an exception is thrown. def profile - @controller.send(:profile) || raise("There is no current profile") + @controller.send(:profile) end def category_color @@ -328,9 +328,13 @@ module ApplicationHelper result << '/stylesheets/' << name << '.css' end - # FIXME do not hardcode 'default' like this def current_theme - 'default' + p = profile + if p + p.theme + else + @environment.theme + end end # generates a image tag for the profile. diff --git a/app/models/environment.rb b/app/models/environment.rb index 858fde9..ddde374 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -241,4 +241,8 @@ class Environment < ActiveRecord::Base has_many :events, :through => :profiles, :source => :articles, :class_name => 'Event' + def theme + self[:theme] || 'default' + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index c1ec980..b8c00b6 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -399,4 +399,8 @@ class Profile < ActiveRecord::Base self[:custom_footer] || environment.custom_footer end + def theme + self[:theme] || environment.theme + end + end diff --git a/db/migrate/049_add_theme_attribute.rb b/db/migrate/049_add_theme_attribute.rb new file mode 100644 index 0000000..488e679 --- /dev/null +++ b/db/migrate/049_add_theme_attribute.rb @@ -0,0 +1,15 @@ +class AddThemeAttribute < ActiveRecord::Migration + TABLE = [ :profiles, :environments ] + + def self.up + TABLE.each do |table| + add_column table, :theme, :string + end + end + + def self.down + TABLE.each do |table| + remove_column table, :theme + end + end +end diff --git a/db/schema.rb b/db/schema.rb index fa5a0a6..98ff7c2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 48) do +ActiveRecord::Schema.define(:version => 49) do create_table "article_versions", :force => true do |t| t.integer "article_id" @@ -145,6 +145,7 @@ ActiveRecord::Schema.define(:version => 48) do t.text "design_data" t.text "custom_header" t.text "custom_footer" + t.string "theme" end create_table "favorite_enteprises_people", :id => false, :force => true do |t| @@ -217,6 +218,7 @@ ActiveRecord::Schema.define(:version => 48) do t.string "nickname", :limit => 16 t.text "custom_header" t.text "custom_footer" + t.string "theme" end add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id" diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index afa6a90..3469de2 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -64,11 +64,8 @@ class ApplicationHelperTest < Test::Unit::TestCase assert_same result, link_to_category(cat) end - should 'get current theme' do - assert_equal 'default', current_theme() - end - should 'nil theme option when no exists theme' do + stubs(:current_theme).returns('something-very-unlikely') File.expects(:exists?).returns(false) assert_nil theme_option() end @@ -84,6 +81,7 @@ class ApplicationHelperTest < Test::Unit::TestCase end should 'nil javascript theme when no exists theme' do + stubs(:current_theme).returns('something-very-unlikely') File.expects(:exists?).returns(false) assert_nil theme_javascript end @@ -138,6 +136,21 @@ class ApplicationHelperTest < Test::Unit::TestCase #assert_no_match /parent category/, result end + should 'get theme from environment by default' do + @environment = mock + @environment.stubs(:theme).returns('my-environment-theme') + stubs(:profile).returns(nil) + assert_equal 'my-environment-theme', current_theme + end + + should 'get theme from profile when profile is present' do + profile = mock + profile.stubs(:theme).returns('my-profile-theme') + stubs(:profile).returns(profile) + assert_equal 'my-profile-theme', current_theme + end + + protected def content_tag(tag, content, options = {}) diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 5336449..0e14e66 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -355,4 +355,12 @@ class EnvironmentTest < Test::Unit::TestCase assert_equal 'my footer', Environment.new(:custom_footer => "my footer").custom_footer end + should 'provide theme' do + assert_equal 'my-custom-theme', Environment.new(:theme => 'my-custom-theme').theme + end + + should 'give default theme' do + assert_equal 'default', Environment.new.theme + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index ce40629..eeaf3d2 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -771,6 +771,20 @@ class ProfileTest < Test::Unit::TestCase assert_equal 'environment footer', profile.custom_footer end + should 'store theme' do + p = Profile.new(:theme => 'my-shiny-theme') + assert_equal 'my-shiny-theme', p.theme + end + + should 'delegate theme selection to environment by default' do + p = Profile.new + env = mock + p.stubs(:environment).returns(env) + env.expects(:theme).returns('environment-stored-theme') + + assert_equal 'environment-stored-theme', p.theme + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2