adds denormalization to budget::ballot::lines

This commit is contained in:
Juanjo Bazán
2016-06-11 14:57:07 +02:00
parent e849357e57
commit ab008ed4e9
5 changed files with 62 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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