Commit fcf765cfd4de3aba2d77c17a5e5a0ebdc754908e

Authored by Eduardo Passos
1 parent 320bcfbb

statistics-block: add products counter

plugins/statistics/lib/statistics_block.rb
... ... @@ -3,13 +3,14 @@ class StatisticsBlock < Block
3 3 settings_items :community_counter, :default => false
4 4 settings_items :user_counter, :default => true
5 5 settings_items :enterprise_counter, :default => false
  6 + settings_items :product_counter, :default => false
6 7 settings_items :category_counter, :default => false
7 8 settings_items :tag_counter, :default => true
8 9 settings_items :comment_counter, :default => true
9 10 settings_items :hit_counter, :default => false
10 11 settings_items :templates_ids_counter, Hash, :default => {}
11 12  
12   - attr_accessible :comment_counter, :community_counter, :user_counter, :enterprise_counter, :category_counter, :tag_counter, :hit_counter, :templates_ids_counter
  13 + attr_accessible :comment_counter, :community_counter, :user_counter, :enterprise_counter, :product_counter, :category_counter, :tag_counter, :hit_counter, :templates_ids_counter
13 14  
14 15 USER_COUNTERS = [:community_counter, :user_counter, :enterprise_counter, :tag_counter, :comment_counter, :hit_counter]
15 16 COMMUNITY_COUNTERS = [:user_counter, :tag_counter, :comment_counter, :hit_counter]
... ... @@ -91,6 +92,16 @@ class StatisticsBlock < Block
91 92 end
92 93 end
93 94  
  95 + def products
  96 + if owner.kind_of?(Environment)
  97 + owner.products.where("profiles.enabled = 't' and profiles.visible = 't'").count
  98 + elsif owner.kind_of?(Enterprise)
  99 + owner.products.count
  100 + else
  101 + 0
  102 + end
  103 + end
  104 +
94 105 def communities
95 106 if owner.kind_of?(Environment) || owner.kind_of?(Person)
96 107 owner.communities.visible.count
... ...
plugins/statistics/test/functional/statistics_plugin_environment_design_controller_test.rb
... ... @@ -42,18 +42,19 @@ class EnvironmentDesignControllerTest < ActionController::TestCase
42 42 @block.user_counter = true
43 43 @block.community_counter = true
44 44 @block.enterprise_counter = true
  45 + @block.product_counter = true
45 46 @block.category_counter = true
46 47 @block.tag_counter = true
47 48 @block.comment_counter = true
48 49 @block.hit_counter = true
49 50 @block.save!
50 51 get :edit, :id => @block.id
51   - post :save, :id => @block.id, :block => {:user_counter => '0', :community_counter => '0', :enterprise_counter => '0',
52   - :category_counter => '0', :tag_counter => '0', :comment_counter => '0', :hit_counter => '0' }
  52 + post :save, :id => @block.id, :block => {:user_counter => '0', :community_counter => '0', :enterprise_counter => '0', :product_counter => '0', :category_counter => '0', :tag_counter => '0', :comment_counter => '0', :hit_counter => '0'}
53 53 @block.reload
54 54 any_checked = @block.is_visible?('user_counter') ||
55 55 @block.is_visible?('community_counter') ||
56 56 @block.is_visible?('enterprise_counter') ||
  57 + @block.is_visible?('product_counter') ||
57 58 @block.is_visible?('category_counter') ||
58 59 @block.is_visible?('tag_counter') ||
59 60 @block.is_visible?('comment_counter') ||
... ... @@ -66,18 +67,20 @@ class EnvironmentDesignControllerTest < ActionController::TestCase
66 67 @block.user_counter = false
67 68 @block.community_counter = false
68 69 @block.enterprise_counter = false
  70 + @block.product_counter = false
