Commit 6dacafc81d15ec96f422ded0ace2791cbd88a5d7
1 parent
c93899a3
Exists in
staging
and in
29 other branches
changed to expose all statistics on StatisticsBlock under an array called statistics
Showing
2 changed files
with
20 additions
and
12 deletions
Show diff stats
plugins/statistics/lib/ext/entities.rb
| ... | ... | @@ -4,14 +4,18 @@ module Noosfero |
| 4 | 4 | module Entities |
| 5 | 5 | class Block < Entity |
| 6 | 6 | available_counters = (StatisticsBlock::USER_COUNTERS + StatisticsBlock::COMMUNITY_COUNTERS + StatisticsBlock::ENTERPRISE_COUNTERS).uniq |
| 7 | - | |
| 8 | - available_counters.each do |counter_attr| | |
| 9 | - expose counter_attr, :if => lambda{|block,options| block.respond_to?(counter_attr) && block.is_counter_available?(counter_attr)} | |
| 10 | - | |
| 11 | - counter_method = counter_attr.to_s.gsub('_counter','').pluralize.to_sym | |
| 12 | - expose counter_method, :if => lambda { |block,options| | |
| 13 | - block.respond_to?(counter_method) && block.is_visible?(counter_attr) | |
| 14 | - } | |
| 7 | + expose :statistics, :if => lambda { |block, options| block.is_a? StatisticsBlock } do |block, options| | |
| 8 | + statistics = [] | |
| 9 | + available_counters.each do |counter_attr| | |
| 10 | + counter_method = counter_attr.to_s.gsub('_counter','').pluralize.to_sym | |
| 11 | + counter = { | |
| 12 | + name: counter_method, | |
| 13 | + display: block.is_counter_available?(counter_attr) && block.is_visible?(counter_attr), | |
| 14 | + quantity: (block.respond_to?(counter_method) && block.is_visible?(counter_attr)) ? block.send(counter_method) : nil | |
| 15 | + } | |
| 16 | + statistics << counter | |
| 17 | + end | |
| 18 | + statistics | |
| 15 | 19 | end |
| 16 | 20 | |
| 17 | 21 | end | ... | ... |
plugins/statistics/test/unit/api_test.rb
| ... | ... | @@ -19,10 +19,12 @@ class ApiTest < ActiveSupport::TestCase |
| 19 | 19 | block = StatisticsBlock.create!(:box_id => box.id) |
| 20 | 20 | block.send("#{counter_attr}=", true) |
| 21 | 21 | block.save |
| 22 | + StatisticsBlock.any_instance.stubs(counter_method).returns(20) | |
| 22 | 23 | get "/api/v1/profiles/#{person.id}/boxes?#{params.to_query}" |
| 23 | - | |
| 24 | 24 | json = JSON.parse(last_response.body) |
| 25 | - assert_not_nil json["boxes"].first['blocks'].first[counter_method.to_s] | |
| 25 | + statistics = json['boxes'].first['blocks'].first['statistics'] | |
| 26 | + statistic_for_method = statistics.select {|statistic| statistic if statistic['name'].eql? counter_method.to_s } | |
| 27 | + assert_equal statistic_for_method.first['quantity'], 20 | |
| 26 | 28 | end |
| 27 | 29 | |
| 28 | 30 | define_method "test_should_not_return_#{counter_method}_attribute_in_statistics_block_if_#{counter_attr} is false" do |
| ... | ... | @@ -31,10 +33,12 @@ class ApiTest < ActiveSupport::TestCase |
| 31 | 33 | block = StatisticsBlock.create!(:box_id => box.id) |
| 32 | 34 | block.send("#{counter_attr}=", false) |
| 33 | 35 | block.save |
| 36 | + StatisticsBlock.any_instance.stubs(counter_method).returns(20) | |
| 34 | 37 | get "/api/v1/profiles/#{person.id}/boxes?#{params.to_query}" |
| 35 | - | |
| 36 | 38 | json = JSON.parse(last_response.body) |
| 37 | - assert_nil json["boxes"].first['blocks'].first[counter_method] | |
| 39 | + statistics = json['boxes'].first['blocks'].first['statistics'] | |
| 40 | + statistic_for_method = statistics.select {|statistic| statistic if statistic['name'].eql? counter_method.to_s } | |
| 41 | + assert_nil statistic_for_method.first["quantity"] | |
| 38 | 42 | end |
| 39 | 43 | end |
| 40 | 44 | ... | ... |