Commit 3b18ff42c9397cdeec403b8043a91a3ebb108452

Authored by Victor Costa
2 parents c4aa0587 09811012

Merge branch 'AI2915-category_color' into rails3_stable

Conflicts:
	app/helpers/application_helper.rb
	app/helpers/categories_helper.rb
	db/migrate/20140807134625_change_category_display_color_to_string.rb
app/helpers/categories_helper.rb
... ... @@ -11,15 +11,6 @@ module CategoriesHelper
11 11 labelled_form_field(_('Type of category'), select_tag('type', options_for_select(TYPES, value)))
12 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 14 def category_color_style(category)
24 15 return '' if category.display_color.blank?
25 16 'background-color: #'+category.display_color+';'
... ... @@ -40,14 +31,4 @@ module CategoriesHelper
40 31 {:id => category_id ? "select-category-#{category_id}-link" : nil, :remote => true, :class => 'select-subcategory-link'}.merge(html_options)
41 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 34 end
... ...
app/models/category.rb
... ... @@ -105,4 +105,12 @@ class Category < ActiveRecord::Base
105 105 self.children.find(:all, :conditions => {:display_in_menu => true}).empty?
106 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 116 end
... ...
db/migrate/20140807134625_change_category_display_color_to_string.rb
1 1 class ChangeCategoryDisplayColorToString < ActiveRecord::Migration
2 2  
  3 + COLORS = ['ffa500', '00FF00', 'a020f0', 'ff0000', '006400', '191970', '0000ff', 'a52a2a', '32cd32', 'add8e6', '483d8b', 'b8e9ee', 'f5f5dc', 'ffff00', 'f4a460']
  4 +
3 5 def self.up
4 6 change_table :categories do |t|
5 7 t.string :display_color_tmp, :limit => 6
6 8 end
7   - Category.update_all({:display_color_tmp => "ffa500"}, {:display_color => 1})
8   - Category.update_all({:display_color_tmp => "00FF00"}, {:display_color => 2})
9   - Category.update_all({:display_color_tmp => "a020f0"}, {:display_color => 3})
10   - Category.update_all({:display_color_tmp => "ff0000"}, {:display_color => 4})
11   - Category.update_all({:display_color_tmp => "006400"}, {:display_color => 5})
12   - Category.update_all({:display_color_tmp => "191970"}, {:display_color => 6})
13   - Category.update_all({:display_color_tmp => "0000ff"}, {:display_color => 7})
14   - Category.update_all({:display_color_tmp => "a52a2a"}, {:display_color => 8})
15   - Category.update_all({:display_color_tmp => "32cd32"}, {:display_color => 9})
16   - Category.update_all({:display_color_tmp => "add8e6"}, {:display_color => 10})
17   - Category.update_all({:display_color_tmp => "483d8b"}, {:display_color => 11})
18   - Category.update_all({:display_color_tmp => "b8e9ee"}, {:display_color => 12})
19   - Category.update_all({:display_color_tmp => "f5f5dc"}, {:display_color => 13})
20   - Category.update_all({:display_color_tmp => "ffff00"}, {:display_color => 14})
21   - Category.update_all({:display_color_tmp => "f4a460"}, {:display_color => 15})
  9 +
  10 + COLORS.each_with_index do |color, i|
  11 + Category.update_all({:display_color_tmp => color}, {:display_color => i+1})
  12 + end
  13 +
  14 + change_table :categories do |t|
  15 + t.remove :display_color
  16 + t.rename :display_color_tmp, :display_color
  17 + end
22 18 end
23 19  
24 20 def self.down
  21 + puts "WARNING: only old defined colors will be reverted"
  22 +
  23 + change_table :categories do |t|
  24 + t.integer :display_color_tmp
  25 + end
  26 +
  27 + COLORS.each_with_index do |color, i|
  28 + Category.update_all({:display_color_tmp => i+1}, {:display_color => color})
  29 + end
  30 +
25 31 change_table :categories do |t|
26 32 t.remove :display_color
27 33 t.rename :display_color_tmp, :display_color
... ...
db/schema.rb
... ... @@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13  
14   -ActiveRecord::Schema.define(:version => 20140724134601) do
  14 +ActiveRecord::Schema.define(:version => 20140807134625) do
15 15  
16 16 create_table "abuse_reports", :force => true do |t|
17 17 t.integer "reporter_id"
... ... @@ -190,19 +190,19 @@ ActiveRecord::Schema.define(:version =&gt; 20140724134601) do
190 190 create_table "categories", :force => true do |t|
191 191 t.string "name"
192 192 t.string "slug"
193   - t.text "path", :default => ""
194   - t.integer "display_color"
  193 + t.text "path", :default => ""
195 194 t.integer "environment_id"
196 195 t.integer "parent_id"
197 196 t.string "type"
198 197 t.float "lat"
199 198 t.float "lng"
200   - t.boolean "display_in_menu", :default => false
201   - t.integer "children_count", :default => 0
202   - t.boolean "accept_products", :default => true
  199 + t.boolean "display_in_menu", :default => false
  200 + t.integer "children_count", :default => 0
  201 + t.boolean "accept_products", :default => true
203 202 t.integer "image_id"
204 203 t.string "acronym"
205 204 t.string "abbreviation"
  205 + t.string "display_color", :limit => 6
206 206 end
207 207  
208 208 create_table "categories_profiles", :id => false, :force => true do |t|
... ...
plugins/community_track/lib/community_track_plugin/track_helper.rb
1 1 module CommunityTrackPlugin::TrackHelper
2 2  
  3 + include CategoriesHelper
  4 +
3 5 def category_class(track)
4 6 'category_' + (track.categories.empty? ? 'not_defined' : track.categories.first.name.to_slug)
5 7 end
... ... @@ -9,4 +11,8 @@ module CommunityTrackPlugin::TrackHelper
9 11 excerpt(lead_stripped, lead_stripped.first(3), track.image ? 180 : 300)
10 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 18 end
... ...
plugins/community_track/test/unit/community_track_plugin/track_helper_test.rb
... ... @@ -52,4 +52,10 @@ class TrackHelperTest &lt; ActiveSupport::TestCase
52 52 assert_equal 186, track_card_lead(@track).length
53 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 61 end
... ...
plugins/community_track/views/blocks/_track_card.html.erb
... ... @@ -2,7 +2,7 @@
2 2 <div class="item_card <%= category_class(track_card) %>">
3 3 <a href="<%= url_for track_card.url %>">
4 4 <div class="track_content">
5   - <div class="title">
  5 + <div class="title" style="<%= track_color_style(track_card) %>">
6 6 <%= track_card.category_name %>
7 7 </div>
8 8 <div class="image">
... ...
test/unit/category_test.rb
... ... @@ -505,4 +505,28 @@ class CategoryTest &lt; ActiveSupport::TestCase
505 505 assert_includes Category.on_level(parent.id), category
506 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 532 end
... ...