69 71 @block.category_counter = false
70 72 @block.tag_counter = false
71 73 @block.comment_counter = false
72 74 @block.hit_counter = false
73 75 @block.save!
74 76 get :edit, :id => @block.id
75   - post :save, :id => @block.id, :block => {:user_counter => '1', :community_counter => '1', :enterprise_counter => '1',
  77 + post :save, :id => @block.id, :block => {:user_counter => '1', :community_counter => '1', :enterprise_counter => '1', :product_counter => '1',
76 78 :category_counter => '1', :tag_counter => '1', :comment_counter => '1', :hit_counter => '1' }
77 79 @block.reload
78 80 all_checked = @block.is_visible?('user_counter') &&
79 81 @block.is_visible?('community_counter') &&
80 82 @block.is_visible?('enterprise_counter') &&
  83 + @block.is_visible?('product_counter') &&
81 84 @block.is_visible?('category_counter') &&
82 85 @block.is_visible?('tag_counter') &&
83 86 @block.is_visible?('comment_counter') &&
... ... @@ -128,14 +131,21 @@ class EnvironmentDesignControllerTest < ActionController::TestCase
128 131 assert_no_tag :input, :attributes => {:id => 'block_enterprise_counter', :checked => 'checked'}
129 132 end
130 133  
131   - should 'not input category counter be checked by default' do
  134 + should 'not input product counter be checked by default' do
  135 + get :edit, :id => @block.id
  136 +
  137 + assert_tag :input, :attributes => {:id => 'block_product_counter'}
  138 + assert_no_tag :input, :attributes => {:id => 'block_product_counter', :checked => 'checked'}
  139 + end
  140 +
  141 + should 'not input category counter be checked by default' do
132 142 get :edit, :id => @block.id
133 143  
134 144 assert_tag :input, :attributes => {:id => 'block_category_counter'}
135 145 assert_no_tag :input, :attributes => {:id => 'block_category_counter', :checked => 'checked'}
136 146 end
137 147  
138   - should 'input tag counter be checked by default' do
  148 + should 'input tag counter be checked by default' do
139 149 get :edit, :id => @block.id
140 150  
141 151 assert_tag :input, :attributes => {:id => 'block_tag_counter', :checked => 'checked'}
... ... @@ -152,4 +162,4 @@ class EnvironmentDesignControllerTest < ActionController::TestCase
152 162  
153 163 assert_no_tag :input, :attributes => {:id => 'block_hit_counter', :checked => 'checked'}
154 164 end
155 165 -end
  166 +end
156 167 \ No newline at end of file
... ...
plugins/statistics/test/functional/statistics_plugin_home_controller_test.rb
... ... @@ -74,6 +74,20 @@ class HomeControllerTest < ActionController::TestCase
74 74 assert_no_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'enterprises'} }
75 75 end
76 76  
  77 + should 'display products class in statistics-block-data block' do
  78 + @block.product_counter = true
  79 + @block.save!
  80 + get :index
  81 +
  82 + assert_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'products'} }
  83 + end
  84 +
  85 + should 'not display products class in statistics-block-data block' do
  86 + get :index
  87 +
  88 + assert_no_tag :tag => 'div', :attributes => {:class => 'statistics-block-data'}, :descendant => { :tag => 'li', :attributes => {:class => 'products'} }
  89 + end
  90 +
77 91 should 'display categories class in statistics-block-data block' do
78 92 @block.category_counter = true
79 93 @block.save!
... ...
plugins/statistics/test/unit/statistics_block_test.rb
... ... @@ -8,7 +8,7 @@ class StatisticsBlockTest < ActiveSupport::TestCase
8 8 end
9 9 end
10 10  
11   - ['community_counter', 'enterprise_counter', 'category_counter', 'hit_counter'].map do |counter|
  11 + ['community_counter', 'enterprise_counter', 'product_counter', 'category_counter', 'hit_counter'].map do |counter|
12 12 should "#{counter} be false by default" do
13 13 b = StatisticsBlock.new
14 14 assert !b.is_visible?(counter)
... ... @@ -139,6 +139,40 @@ class StatisticsBlockTest < ActiveSupport::TestCase
139 139 assert_equal 2, b.enterprises
140 140 end
141 141  
  142 + should 'return the amount of visible environment products' do
  143 + b = StatisticsBlock.new
  144 + e = fast_create(Environment)
  145 +
  146 + e1 = fast_create(Enterprise, :visible => true, :enabled => true, :environment_id => e.id)
  147 + e2 = fast_create(Enterprise, :visible => true, :enabled => false, :environment_id => e.id)
  148 + e3 = fast_create(Enterprise, :visible => false, :enabled => true, :environment_id => e.id)
  149 +
  150 + fast_create(Product, :profile_id => e1.id)
  151 + fast_create(Product, :profile_id => e1.id)
  152 + fast_create(Product, :profile_id => e2.id)
  153 + fast_create(Product, :profile_id => e2.id)
  154 + fast_create(Product, :profile_id => e3.id)
  155 + fast_create(Product, :profile_id => e3.id)
  156 +
  157 + b.expects(:owner).at_least_once.returns(e)
  158 +
  159 + assert_equal 2, b.products
  160 + end
  161 +
  162 + should 'return the amount of visible enterprise products' do
  163 + b = StatisticsBlock.new
  164 +
  165 + e = fast_create(Enterprise)
  166 +
  167 + fast_create(Product, :profile_id => e.id)
  168 + fast_create(Product, :profile_id => e.id)
  169 + fast_create(Product, :profile_id => nil)
  170 +
  171 + b.expects(:owner).at_least_once.returns(e)
  172 +
  173 + assert_equal 2, b.products
  174 + end
  175 +
