From daa8783b025e529983dc9db1495048e86673cf80 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 30 May 2016 13:53:30 +0200 Subject: [PATCH 1/4] Adds common Investment abilities --- app/models/abilities/common.rb | 4 ++++ spec/models/abilities/common_spec.rb | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb index 6dd36d5b0..ed2c3d4a0 100644 --- a/app/models/abilities/common.rb +++ b/app/models/abilities/common.rb @@ -19,6 +19,7 @@ module Abilities can [:retire_form, :retire], Proposal, author_id: user.id can :read, SpendingProposal + can :read, Budget::Investment can :create, Comment can :create, Debate @@ -46,6 +47,9 @@ module Abilities can :vote_featured, Proposal can :vote, SpendingProposal can :create, SpendingProposal + can :create, Budget::Investment, budget: { phase: "accepting" } + can :vote, Budget::Investment, budget: { phase: "selecting" } + can :ballot, Budget::Investment, budget: { phase: "balloting" } end can :create, Annotation diff --git a/spec/models/abilities/common_spec.rb b/spec/models/abilities/common_spec.rb index 468173797..36b4b49f6 100644 --- a/spec/models/abilities/common_spec.rb +++ b/spec/models/abilities/common_spec.rb @@ -9,6 +9,9 @@ describe "Abilities::Common" do let(:debate) { create(:debate) } let(:comment) { create(:comment) } let(:proposal) { create(:proposal) } + let(:investment_in_accepting_budget) { create(:budget_investment, budget: create(:budget, phase: 'accepting')) } + let(:investment_in_selecting_budget) { create(:budget_investment, budget: create(:budget, phase: 'selecting')) } + let(:investment_in_balloting_budget) { create(:budget_investment, budget: create(:budget, phase: 'balloting')) } let(:own_debate) { create(:debate, author: user) } let(:own_comment) { create(:comment, author: user) } let(:own_proposal) { create(:proposal, author: user) } @@ -93,6 +96,19 @@ describe "Abilities::Common" do it { should be_able_to(:create, SpendingProposal) } it { should_not be_able_to(:destroy, create(:spending_proposal)) } it { should_not be_able_to(:destroy, own_spending_proposal) } + + it { should be_able_to(:create, investment_in_accepting_budget) } + it { should_not be_able_to(:create, investment_in_selecting_budget) } + it { should_not be_able_to(:create, investment_in_balloting_budget) } + + it { should_not be_able_to(:vote, investment_in_accepting_budget) } + it { should be_able_to(:vote, investment_in_selecting_budget) } + it { should_not be_able_to(:vote, investment_in_balloting_budget) } + + it { should_not be_able_to(:ballot, investment_in_accepting_budget) } + it { should_not be_able_to(:ballot, investment_in_selecting_budget) } + it { should be_able_to(:ballot, investment_in_balloting_budget) } + end describe "when level 3 verified" do From 13f43d8b67f57e295ec639635deb154602838b9e Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 30 May 2016 13:55:12 +0200 Subject: [PATCH 2/4] Adds valuator investment abilities --- app/models/abilities/valuator.rb | 4 +++- app/models/valuator.rb | 2 ++ spec/models/abilities/valuator_spec.rb | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/abilities/valuator.rb b/app/models/abilities/valuator.rb index 15add866a..85c598d12 100644 --- a/app/models/abilities/valuator.rb +++ b/app/models/abilities/valuator.rb @@ -3,7 +3,9 @@ module Abilities include CanCan::Ability def initialize(user) + valuator = user.valuator can [:read, :update, :valuate], SpendingProposal + can [:update, :valuate], Budget::Investment, id: valuator.investment_ids, budget: { valuating: true } end end -end \ No newline at end of file +end diff --git a/app/models/valuator.rb b/app/models/valuator.rb index 8b82d20b1..5df6ea030 100644 --- a/app/models/valuator.rb +++ b/app/models/valuator.rb @@ -4,6 +4,8 @@ class Valuator < ActiveRecord::Base has_many :valuation_assignments, dependent: :destroy has_many :spending_proposals, through: :valuation_assignments + has_many :valuator_assignments, dependent: :destroy, class_name: 'Budget::ValuatorAssignment' + has_many :investments, through: :valuator_assignments, class_name: 'Budget::Investment' validates :user_id, presence: true, uniqueness: true diff --git a/spec/models/abilities/valuator_spec.rb b/spec/models/abilities/valuator_spec.rb index ce108200c..9fa285f19 100644 --- a/spec/models/abilities/valuator_spec.rb +++ b/spec/models/abilities/valuator_spec.rb @@ -5,8 +5,24 @@ describe "Abilities::Valuator" do subject(:ability) { Ability.new(user) } let(:user) { valuator.user } let(:valuator) { create(:valuator) } + let(:non_assigned_investment) { create(:budget_investment) } + + let(:assigned_investment) { create(:budget_investment, budget: create(:budget, valuating: true)) } + before(:each) { assigned_investment.valuators << valuator } + + let(:assigned_investment_not_valuating) { create(:budget_investment, budget: create(:budget, valuating: false)) } + before(:each) { assigned_investment_not_valuating.valuators << valuator } it { should be_able_to(:read, SpendingProposal) } it { should be_able_to(:update, SpendingProposal) } it { should be_able_to(:valuate, SpendingProposal) } + + it { should_not be_able_to(:update, non_assigned_investment) } + it { should_not be_able_to(:valuate, non_assigned_investment) } + + it { should be_able_to(:update, assigned_investment) } + it { should be_able_to(:valuate, assigned_investment) } + + it { should_not be_able_to(:update, assigned_investment_not_valuating) } + it { should_not be_able_to(:valuate, assigned_investment_not_valuating) } end From 2a82e9e33ac447f95e32f6db30f7f1779901d1f1 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 30 May 2016 14:01:47 +0200 Subject: [PATCH 3/4] Redoes the common balloting permissions --- app/models/abilities/common.rb | 2 +- spec/models/abilities/common_spec.rb | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb index ed2c3d4a0..0d1bfd4b9 100644 --- a/app/models/abilities/common.rb +++ b/app/models/abilities/common.rb @@ -49,7 +49,7 @@ module Abilities can :create, SpendingProposal can :create, Budget::Investment, budget: { phase: "accepting" } can :vote, Budget::Investment, budget: { phase: "selecting" } - can :ballot, Budget::Investment, budget: { phase: "balloting" } + can :create, Budget::Ballot, budget: { phase: "balloting" } end can :create, Annotation diff --git a/spec/models/abilities/common_spec.rb b/spec/models/abilities/common_spec.rb index 36b4b49f6..6fd751c9f 100644 --- a/spec/models/abilities/common_spec.rb +++ b/spec/models/abilities/common_spec.rb @@ -9,9 +9,15 @@ describe "Abilities::Common" do let(:debate) { create(:debate) } let(:comment) { create(:comment) } let(:proposal) { create(:proposal) } - let(:investment_in_accepting_budget) { create(:budget_investment, budget: create(:budget, phase: 'accepting')) } - let(:investment_in_selecting_budget) { create(:budget_investment, budget: create(:budget, phase: 'selecting')) } - let(:investment_in_balloting_budget) { create(:budget_investment, budget: create(:budget, phase: 'balloting')) } + let(:accepting_budget) { create(:budget, phase: 'accepting') } + let(:selecting_budget) { create(:budget, phase: 'selecting') } + let(:balloting_budget) { create(:budget, phase: 'balloting') } + let(:investment_in_accepting_budget) { create(:budget_investment, budget: accepting_budget) } + let(:investment_in_selecting_budget) { create(:budget_investment, budget: selecting_budget) } + let(:investment_in_balloting_budget) { create(:budget_investment, budget: balloting_budget) } + let(:ballot_in_accepting_budget) { create(:budget_ballot, budget: accepting_budget) } + let(:ballot_in_selecting_budget) { create(:budget_ballot, budget: selecting_budget) } + let(:ballot_in_balloting_budget) { create(:budget_ballot, budget: balloting_budget) } let(:own_debate) { create(:debate, author: user) } let(:own_comment) { create(:comment, author: user) } let(:own_proposal) { create(:proposal, author: user) } @@ -105,9 +111,9 @@ describe "Abilities::Common" do it { should be_able_to(:vote, investment_in_selecting_budget) } it { should_not be_able_to(:vote, investment_in_balloting_budget) } - it { should_not be_able_to(:ballot, investment_in_accepting_budget) } - it { should_not be_able_to(:ballot, investment_in_selecting_budget) } - it { should be_able_to(:ballot, investment_in_balloting_budget) } + it { should_not be_able_to(:create, ballot_in_accepting_budget) } + it { should_not be_able_to(:create, ballot_in_selecting_budget) } + it { should be_able_to(:create, ballot_in_balloting_budget) } end From 7005faabe6c4ba1030935eafadce39bad87b94e5 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 30 May 2016 14:13:36 +0200 Subject: [PATCH 4/4] adds admin permissions for investments --- app/models/abilities/administrator.rb | 8 ++++++-- spec/models/abilities/administrator_spec.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 6b77c130d..b86723c93 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -4,7 +4,6 @@ module Abilities def initialize(user) self.merge Abilities::Moderation.new(user) - self.merge Abilities::Valuator.new(user) can :restore, Comment cannot :restore, Comment, hidden_at: nil @@ -41,7 +40,12 @@ module Abilities can :manage, Annotation - can [:read, :update, :destroy, :summary], SpendingProposal + can [:read, :update, :valuate, :destroy, :summary], SpendingProposal + + can [:create, :update], Budget + can [:hide, :update], Budget::Investment + can :valuate, Budget::Investment, budget: { valuating: true } + can :create, Budget::ValuatorAssignment end end end diff --git a/spec/models/abilities/administrator_spec.rb b/spec/models/abilities/administrator_spec.rb index f9ed7a0c5..8170d3434 100644 --- a/spec/models/abilities/administrator_spec.rb +++ b/spec/models/abilities/administrator_spec.rb @@ -56,4 +56,16 @@ describe "Abilities::Administrator" do it { should be_able_to(:update, SpendingProposal) } it { should be_able_to(:valuate, SpendingProposal) } it { should be_able_to(:destroy, SpendingProposal) } + + it { should be_able_to(:create, Budget) } + it { should be_able_to(:update, Budget) } + + it { should be_able_to(:create, Budget::ValuatorAssignment) } + + it { should be_able_to(:update, Budget::Investment) } + it { should be_able_to(:hide, Budget::Investment) } + + it { should be_able_to(:valuate, create(:budget_investment, budget: create(:budget, valuating: true))) } + it { should_not be_able_to(:valuate, create(:budget_investment, budget: create(:budget, valuating: false))) } + end