Commit be9074f5b1e7d0b07a0f569e587edbb458677e5d

Authored by Victor Costa
1 parent cdfd3ecb

proposals_discussion: add api method that return proposal ranking

lib/proposals_discussion_plugin.rb
... ... @@ -48,4 +48,8 @@ class ProposalsDiscussionPlugin < Noosfero::Plugin
48 48 ['jquery.jscroll.min.js', 'jquery.masonry.min.js', 'flotr2.min.js']
49 49 end
50 50  
  51 + def self.api_mount_points
  52 + [ProposalsDiscussionPlugin::API]
  53 + end
  54 +
51 55 end
... ...
lib/proposals_discussion_plugin/api.rb 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +class ProposalsDiscussionPlugin::API < Grape::API
  2 +
  3 + resource :proposals_discussion_plugin do
  4 +
  5 + get ':id/ranking' do
  6 + article = find_article(environment.articles, params[:id])
  7 + proposals = article.proposals.map do |proposal|
  8 + effective_support = (proposal.votes_for - proposal.votes_against)/proposal.hits.to_f
  9 + effective_participation = (proposal.votes_for + proposal.votes_against)/proposal.hits.to_f
  10 +
  11 + {:id => proposal.id, :abstract => proposal.abstract, :votes_for => proposal.votes_for, :votes_against => proposal.votes_against, :effective_support => effective_support, :effective_participation => effective_participation}
  12 + end
  13 +
  14 + proposals = proposals.sort_by { |p| p[:effective_support] }.reverse
  15 + {:proposals => proposals, :updated_at => DateTime.now}
  16 + end
  17 +
  18 + end
  19 +
  20 +end
... ...
lib/proposals_discussion_plugin/proposals_holder.rb
1 1 class ProposalsDiscussionPlugin::ProposalsHolder < Folder
2 2  
  3 + has_many :proposals, :class_name => 'ProposalsDiscussionPlugin::Proposal', :foreign_key => 'parent_id'
  4 + has_many :proposals_authors, :class_name => 'Person', :through => :children, :source => :created_by
  5 +
3 6 def accept_comments?
4 7 accept_comments
5 8 end
... ...
lib/proposals_discussion_plugin/topic.rb
... ... @@ -2,9 +2,7 @@ class ProposalsDiscussionPlugin::Topic &lt; ProposalsDiscussionPlugin::ProposalsHo
2 2  
3 3 alias :discussion :parent
4 4  
5   - has_many :proposals, :class_name => 'ProposalsDiscussionPlugin::Proposal', :foreign_key => 'parent_id'
6 5 has_many :proposals_comments, :class_name => 'Comment', :through => :children, :source => :comments
7   - has_many :proposals_authors, :class_name => 'Person', :through => :children, :source => :created_by
8 6  
9 7 settings_items :color, :type => :string
10 8  
... ...
test/functional/proposals_discussion_plugin_myprofile_controller_test.rb
... ... @@ -4,8 +4,8 @@ class ProposalsDiscussionPluginMyprofileControllerTest &lt; ActionController::TestC
4 4  
5 5 def setup
6 6 @profile = fast_create(Community)
7   - @discussion = fast_create(ProposalsDiscussionPlugin::Discussion, :profile_id => @profile.id)
8   - @topic = fast_create(ProposalsDiscussionPlugin::Topic, :parent_id => @discussion.id, :profile_id => @profile.id)
  7 + @discussion = ProposalsDiscussionPlugin::Discussion.create!(:profile => @profile, :name => "Discussion")
  8 + @topic = ProposalsDiscussionPlugin::Topic.create!(:parent => @discussion, :profile => @profile, :name => 'Topic')
9 9 @person = create_user_with_permission('testinguser', 'post_content')
10 10 login_as :testinguser
11 11 end
... ... @@ -28,6 +28,13 @@ class ProposalsDiscussionPluginMyprofileControllerTest &lt; ActionController::TestC
28 28 assert_redirected_to :controller => 'cms', :action => 'new', :type => "ProposalsDiscussionPlugin::Proposal", :parent_id => topic.id
29 29 end
30 30  
  31 + should 'new_proposal with moderated discussion redirect to suggest action in cms controller' do
  32 + discussion.moderate_proposals = true
  33 + discussion.save!
  34 + get :new_proposal, :profile => profile.identifier,:parent_id => topic.id, :discussion_id => discussion.id
  35 + assert_match /suggest_an_article/, @response.location.inspect
  36 + end
  37 +
31 38 should 'publish a proposal' do
32 39 proposal = fast_create(ProposalsDiscussionPlugin::Proposal, :parent_id => topic.id, :profile_id => profile.id, :published => false, :created_by_id => person.id)
33 40 get :publish_proposal, :proposal_id => proposal.id, :profile => profile.identifier
... ...
test/unit/api_test.rb 0 → 100644
... ... @@ -0,0 +1,37 @@
  1 +require_relative '../test_helper'
  2 +require_relative '../../../../test/unit/api/test_helper'
  3 +
  4 +class APITest < ActiveSupport::TestCase
  5 +
  6 + def setup
  7 + login_api
  8 + end
  9 +
  10 + should 'return proposal ranking' do
  11 + begin
  12 + Environment.default.enable_plugin(VotePlugin)
  13 + rescue
  14 + puts 'VotePlugin not enabled'
  15 + return
  16 + end
  17 +
  18 + discussion = fast_create(ProposalsDiscussionPlugin::Discussion, :profile_id => user.person.id)
  19 + topic = fast_create(ProposalsDiscussionPlugin::Topic, :profile_id => user.person.id, :parent_id => discussion.id)
  20 + proposal1 = fast_create(ProposalsDiscussionPlugin::Proposal, :profile_id => user.person.id, :parent_id => topic.id)
  21 + proposal2 = fast_create(ProposalsDiscussionPlugin::Proposal, :profile_id => user.person.id, :parent_id => topic.id)
  22 + proposal3 = fast_create(ProposalsDiscussionPlugin::Proposal, :profile_id => user.person.id, :parent_id => topic.id)
  23 +
  24 + proposal2.update_attribute(:hits, 10)
  25 + 10.times { Vote.create!(:voteable => proposal2, :voter => nil, :vote => 1) }
  26 +
  27 + proposal3.update_attribute(:hits, 10)
  28 + 2.times { Vote.create!(:voteable => proposal3, :voter => nil, :vote => 1) }
  29 +
  30 + proposal1.update_attribute(:hits, 5)
  31 +
  32 + get "/api/v1/proposals_discussion_plugin/#{topic.id}/ranking?#{params.to_query}"
  33 + json = JSON.parse(last_response.body)
  34 + assert_equal [proposal2.id, proposal3.id, proposal1.id], json['proposals'].map {|p| p['id']}
  35 + end
  36 +
  37 +end
... ...