142 176 should 'categories return the amount of categories of the Environment' do
143 177 b = StatisticsBlock.new
144 178 e = fast_create(Environment)
... ...
plugins/statistics/views/box_organizer/_statistics_block.html.erb
1 1 <%= labelled_form_field check_box(:block, :user_counter) + _('Show user counter'), '' %>
2 2  
3 3 <% if @block.is_counter_available?(:community_counter) %>
4   -<%= labelled_form_field check_box(:block, :community_counter) + _('Show community counter'), '' %>
  4 + <%= labelled_form_field check_box(:block, :community_counter) + _('Show community counter'), '' %>
5 5 <% end %>
6 6  
7 7 <% if @block.is_counter_available?(:enterprise_counter) %>
8   -<%= labelled_form_field check_box(:block, :enterprise_counter) + _('Show enterprise counter'), '' %>
  8 + <%= labelled_form_field check_box(:block, :enterprise_counter) + _('Show enterprise counter'), '' %>
  9 +<% end %>
  10 +
  11 +<% if @block.is_counter_available?(:product_counter) %>
  12 + <%= labelled_form_field check_box(:block, :product_counter) + _('Show product counter'), '' %>
9 13 <% end %>
10 14  
11 15 <% if @block.is_counter_available?(:category_counter) %>
12   -<%= labelled_form_field check_box(:block, :category_counter) + _('Show category counter'), '' %>
  16 + <%= labelled_form_field check_box(:block, :category_counter) + _('Show category counter'), '' %>
13 17 <% end %>
14 18  
15 19 <% if @block.is_counter_available?(:tag_counter) %>
16   -<%= labelled_form_field check_box(:block, :tag_counter) + _('Show tag counter'), '' %>
  20 + <%= labelled_form_field check_box(:block, :tag_counter) + _('Show tag counter'), '' %>
17 21 <% end %>
18 22  
19 23 <% if @block.is_counter_available?(:comment_counter) %>
20   -<%= labelled_form_field check_box(:block, :comment_counter) + _('Show comment counter'), '' %>
  24 + <%= labelled_form_field check_box(:block, :comment_counter) + _('Show comment counter'), '' %>
21 25 <% end %>
22 26  
23 27 <% if @block.is_counter_available?(:hit_counter) %>
24   -<%= labelled_form_field check_box(:block, :hit_counter) + _('Show hit counter'), '' %>
  28 + <%= labelled_form_field check_box(:block, :hit_counter) + _('Show hit counter'), '' %>
25 29 <% end %>
26 30  
27 31 <% if @block.is_counter_available?(:templates_ids_counter) %>
28   -<% @block.templates.map do |item|%>
29   - <%= hidden_field_tag("block[templates_ids_counter][#{item.id}]", false)%>
30   - <%= labelled_form_field check_box_tag("block[templates_ids_counter][#{item.id}]", true, @block.is_template_counter_active?(item.id)) + _("Show counter for communities with template %s" % item.name), '' %>
31   -<% end %>
  32 + <% @block.templates.map do |item|%>
  33 + <%= hidden_field_tag("block[templates_ids_counter][#{item.id}]", false)%>
  34 + <%= labelled_form_field check_box_tag("block[templates_ids_counter][#{item.id}]", true, @block.is_template_counter_active?(item.id)) + _("Show counter for communities with template %s" % item.name), '' %>
  35 + <% end %>
32 36 <% end %>
... ...
plugins/statistics/views/statistics_block.html.erb
... ... @@ -9,6 +9,9 @@
9 9 <% if block.is_visible?('enterprise_counter') && !block.environment.enabled?('disable_asset_enterprises') %>
10 10 <li class="enterprises"><span class="amount"><%= block.enterprises%> </span><span class="label"><%= _('enterprises')%></span></li>
11 11 <% end %>
  12 + <% if block.is_visible?('product_counter') && !block.environment.enabled?('disable_asset_products') %>
  13 + <li class="products"><span class="amount"><%= block.products%> </span><span class="label"><%= _('products')%></span></li>
  14 + <% end %>
12 15 <% if block.is_visible?('community_counter') %>
13 16 <li class="communities"><span class="amount"><%= block.communities%> </span><span class="label"><%= _('communities')%></span></li>
14 17 <% end %>
... ...