Commit c93899a31129ad24f0872d6374c1e6805cbbe304

Authored by Leandro Santos
1 parent 4d79eb7a

expose statistics block atrributes in api

plugins/statistics/lib/ext/entities.rb 0 → 100644
... ... @@ -0,0 +1,21 @@
  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 +
  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 + }
  15 + end
  16 +
  17 + end
  18 + end
  19 + end
  20 +end
  21 +
... ...
plugins/statistics/test/unit/api_test.rb 0 → 100644
... ... @@ -0,0 +1,41 @@
  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 + get "/api/v1/profiles/#{person.id}/boxes?#{params.to_query}"
  23 +
  24 + json = JSON.parse(last_response.body)
  25 + assert_not_nil json["boxes"].first['blocks'].first[counter_method.to_s]
  26 + end
  27 +
  28 + define_method "test_should_not_return_#{counter_method}_attribute_in_statistics_block_if_#{counter_attr} is false" do
  29 + person.boxes.destroy_all
  30 + box = Box.create!(:owner => person)
  31 + block = StatisticsBlock.create!(:box_id => box.id)
  32 + block.send("#{counter_attr}=", false)
  33 + block.save
  34 + get "/api/v1/profiles/#{person.id}/boxes?#{params.to_query}"
  35 +
  36 + json = JSON.parse(last_response.body)
  37 + assert_nil json["boxes"].first['blocks'].first[counter_method]
  38 + end
  39 + end
  40 +
  41 +end
... ...