From 53e85962c80011f8aa9b311079e9037c2665ffbb Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Fri, 8 Aug 2014 14:46:51 -0300 Subject: [PATCH] community_track: added background color to track card --- app/helpers/categories_helper.rb | 19 ------------------- app/models/category.rb | 8 ++++++++ plugins/community_track/lib/community_track_plugin/track_helper.rb | 6 ++++++ plugins/community_track/test/unit/community_track_plugin/track_helper_test.rb | 6 ++++++ plugins/community_track/views/blocks/_track_card.html.erb | 2 +- test/unit/category_test.rb | 24 ++++++++++++++++++++++++ 6 files changed, 45 insertions(+), 20 deletions(-) diff --git a/app/helpers/categories_helper.rb b/app/helpers/categories_helper.rb index 4f3ae40..4454141 100644 --- a/app/helpers/categories_helper.rb +++ b/app/helpers/categories_helper.rb @@ -11,15 +11,6 @@ module CategoriesHelper labelled_form_field(_('Type of category'), select_tag('type', options_for_select(TYPES, value))) end - def display_color_for_category(category) - color = category.display_color - if color.nil? - "" - else - "[" + gettext(CategoriesHelper::COLORS.find {|item| item[1] == color}.first) + "]" - end - end - def category_color_style(category) return '' if category.display_color.blank? 'background-color: #'+category.display_color+';' @@ -40,14 +31,4 @@ module CategoriesHelper {:id => category_id ? "select-category-#{category_id}-link" : nil, :remote => true, :class => 'select-subcategory-link'}.merge(html_options) end - protected - - def search_category_tree_for_color(category) - if category.display_color.blank? - category.parent.nil? ? nil : search_category_tree_for_color(category.parent) - else - category.display_color - end - end - end diff --git a/app/models/category.rb b/app/models/category.rb index c468372..2c4aa67 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -105,4 +105,12 @@ class Category < ActiveRecord::Base self.children.find(:all, :conditions => {:display_in_menu => true}).empty? end + def with_color + if display_color.blank? + parent.nil? ? nil : parent.with_color + else + self + end + end + end diff --git a/plugins/community_track/lib/community_track_plugin/track_helper.rb b/plugins/community_track/lib/community_track_plugin/track_helper.rb index d926589..4938978 100644 --- a/plugins/community_track/lib/community_track_plugin/track_helper.rb +++ b/plugins/community_track/lib/community_track_plugin/track_helper.rb @@ -1,5 +1,7 @@ module CommunityTrackPlugin::TrackHelper + include CategoriesHelper + def category_class(track) 'category_' + (track.categories.empty? ? 'not_defined' : track.categories.first.name.to_slug) end @@ -9,4 +11,8 @@ module CommunityTrackPlugin::TrackHelper excerpt(lead_stripped, lead_stripped.first(3), track.image ? 180 : 300) end + def track_color_style(track) + category_color_style(track.categories.first.with_color) if !track.categories.empty? + end + end diff --git a/plugins/community_track/test/unit/community_track_plugin/track_helper_test.rb b/plugins/community_track/test/unit/community_track_plugin/track_helper_test.rb index 6be91d2..5d57520 100644 --- a/plugins/community_track/test/unit/community_track_plugin/track_helper_test.rb +++ b/plugins/community_track/test/unit/community_track_plugin/track_helper_test.rb @@ -52,4 +52,10 @@ class TrackHelperTest < ActiveSupport::TestCase assert_equal 186, track_card_lead(@track).length end + should 'return category color if its defined' do + category1 = fast_create(Category, :name => 'education', :display_color => 'fbfbfb') + @track.categories << category1 + assert_equal 'background-color: #fbfbfb;', track_color_style(@track) + end + end diff --git a/plugins/community_track/views/blocks/_track_card.html.erb b/plugins/community_track/views/blocks/_track_card.html.erb index bde884e..e7e4a84 100644 --- a/plugins/community_track/views/blocks/_track_card.html.erb +++ b/plugins/community_track/views/blocks/_track_card.html.erb @@ -2,7 +2,7 @@
-
+
<%= track_card.category_name %>
diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index eff875f..7b665d7 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -505,4 +505,28 @@ class CategoryTest < ActiveSupport::TestCase assert_includes Category.on_level(parent.id), category end + should 'return self if the category has display_color defined' do + c1 = fast_create(Category) + c2 = fast_create(Category, :parent_id => c1) + c3 = fast_create(Category, :parent_id => c2, :display_color => 'FFFFFF') + c4 = fast_create(Category, :parent_id => c3, :display_color => '000000') + assert_equal c4, c4.with_color + end + + should 'return first category on hierarchy with display_color defined' do + c1 = fast_create(Category, :display_color => '111111') + c2 = fast_create(Category, :parent_id => c1) + c3 = fast_create(Category, :parent_id => c2) + c4 = fast_create(Category, :parent_id => c3) + assert_equal c1, c4.with_color + end + + should 'return nil if no category on hierarchy has display_color defined' do + c1 = fast_create(Category) + c2 = fast_create(Category, :parent_id => c1) + c3 = fast_create(Category, :parent_id => c2) + c4 = fast_create(Category, :parent_id => c3) + assert_equal nil, c4.with_color + end + end -- libgit2 0.21.2