Commit b4b7ad920df27d22be46560389de176e6e4646d8
Exists in
ratings_minor_fixes
and in
4 other branches
Merge branch 'statistics_block_api' into 'master'
expose statistics block atrributes in api expose statistics block atrributes in api See merge request !902
Showing
2 changed files
with
70 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,25 @@ |
1 | +require File.join(Rails.root,'lib','noosfero','api','entities') | |
2 | +module Noosfero | |
3 | + module API | |
4 | + module Entities | |
5 | + class Block < Entity | |
6 | + available_counters = (StatisticsBlock::USER_COUNTERS + StatisticsBlock::COMMUNITY_COUNTERS + StatisticsBlock::ENTERPRISE_COUNTERS).uniq | |
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 | |
19 | + end | |
20 | + | |
21 | + end | |
22 | + end | |
23 | + end | |
24 | +end | |
25 | + | ... | ... |
... | ... | @@ -0,0 +1,45 @@ |
1 | +require_relative '../../../../test/api/test_helper' | |
2 | + | |
3 | + | |
4 | +class ApiTest < ActiveSupport::TestCase | |
5 | + | |
6 | + def setup | |
7 | + create_and_activate_user | |
8 | + login_api | |
9 | + environment.enable_plugin(StatisticsPlugin) | |
10 | + end | |
11 | + | |
12 | + AVAILABLE_ATTRIBUTES = StatisticsBlock::USER_COUNTERS + StatisticsBlock::COMMUNITY_COUNTERS + StatisticsBlock::ENTERPRISE_COUNTERS | |
13 | + | |
14 | + AVAILABLE_ATTRIBUTES.map do |counter_attr| | |
15 | + counter_method = counter_attr.to_s.gsub('_counter','').pluralize.to_sym | |
16 | + define_method "test_should_return_#{counter_method}_attribute_in_statistics_block_if_#{counter_attr} is true" do | |
17 | + person.boxes.destroy_all | |
18 | + box = Box.create!(:owner => person) | |
19 | + block = StatisticsBlock.create!(:box_id => box.id) | |
20 | + block.send("#{counter_attr}=", true) | |
21 | + block.save | |
22 | + StatisticsBlock.any_instance.stubs(counter_method).returns(20) | |
23 | + get "/api/v1/profiles/#{person.id}/boxes?#{params.to_query}" | |
24 | + json = JSON.parse(last_response.body) | |
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 | |
28 | + end | |
29 | + | |
30 | + define_method "test_should_not_return_#{counter_method}_attribute_in_statistics_block_if_#{counter_attr} is false" do | |
31 | + person.boxes.destroy_all | |
32 | + box = Box.create!(:owner => person) | |
33 | + block = StatisticsBlock.create!(:box_id => box.id) | |
34 | + block.send("#{counter_attr}=", false) | |
35 | + block.save | |
36 | + StatisticsBlock.any_instance.stubs(counter_method).returns(20) | |
37 | + get "/api/v1/profiles/#{person.id}/boxes?#{params.to_query}" | |
38 | + json = JSON.parse(last_response.body) | |
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"] | |
42 | + end | |
43 | + end | |
44 | + | |
45 | +end | ... | ... |