Commit 229119feb3a231d1b7bd9d2da2bbed6f09d264c0
1 parent
6f1ddaea
Exists in
master
and in
29 other branches
ActionItem122: implementing storage of theme info
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2390 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
8 changed files
with
72 additions
and
8 deletions
Show diff stats
app/helpers/application_helper.rb
@@ -153,7 +153,7 @@ module ApplicationHelper | @@ -153,7 +153,7 @@ module ApplicationHelper | ||
153 | # should be a current profile (i.e. while viewing some profile's pages, or the | 153 | # should be a current profile (i.e. while viewing some profile's pages, or the |
154 | # profile info, etc), because if there is no profile an exception is thrown. | 154 | # profile info, etc), because if there is no profile an exception is thrown. |
155 | def profile | 155 | def profile |
156 | - @controller.send(:profile) || raise("There is no current profile") | 156 | + @controller.send(:profile) |
157 | end | 157 | end |
158 | 158 | ||
159 | def category_color | 159 | def category_color |
@@ -328,9 +328,13 @@ module ApplicationHelper | @@ -328,9 +328,13 @@ module ApplicationHelper | ||
328 | result << '/stylesheets/' << name << '.css' | 328 | result << '/stylesheets/' << name << '.css' |
329 | end | 329 | end |
330 | 330 | ||
331 | - # FIXME do not hardcode 'default' like this | ||
332 | def current_theme | 331 | def current_theme |
333 | - 'default' | 332 | + p = profile |
333 | + if p | ||
334 | + p.theme | ||
335 | + else | ||
336 | + @environment.theme | ||
337 | + end | ||
334 | end | 338 | end |
335 | 339 | ||
336 | # generates a image tag for the profile. | 340 | # generates a image tag for the profile. |
app/models/environment.rb
@@ -241,4 +241,8 @@ class Environment < ActiveRecord::Base | @@ -241,4 +241,8 @@ class Environment < ActiveRecord::Base | ||
241 | 241 | ||
242 | has_many :events, :through => :profiles, :source => :articles, :class_name => 'Event' | 242 | has_many :events, :through => :profiles, :source => :articles, :class_name => 'Event' |
243 | 243 | ||
244 | + def theme | ||
245 | + self[:theme] || 'default' | ||
246 | + end | ||
247 | + | ||
244 | end | 248 | end |
app/models/profile.rb
@@ -399,4 +399,8 @@ class Profile < ActiveRecord::Base | @@ -399,4 +399,8 @@ class Profile < ActiveRecord::Base | ||
399 | self[:custom_footer] || environment.custom_footer | 399 | self[:custom_footer] || environment.custom_footer |
400 | end | 400 | end |
401 | 401 | ||
402 | + def theme | ||
403 | + self[:theme] || environment.theme | ||
404 | + end | ||
405 | + | ||
402 | end | 406 | end |
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +class AddThemeAttribute < ActiveRecord::Migration | ||
2 | + TABLE = [ :profiles, :environments ] | ||
3 | + | ||
4 | + def self.up | ||
5 | + TABLE.each do |table| | ||
6 | + add_column table, :theme, :string | ||
7 | + end | ||
8 | + end | ||
9 | + | ||
10 | + def self.down | ||
11 | + TABLE.each do |table| | ||
12 | + remove_column table, :theme | ||
13 | + end | ||
14 | + end | ||
15 | +end |
db/schema.rb
@@ -9,7 +9,7 @@ | @@ -9,7 +9,7 @@ | ||
9 | # | 9 | # |
10 | # It's strongly recommended to check this file into your version control system. | 10 | # It's strongly recommended to check this file into your version control system. |
11 | 11 | ||
12 | -ActiveRecord::Schema.define(:version => 48) do | 12 | +ActiveRecord::Schema.define(:version => 49) do |
13 | 13 | ||
14 | create_table "article_versions", :force => true do |t| | 14 | create_table "article_versions", :force => true do |t| |
15 | t.integer "article_id" | 15 | t.integer "article_id" |
@@ -145,6 +145,7 @@ ActiveRecord::Schema.define(:version => 48) do | @@ -145,6 +145,7 @@ ActiveRecord::Schema.define(:version => 48) do | ||
145 | t.text "design_data" | 145 | t.text "design_data" |
146 | t.text "custom_header" | 146 | t.text "custom_header" |
147 | t.text "custom_footer" | 147 | t.text "custom_footer" |
148 | + t.string "theme" | ||
148 | end | 149 | end |
149 | 150 | ||
150 | create_table "favorite_enteprises_people", :id => false, :force => true do |t| | 151 | create_table "favorite_enteprises_people", :id => false, :force => true do |t| |
@@ -217,6 +218,7 @@ ActiveRecord::Schema.define(:version => 48) do | @@ -217,6 +218,7 @@ ActiveRecord::Schema.define(:version => 48) do | ||
217 | t.string "nickname", :limit => 16 | 218 | t.string "nickname", :limit => 16 |
218 | t.text "custom_header" | 219 | t.text "custom_header" |
219 | t.text "custom_footer" | 220 | t.text "custom_footer" |
221 | + t.string "theme" | ||
220 | end | 222 | end |
221 | 223 | ||
222 | add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id" | 224 | add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id" |
test/unit/application_helper_test.rb
@@ -64,11 +64,8 @@ class ApplicationHelperTest < Test::Unit::TestCase | @@ -64,11 +64,8 @@ class ApplicationHelperTest < Test::Unit::TestCase | ||
64 | assert_same result, link_to_category(cat) | 64 | assert_same result, link_to_category(cat) |
65 | end | 65 | end |
66 | 66 | ||
67 | - should 'get current theme' do | ||
68 | - assert_equal 'default', current_theme() | ||
69 | - end | ||
70 | - | ||
71 | should 'nil theme option when no exists theme' do | 67 | should 'nil theme option when no exists theme' do |
68 | + stubs(:current_theme).returns('something-very-unlikely') | ||
72 | File.expects(:exists?).returns(false) | 69 | File.expects(:exists?).returns(false) |
73 | assert_nil theme_option() | 70 | assert_nil theme_option() |
74 | end | 71 | end |
@@ -84,6 +81,7 @@ class ApplicationHelperTest < Test::Unit::TestCase | @@ -84,6 +81,7 @@ class ApplicationHelperTest < Test::Unit::TestCase | ||
84 | end | 81 | end |
85 | 82 | ||
86 | should 'nil javascript theme when no exists theme' do | 83 | should 'nil javascript theme when no exists theme' do |
84 | + stubs(:current_theme).returns('something-very-unlikely') | ||
87 | File.expects(:exists?).returns(false) | 85 | File.expects(:exists?).returns(false) |
88 | assert_nil theme_javascript | 86 | assert_nil theme_javascript |
89 | end | 87 | end |
@@ -138,6 +136,21 @@ class ApplicationHelperTest < Test::Unit::TestCase | @@ -138,6 +136,21 @@ class ApplicationHelperTest < Test::Unit::TestCase | ||
138 | #assert_no_match /parent category/, result | 136 | #assert_no_match /parent category/, result |
139 | end | 137 | end |
140 | 138 | ||
139 | + should 'get theme from environment by default' do | ||
140 | + @environment = mock | ||
141 | + @environment.stubs(:theme).returns('my-environment-theme') | ||
142 | + stubs(:profile).returns(nil) | ||
143 | + assert_equal 'my-environment-theme', current_theme | ||
144 | + end | ||
145 | + | ||
146 | + should 'get theme from profile when profile is present' do | ||
147 | + profile = mock | ||
148 | + profile.stubs(:theme).returns('my-profile-theme') | ||
149 | + stubs(:profile).returns(profile) | ||
150 | + assert_equal 'my-profile-theme', current_theme | ||
151 | + end | ||
152 | + | ||
153 | + | ||
141 | protected | 154 | protected |
142 | 155 | ||
143 | def content_tag(tag, content, options = {}) | 156 | def content_tag(tag, content, options = {}) |
test/unit/environment_test.rb
@@ -355,4 +355,12 @@ class EnvironmentTest < Test::Unit::TestCase | @@ -355,4 +355,12 @@ class EnvironmentTest < Test::Unit::TestCase | ||
355 | assert_equal 'my footer', Environment.new(:custom_footer => "my footer").custom_footer | 355 | assert_equal 'my footer', Environment.new(:custom_footer => "my footer").custom_footer |
356 | end | 356 | end |
357 | 357 | ||
358 | + should 'provide theme' do | ||
359 | + assert_equal 'my-custom-theme', Environment.new(:theme => 'my-custom-theme').theme | ||
360 | + end | ||
361 | + | ||
362 | + should 'give default theme' do | ||
363 | + assert_equal 'default', Environment.new.theme | ||
364 | + end | ||
365 | + | ||
358 | end | 366 | end |
test/unit/profile_test.rb
@@ -771,6 +771,20 @@ class ProfileTest < Test::Unit::TestCase | @@ -771,6 +771,20 @@ class ProfileTest < Test::Unit::TestCase | ||
771 | assert_equal 'environment footer', profile.custom_footer | 771 | assert_equal 'environment footer', profile.custom_footer |
772 | end | 772 | end |
773 | 773 | ||
774 | + should 'store theme' do | ||
775 | + p = Profile.new(:theme => 'my-shiny-theme') | ||
776 | + assert_equal 'my-shiny-theme', p.theme | ||
777 | + end | ||
778 | + | ||
779 | + should 'delegate theme selection to environment by default' do | ||
780 | + p = Profile.new | ||
781 | + env = mock | ||
782 | + p.stubs(:environment).returns(env) | ||
783 | + env.expects(:theme).returns('environment-stored-theme') | ||
784 | + | ||
785 | + assert_equal 'environment-stored-theme', p.theme | ||
786 | + end | ||
787 | + | ||
774 | private | 788 | private |
775 | 789 | ||
776 | def assert_invalid_identifier(id) | 790 | def assert_invalid_identifier(id) |