Commit 09811012bffa6cee9478e5d1dd1ab827ac2e5f47
1 parent
262426cb
Exists in
staging
and in
4 other branches
Added background color to track card
Showing
6 changed files
with
45 additions
and
20 deletions
Show diff stats
app/helpers/categories_helper.rb
| @@ -11,15 +11,6 @@ module CategoriesHelper | @@ -11,15 +11,6 @@ module CategoriesHelper | ||
| 11 | labelled_form_field(_('Type of category'), select_tag('type', options_for_select(TYPES, value))) | 11 | labelled_form_field(_('Type of category'), select_tag('type', options_for_select(TYPES, value))) |
| 12 | end | 12 | end |
| 13 | 13 | ||
| 14 | - def display_color_for_category(category) | ||
| 15 | - color = category.display_color | ||
| 16 | - if color.nil? | ||
| 17 | - "" | ||
| 18 | - else | ||
| 19 | - "[" + gettext(CategoriesHelper::COLORS.find {|item| item[1] == color}.first) + "]" | ||
| 20 | - end | ||
| 21 | - end | ||
| 22 | - | ||
| 23 | def category_color_style(category) | 14 | def category_color_style(category) |
| 24 | return '' if category.display_color.blank? | 15 | return '' if category.display_color.blank? |
| 25 | 'background-color: #'+category.display_color+';' | 16 | 'background-color: #'+category.display_color+';' |
| @@ -40,14 +31,4 @@ module CategoriesHelper | @@ -40,14 +31,4 @@ module CategoriesHelper | ||
| 40 | {:id => category_id ? "select-category-#{category_id}-link" : nil, :remote => true, :class => 'select-subcategory-link'}.merge(html_options) | 31 | {:id => category_id ? "select-category-#{category_id}-link" : nil, :remote => true, :class => 'select-subcategory-link'}.merge(html_options) |
| 41 | end | 32 | end |
| 42 | 33 | ||
| 43 | - protected | ||
| 44 | - | ||
| 45 | - def search_category_tree_for_color(category) | ||
| 46 | - if category.display_color.blank? | ||
| 47 | - category.parent.nil? ? nil : search_category_tree_for_color(category.parent) | ||
| 48 | - else | ||
| 49 | - category.display_color | ||
| 50 | - end | ||
| 51 | - end | ||
| 52 | - | ||
| 53 | end | 34 | end |
app/models/category.rb
| @@ -105,4 +105,12 @@ class Category < ActiveRecord::Base | @@ -105,4 +105,12 @@ class Category < ActiveRecord::Base | ||
| 105 | self.children.find(:all, :conditions => {:display_in_menu => true}).empty? | 105 | self.children.find(:all, :conditions => {:display_in_menu => true}).empty? |
| 106 | end | 106 | end |
| 107 | 107 | ||
| 108 | + def with_color | ||
| 109 | + if display_color.blank? | ||
| 110 | + parent.nil? ? nil : parent.with_color | ||
| 111 | + else | ||
| 112 | + self | ||
| 113 | + end | ||
| 114 | + end | ||
| 115 | + | ||
| 108 | end | 116 | end |
plugins/community_track/lib/community_track_plugin/track_helper.rb
| 1 | module CommunityTrackPlugin::TrackHelper | 1 | module CommunityTrackPlugin::TrackHelper |
| 2 | 2 | ||
| 3 | + include CategoriesHelper | ||
| 4 | + | ||
| 3 | def category_class(track) | 5 | def category_class(track) |
| 4 | 'category_' + (track.categories.empty? ? 'not_defined' : track.categories.first.name.to_slug) | 6 | 'category_' + (track.categories.empty? ? 'not_defined' : track.categories.first.name.to_slug) |
| 5 | end | 7 | end |
| @@ -9,4 +11,8 @@ module CommunityTrackPlugin::TrackHelper | @@ -9,4 +11,8 @@ module CommunityTrackPlugin::TrackHelper | ||
| 9 | excerpt(lead_stripped, lead_stripped.first(3), track.image ? 180 : 300) | 11 | excerpt(lead_stripped, lead_stripped.first(3), track.image ? 180 : 300) |
| 10 | end | 12 | end |
| 11 | 13 | ||
| 14 | + def track_color_style(track) | ||
| 15 | + category_color_style(track.categories.first.with_color) if !track.categories.empty? | ||
| 16 | + end | ||
| 17 | + | ||
| 12 | end | 18 | end |
plugins/community_track/test/unit/community_track_plugin/track_helper_test.rb
| @@ -52,4 +52,10 @@ class TrackHelperTest < ActiveSupport::TestCase | @@ -52,4 +52,10 @@ class TrackHelperTest < ActiveSupport::TestCase | ||
| 52 | assert_equal 186, track_card_lead(@track).length | 52 | assert_equal 186, track_card_lead(@track).length |
| 53 | end | 53 | end |
| 54 | 54 | ||
| 55 | + should 'return category color if its defined' do | ||
| 56 | + category1 = fast_create(Category, :name => 'education', :display_color => 'fbfbfb') | ||
| 57 | + @track.categories << category1 | ||
| 58 | + assert_equal 'background-color: #fbfbfb;', track_color_style(@track) | ||
| 59 | + end | ||
| 60 | + | ||
| 55 | end | 61 | end |
plugins/community_track/views/blocks/_track_card.html.erb
| @@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
| 2 | <div class="item_card <%= category_class(track_card) %>"> | 2 | <div class="item_card <%= category_class(track_card) %>"> |
| 3 | <a href="<%= url_for track_card.url %>"> | 3 | <a href="<%= url_for track_card.url %>"> |
| 4 | <div class="track_content"> | 4 | <div class="track_content"> |
| 5 | - <div class="title"> | 5 | + <div class="title" style="<%= track_color_style(track_card) %>"> |
| 6 | <%= track_card.category_name %> | 6 | <%= track_card.category_name %> |
| 7 | </div> | 7 | </div> |
| 8 | <div class="image"> | 8 | <div class="image"> |
test/unit/category_test.rb
| @@ -505,4 +505,28 @@ class CategoryTest < ActiveSupport::TestCase | @@ -505,4 +505,28 @@ class CategoryTest < ActiveSupport::TestCase | ||
| 505 | assert_includes Category.on_level(parent.id), category | 505 | assert_includes Category.on_level(parent.id), category |
| 506 | end | 506 | end |
| 507 | 507 | ||
| 508 | + should 'return self if the category has display_color defined' do | ||
| 509 | + c1 = fast_create(Category) | ||
| 510 | + c2 = fast_create(Category, :parent_id => c1) | ||
| 511 | + c3 = fast_create(Category, :parent_id => c2, :display_color => 'FFFFFF') | ||
| 512 | + c4 = fast_create(Category, :parent_id => c3, :display_color => '000000') | ||
| 513 | + assert_equal c4, c4.with_color | ||
| 514 | + end | ||
| 515 | + | ||
| 516 | + should 'return first category on hierarchy with display_color defined' do | ||
| 517 | + c1 = fast_create(Category, :display_color => '111111') | ||
| 518 | + c2 = fast_create(Category, :parent_id => c1) | ||
| 519 | + c3 = fast_create(Category, :parent_id => c2) | ||
| 520 | + c4 = fast_create(Category, :parent_id => c3) | ||
| 521 | + assert_equal c1, c4.with_color | ||
| 522 | + end | ||
| 523 | + | ||
| 524 | + should 'return nil if no category on hierarchy has display_color defined' do | ||
| 525 | + c1 = fast_create(Category) | ||
| 526 | + c2 = fast_create(Category, :parent_id => c1) | ||
| 527 | + c3 = fast_create(Category, :parent_id => c2) | ||
| 528 | + c4 = fast_create(Category, :parent_id => c3) | ||
| 529 | + assert_equal nil, c4.with_color | ||
| 530 | + end | ||
| 531 | + | ||
| 508 | end | 532 | end |