Introduce the Proposition model

Introduce an abstraction that is going to be used to propose models for
other models that can accept propositions.
This commit is contained in:
Petko Bordjukov 2015-04-17 02:01:28 +03:00
parent 408457a765
commit cc4f1e9c13
6 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,5 @@
class Proposition < ActiveRecord::Base
belongs_to :proposer, class_name: 'User'
belongs_to :proposition_accepting, polymorphic: true
belongs_to :proposable, polymorphic: true
end

View File

@ -0,0 +1,13 @@
class CreatePropositions < ActiveRecord::Migration
def change
create_table :propositions do |t|
t.references :proposer, index: true
t.references :proposition_accepting, polymorphic: true
t.index [:proposition_accepting_id, :proposition_accepting_type], name: 'proposition_accepting_index'
t.references :proposable, polymorphic: true, index: true
t.integer :status
t.timestamps
end
end
end

View File

@ -0,0 +1,15 @@
class CreatePropositionsForExistingEvents < ActiveRecord::Migration
def up
events = execute 'SELECT * FROM events'
events.each do |event|
execute "INSERT INTO propositions (proposer_id, proposition_accepting_id, proposition_accepting_type, proposable_id, proposable_type, status, created_at, updated_at)
VALUES (#{event['user_id']}, #{event['track_id']}, 'Track', #{event['id']}, 'Event', #{event['state']}, '#{event['created_at']}', '#{event['updated_at']}')"
end
end
def down
event_ids = execute('SELECT * FROM events').map { |event| event['id'] }
execute "DELETE FROM propositions WHERE proposable_id IN (#{event_ids.join(', ')}) AND proposable_type = 'Event'"
end
end

8
spec/factories/events.rb Normal file
View File

@ -0,0 +1,8 @@
FactoryGirl.define do
factory :event do
title { |n| "Event #{n}" }
length { 60 }
abstract { 'foo' }
description { 'foo' }
end
end

View File

@ -0,0 +1,7 @@
FactoryGirl.define do
factory :proposition do
association :proposer, factory: :user
association :proposition_accepting, factory: :track
association :proposable, factory: :event
end
end

View File

@ -0,0 +1,21 @@
require 'rails_helper'
RSpec.describe Proposition, type: :model do
it 'belongs to a proposer' do
user = create :user
expect(create(:proposition, proposer: user).proposer).to eq user
end
it 'belongs to a proposition_accepting' do
track = create :track
expect(create(:proposition, proposition_accepting: track).proposition_accepting).to eq track
end
it 'belongs to a proposable' do
event = create :event
expect(create(:proposition, proposable: event).proposable).to eq event
end
it 'is invalid without an existing proposition_accepting'
it 'is invalid without an existing proposable'
end