From cc4f1e9c13b9d13953e2fcf135a8ede65ff7b876 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Fri, 17 Apr 2015 02:01:28 +0300 Subject: [PATCH] Introduce the Proposition model Introduce an abstraction that is going to be used to propose models for other models that can accept propositions. --- app/models/proposition.rb | 5 +++++ .../20141105100105_create_propositions.rb | 13 ++++++++++++ ...create_propositions_for_existing_events.rb | 15 +++++++++++++ spec/factories/events.rb | 8 +++++++ spec/factories/propositions.rb | 7 +++++++ spec/models/proposition_spec.rb | 21 +++++++++++++++++++ 6 files changed, 69 insertions(+) create mode 100644 app/models/proposition.rb create mode 100644 db/migrate/20141105100105_create_propositions.rb create mode 100644 db/migrate/20141105100827_create_propositions_for_existing_events.rb create mode 100644 spec/factories/events.rb create mode 100644 spec/factories/propositions.rb create mode 100644 spec/models/proposition_spec.rb diff --git a/app/models/proposition.rb b/app/models/proposition.rb new file mode 100644 index 0000000..0ae055d --- /dev/null +++ b/app/models/proposition.rb @@ -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 diff --git a/db/migrate/20141105100105_create_propositions.rb b/db/migrate/20141105100105_create_propositions.rb new file mode 100644 index 0000000..09d62e4 --- /dev/null +++ b/db/migrate/20141105100105_create_propositions.rb @@ -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 diff --git a/db/migrate/20141105100827_create_propositions_for_existing_events.rb b/db/migrate/20141105100827_create_propositions_for_existing_events.rb new file mode 100644 index 0000000..0dddf10 --- /dev/null +++ b/db/migrate/20141105100827_create_propositions_for_existing_events.rb @@ -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 diff --git a/spec/factories/events.rb b/spec/factories/events.rb new file mode 100644 index 0000000..c690e32 --- /dev/null +++ b/spec/factories/events.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :event do + title { |n| "Event #{n}" } + length { 60 } + abstract { 'foo' } + description { 'foo' } + end +end diff --git a/spec/factories/propositions.rb b/spec/factories/propositions.rb new file mode 100644 index 0000000..72287fc --- /dev/null +++ b/spec/factories/propositions.rb @@ -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 diff --git a/spec/models/proposition_spec.rb b/spec/models/proposition_spec.rb new file mode 100644 index 0000000..0d512f3 --- /dev/null +++ b/spec/models/proposition_spec.rb @@ -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