diff --git a/app/helpers/signature_sheets_helper.rb b/app/helpers/signature_sheets_helper.rb index acd75a5ab..8e75dccfd 100644 --- a/app/helpers/signature_sheets_helper.rb +++ b/app/helpers/signature_sheets_helper.rb @@ -2,7 +2,7 @@ module SignatureSheetsHelper def signable_options [[t("activerecord.models.proposal", count: 1), Proposal], - [t("activerecord.models.spending_proposal", count: 1), SpendingProposal]] + [t("activerecord.models.budget/investment", count: 1), Budget::Investment]] end end \ No newline at end of file diff --git a/app/models/signature.rb b/app/models/signature.rb index ece75773a..a645fbfb6 100644 --- a/app/models/signature.rb +++ b/app/models/signature.rb @@ -12,35 +12,30 @@ class Signature < ActiveRecord::Base before_validation :clean_document_number - def verified? - user_exists? || in_census? - end - def verify - if verified? - assign_vote - mark_as_verified - end - end - - def assign_vote if user_exists? assign_vote_to_user + mark_as_verified elsif in_census? create_user assign_vote_to_user + mark_as_verified end end def assign_vote_to_user set_user - signable.register_vote(user, "yes") + if signable.is_a? Budget::Investment + signable.vote_by(voter: user, vote: 'yes') if [nil, :no_selecting_allowed].include?(signable.reason_for_not_being_selectable_by(user)) + else + signable.register_vote(user, "yes") + end assign_signature_to_vote end def assign_signature_to_vote vote = Vote.where(votable: signable, voter: user).first - vote.update(signature: self) + vote.update(signature: self) if vote end def user_exists? diff --git a/app/models/signature_sheet.rb b/app/models/signature_sheet.rb index 9720c891f..1434143ac 100644 --- a/app/models/signature_sheet.rb +++ b/app/models/signature_sheet.rb @@ -2,7 +2,7 @@ class SignatureSheet < ActiveRecord::Base belongs_to :signable, polymorphic: true belongs_to :author, class_name: 'User', foreign_key: 'author_id' - VALID_SIGNABLES = %w( Proposal SpendingProposal ) + VALID_SIGNABLES = %w( Proposal Budget::Investment SpendingProposal ) has_many :signatures diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 7d47a8e30..76930d715 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -8,8 +8,8 @@ es: one: "Presupuesto participativo" other: "Presupuestos participativos" budget/investment: - one: "Propuesta de inversión" - other: "Propuestas de inversión" + one: "Proyecto de inversión" + other: "Proyectos de inversión" comment: one: "Comentario" other: "Comentarios" diff --git a/spec/features/admin/signature_sheets_spec.rb b/spec/features/admin/signature_sheets_spec.rb index 8243dd913..bcde7bfc0 100644 --- a/spec/features/admin/signature_sheets_spec.rb +++ b/spec/features/admin/signature_sheets_spec.rb @@ -19,20 +19,42 @@ feature 'Signature sheets' do end end - scenario 'Create' do - proposal = create(:proposal) - visit new_admin_signature_sheet_path + context 'Create' do + scenario 'Proposal' do + proposal = create(:proposal) + visit new_admin_signature_sheet_path - select "Citizen proposal", from: "signature_sheet_signable_type" - fill_in "signature_sheet_signable_id", with: proposal.id - fill_in "signature_sheet_document_numbers", with: "12345678Z, 99999999Z" - click_button "Create signature sheet" + select "Citizen proposal", from: "signature_sheet_signable_type" + fill_in "signature_sheet_signable_id", with: proposal.id + fill_in "signature_sheet_document_numbers", with: "12345678Z, 99999999Z" + click_button "Create signature sheet" - expect(page).to have_content "Signature sheet created successfully" + expect(page).to have_content "Signature sheet created successfully" - visit proposal_path(proposal) + visit proposal_path(proposal) + + expect(page).to have_content "1 support" + end + + scenario 'Budget Investment' do + investment = create(:budget_investment) + budget = investment.budget + budget.update(phase: 'selecting') + + visit new_admin_signature_sheet_path + + select "Investment", from: "signature_sheet_signable_type" + fill_in "signature_sheet_signable_id", with: investment.id + fill_in "signature_sheet_document_numbers", with: "12345678Z, 99999999Z" + click_button "Create signature sheet" + + expect(page).to have_content "Signature sheet created successfully" + + visit budget_investment_path(budget, investment) + + expect(page).to have_content "1 support" + end - expect(page).to have_content "1 support" end scenario 'Errors on create' do diff --git a/spec/models/signature_sheet_spec.rb b/spec/models/signature_sheet_spec.rb index 473a7b96d..aeed78996 100644 --- a/spec/models/signature_sheet_spec.rb +++ b/spec/models/signature_sheet_spec.rb @@ -46,12 +46,20 @@ describe SignatureSheet do expect(signature_sheet.name).to eq("Citizen proposal #{proposal.id}") end + it "returns name for spending proposal signature sheets" do spending_proposal = create(:spending_proposal) signature_sheet.signable = spending_proposal expect(signature_sheet.name).to eq("Spending proposal #{spending_proposal.id}") end + + it "returns name for budget investment signature sheets" do + budget_investment = create(:budget_investment) + signature_sheet.signable = budget_investment + + expect(signature_sheet.name).to eq("Investment #{budget_investment.id}") + end end describe "#verify_signatures" do diff --git a/spec/models/signature_spec.rb b/spec/models/signature_spec.rb index f1c81da19..207208bbf 100644 --- a/spec/models/signature_spec.rb +++ b/spec/models/signature_spec.rb @@ -46,49 +46,61 @@ describe Signature do end end - describe "#verified?" do - - it "returns true if user exists" do - user = create(:user, :level_two, document_number: "123A") - signature = create(:signature, document_number: user.document_number) - - expect(signature.verified?).to eq(true) - end - - it "returns true if document number in census" do - signature = create(:signature, document_number: "12345678Z") - - expect(signature.verified?).to eq(true) - end - - it "returns false if user does not exist and not in census" do - signature = create(:signature, document_number: "123A") - - expect(signature.verified?).to eq(false) - end - - end - - describe "#assign_vote" do + describe "#verify" do describe "existing user" do - it "assigns vote to user" do + it "assigns vote to user on proposal" do user = create(:user, :level_two, document_number: "123A") signature = create(:signature, document_number: user.document_number) proposal = signature.signable - signature.assign_vote + signature.verify expect(user.voted_for?(proposal)).to be end + it "assigns vote to user on budget investment" do + investment = create(:budget_investment) + signature_sheet = create(:signature_sheet, signable: investment) + user = create(:user, :level_two, document_number: "123A") + signature = create(:signature, document_number: user.document_number, signature_sheet: signature_sheet) + + signature.verify + + expect(user.voted_for?(investment)).to be + end + it "does not assign vote to user multiple times" do user = create(:user, :level_two, document_number: "123A") signature = create(:signature, document_number: user.document_number) - signature.assign_vote - signature.assign_vote + signature.verify + signature.verify + + expect(Vote.count).to eq(1) + end + + it "does not assigns vote to invalid user on budget investment" do + investment = create(:budget_investment) + signature_sheet = create(:signature_sheet, signable: investment) + user = create(:user, document_number: "123A") + signature = create(:signature, document_number: user.document_number, signature_sheet: signature_sheet) + + signature.verify + + expect(user.voted_for?(investment)).to_not be + expect(Vote.count).to eq(0) + end + + it "does not assign vote to user multiple times on budget investment" do + investment = create(:budget_investment) + signature_sheet = create(:signature_sheet, signable: investment) + user = create(:user, :level_two, document_number: "123A") + signature = create(:signature, document_number: user.document_number, signature_sheet: signature_sheet) + + signature.verify + signature.verify expect(Vote.count).to eq(1) end @@ -100,7 +112,22 @@ describe Signature do signature_sheet = create(:signature_sheet, signable: proposal) signature = create(:signature, signature_sheet: signature_sheet, document_number: user.document_number) - signature.assign_vote + signature.verify + + expect(Vote.count).to eq(1) + end + + it "does not assign vote to user if already voted on budget investment" do + investment = create(:budget_investment) + user = create(:user, :level_two, document_number: "123A") + vote = create(:vote, votable: investment, voter: user) + + signature_sheet = create(:signature_sheet, signable: investment) + signature = create(:signature, document_number: user.document_number, signature_sheet: signature_sheet) + + expect(Vote.count).to eq(1) + + signature.verify expect(Vote.count).to eq(1) end @@ -108,7 +135,7 @@ describe Signature do it "marks the vote as coming from a signature" do signature = create(:signature, document_number: "12345678Z") - signature.assign_vote + signature.verify expect(Vote.last.signature).to eq(signature) end @@ -122,7 +149,7 @@ describe Signature do signature = create(:signature, document_number: "12345678Z") proposal = signature.signable - signature.assign_vote + signature.verify user = User.last expect(user.document_number).to eq("12345678Z") @@ -138,7 +165,7 @@ describe Signature do signature = create(:signature, document_number: "12345678Z") proposal = signature.signable - signature.assign_vote + signature.verify user = signature.user expect(user.voted_for?(proposal)).to be @@ -147,22 +174,18 @@ describe Signature do it "assigns signature to vote" do signature = create(:signature, document_number: "12345678Z") - signature.assign_vote + signature.verify expect(Vote.last.signature).to eq(signature) end end - end - - describe "#verify" do - describe "document in census" do - it "calls assign_vote" do + it "calls assign_vote_to_user" do signature = create(:signature, document_number: "12345678Z") - expect(signature).to receive(:assign_vote) + expect(signature).to receive(:assign_vote_to_user) signature.verify end @@ -179,10 +202,10 @@ describe Signature do describe "document not in census" do - it "does not call assign_vote" do + it "does not call assign_vote_to_user" do signature = create(:signature, document_number: "123A") - expect(signature).to_not receive(:assign_vote) + expect(signature).to_not receive(:assign_vote_to_user) signature.verify end