[Git][noosfero/noosfero][master] 3 commits: expose statistics block atrributes in api

Leandro Nunes gitlab at mg.gitlab.com
Tue May 10 19:15:38 BRT 2016


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
c93899a3 by Leandro Nunes dos Santos at 2016-05-09T09:14:43-03:00
expose statistics block atrributes in api

- - - - -
6dacafc8 by ABNER SILVA DE OLIVEIRA at 2016-05-10T18:47:01-03:00
changed to expose all statistics on StatisticsBlock under an array called statistics

- - - - -
b4b7ad92 by Leandro Nunes at 2016-05-10T22:15:24+00:00
Merge branch 'statistics_block_api' into 'master'

expose statistics block atrributes in api

expose statistics block atrributes in api


See merge request !902
- - - - -


2 changed files:

- + plugins/statistics/lib/ext/entities.rb
- + plugins/statistics/test/unit/api_test.rb


Changes:

=====================================
plugins/statistics/lib/ext/entities.rb
=====================================
--- /dev/null
+++ b/plugins/statistics/lib/ext/entities.rb
@@ -0,0 +1,25 @@
+require File.join(Rails.root,'lib','noosfero','api','entities')
+module Noosfero
+  module API
+    module Entities
+      class Block < Entity
+        available_counters = (StatisticsBlock::USER_COUNTERS + StatisticsBlock::COMMUNITY_COUNTERS + StatisticsBlock::ENTERPRISE_COUNTERS).uniq
+        expose :statistics, :if => lambda { |block, options| block.is_a? StatisticsBlock } do |block, options|
+          statistics = []
+          available_counters.each do |counter_attr|
+            counter_method = counter_attr.to_s.gsub('_counter','').pluralize.to_sym
+            counter = {
+                name:  counter_method,
+                display: block.is_counter_available?(counter_attr) && block.is_visible?(counter_attr),
+                quantity: (block.respond_to?(counter_method) && block.is_visible?(counter_attr)) ? block.send(counter_method) :  nil
+            }
+            statistics << counter
+          end
+          statistics
+        end
+
+      end
+    end
+  end
+end
+


=====================================
plugins/statistics/test/unit/api_test.rb
=====================================
--- /dev/null
+++ b/plugins/statistics/test/unit/api_test.rb
@@ -0,0 +1,45 @@
+require_relative '../../../../test/api/test_helper'
+
+
+class ApiTest < ActiveSupport::TestCase
+
+  def setup
+    create_and_activate_user
+    login_api
+    environment.enable_plugin(StatisticsPlugin)
+  end
+
+  AVAILABLE_ATTRIBUTES = StatisticsBlock::USER_COUNTERS + StatisticsBlock::COMMUNITY_COUNTERS + StatisticsBlock::ENTERPRISE_COUNTERS
+
+  AVAILABLE_ATTRIBUTES.map do |counter_attr|
+    counter_method = counter_attr.to_s.gsub('_counter','').pluralize.to_sym
+    define_method "test_should_return_#{counter_method}_attribute_in_statistics_block_if_#{counter_attr} is true" do
+      person.boxes.destroy_all
+      box = Box.create!(:owner => person)
+      block = StatisticsBlock.create!(:box_id => box.id)
+      block.send("#{counter_attr}=", true)
+      block.save
+      StatisticsBlock.any_instance.stubs(counter_method).returns(20)
+      get "/api/v1/profiles/#{person.id}/boxes?#{params.to_query}"
+      json = JSON.parse(last_response.body)
+      statistics = json['boxes'].first['blocks'].first['statistics']
+      statistic_for_method = statistics.select {|statistic| statistic if statistic['name'].eql? counter_method.to_s }
+      assert_equal statistic_for_method.first['quantity'], 20
+    end
+
+    define_method "test_should_not_return_#{counter_method}_attribute_in_statistics_block_if_#{counter_attr} is false" do
+      person.boxes.destroy_all
+      box = Box.create!(:owner => person)
+      block = StatisticsBlock.create!(:box_id => box.id)
+      block.send("#{counter_attr}=", false)
+      block.save
+      StatisticsBlock.any_instance.stubs(counter_method).returns(20)
+      get "/api/v1/profiles/#{person.id}/boxes?#{params.to_query}"
+      json = JSON.parse(last_response.body)
+      statistics = json['boxes'].first['blocks'].first['statistics']
+      statistic_for_method = statistics.select {|statistic| statistic if statistic['name'].eql? counter_method.to_s }
+      assert_nil statistic_for_method.first["quantity"]
+    end
+  end
+
+end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/8c14cce34d74073835603bfbc2dce842e9e7c759...b4b7ad920df27d22be46560389de176e6e4646d8
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160510/9bf55cda/attachment-0001.html>


More information about the Noosfero-dev mailing list