From ab008ed4e9ffad4b7309bfa01468def7623d70cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sat, 11 Jun 2016 14:57:07 +0200 Subject: [PATCH] adds denormalization to budget::ballot::lines --- app/models/budget/ballot/line.rb | 5 +++ ...20160610094658_desnormalize_ballot_line.rb | 7 ++++ db/schema.rb | 5 ++- spec/factories.rb | 8 +++- spec/models/budget/ballot/line_spec.rb | 40 +++++++++++++++++++ 5 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20160610094658_desnormalize_ballot_line.rb create mode 100644 spec/models/budget/ballot/line_spec.rb diff --git a/app/models/budget/ballot/line.rb b/app/models/budget/ballot/line.rb index 804ee3a2c..54b5e221f 100644 --- a/app/models/budget/ballot/line.rb +++ b/app/models/budget/ballot/line.rb @@ -2,7 +2,12 @@ class Budget class Ballot class Line < ActiveRecord::Base belongs_to :ballot + belongs_to :budget + belongs_to :group + belongs_to :heading belongs_to :investment + + validates :ballot_id, :budget_id, :group_id, :heading_id, :investment_id, presence: true end end end diff --git a/db/migrate/20160610094658_desnormalize_ballot_line.rb b/db/migrate/20160610094658_desnormalize_ballot_line.rb new file mode 100644 index 000000000..b4eee7dc2 --- /dev/null +++ b/db/migrate/20160610094658_desnormalize_ballot_line.rb @@ -0,0 +1,7 @@ +class DesnormalizeBallotLine < ActiveRecord::Migration + def change + add_column :budget_ballot_lines, :budget_id, :integer, index: true + add_column :budget_ballot_lines, :group_id, :integer, index: true + add_column :budget_ballot_lines, :heading_id, :integer, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 8cd96edfd..5eb4435e7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160609152026) do +ActiveRecord::Schema.define(version: 20160610094658) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -83,6 +83,9 @@ ActiveRecord::Schema.define(version: 20160609152026) do t.integer "investment_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "budget_id" + t.integer "group_id" + t.integer "heading_id" end add_index "budget_ballot_lines", ["ballot_id"], name: "index_budget_ballot_lines_on_ballot_id", using: :btree diff --git a/spec/factories.rb b/spec/factories.rb index d20cfa84f..fe06ffe5f 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -222,6 +222,7 @@ FactoryGirl.define do association :heading, factory: :budget_heading association :author, factory: :user description 'Spend money on this' + price 10 unfeasibility_explanation '' external_url 'http://external_documention.org' terms_of_service '1' @@ -249,8 +250,11 @@ FactoryGirl.define do end factory :budget_ballot_line, class: 'Budget::Ballot::Line' do - association :ballot, factory: :budget_ballot - investment { FactoryGirl.build(:budget_investment, :feasible) } + budget + ballot { create :budget_ballot, budget: budget } + group { create :budget_group, budget: budget } + heading { create :budget_heading, group: group } + investment { create :budget_investment, :feasible, heading: heading } end factory :vote do diff --git a/spec/models/budget/ballot/line_spec.rb b/spec/models/budget/ballot/line_spec.rb new file mode 100644 index 000000000..f18ec3dc9 --- /dev/null +++ b/spec/models/budget/ballot/line_spec.rb @@ -0,0 +1,40 @@ +require 'rails_helper' + +describe "Budget::Ballot::Line" do + + let(:ballot_line) { build(:budget_ballot_line) } + + describe 'Validations' do + + it "should be valid" do + expect(ballot_line).to be_valid + end + + it "should be invalid if missing id from ballot|budget|group|heading|investment" do + budget = create(:budget) + group = create(:budget_group, budget: budget) + heading = create(:budget_heading, group: group, price: 10000000) + investment = create(:budget_investment, :feasible, price: 5000000, heading: heading) + ballot = create(:budget_ballot, budget: budget) + + ballot_line = build(:budget_ballot_line, ballot: ballot, budget: budget, group: group, heading: heading, investment: investment) + expect(ballot_line).to be_valid + + ballot_line = build(:budget_ballot_line, ballot: nil, budget: budget, group: group, heading: heading, investment: investment) + expect(ballot_line).to_not be_valid + + ballot_line = build(:budget_ballot_line, ballot: ballot, budget: nil, group: group, heading: heading, investment: investment) + expect(ballot_line).to_not be_valid + + ballot_line = build(:budget_ballot_line, ballot: ballot, budget: budget, group: nil, heading: heading, investment: investment) + expect(ballot_line).to_not be_valid + + ballot_line = build(:budget_ballot_line, ballot: ballot, budget: budget, group: group, heading: nil, investment: investment) + expect(ballot_line).to_not be_valid + + ballot_line = build(:budget_ballot_line, ballot: ballot, budget: budget, group: group, heading: heading, investment: nil) + expect(ballot_line).to_not be_valid + end + + end +end \ No newline at end of file