From 9ca34c5a22fc73b78749ee774a8a5efaf5f36e27 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Wed, 29 Jul 2015 17:54:41 +0300 Subject: [PATCH] Introduce a Participation model This model is responsible for the 1..n - 1..n relationship between an event and the people that will participate in it. --- app/models/concerns/proposable.rb | 5 +---- app/models/conference.rb | 4 ++-- app/models/event.rb | 6 ++++++ app/models/participation.rb | 7 +++++++ .../20150729135346_create_participations.rb | 11 +++++++++++ ...50729135818_create_participation_records.rb | 18 ++++++++++++++++++ ..._drop_join_table_events_sepaker_profiles.rb | 5 +++++ spec/factories/participations.rb | 7 +++++++ 8 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 app/models/participation.rb create mode 100644 db/migrate/20150729135346_create_participations.rb create mode 100644 db/migrate/20150729135818_create_participation_records.rb create mode 100644 db/migrate/20150729145831_drop_join_table_events_sepaker_profiles.rb create mode 100644 spec/factories/participations.rb diff --git a/app/models/concerns/proposable.rb b/app/models/concerns/proposable.rb index c6c287e..c1f35e7 100644 --- a/app/models/concerns/proposable.rb +++ b/app/models/concerns/proposable.rb @@ -1,12 +1,9 @@ module Proposable extend ActiveSupport::Concern - def proposer - proposition.proposer - end - included do has_one :proposition, as: :proposable + has_one :proposer, through: :proposition Proposition.defined_enums["status"].keys.each do |status| scope status.to_sym, -> { joins(:proposition).where(propositions: {status: Proposition.defined_enums["status"][status]}) } diff --git a/app/models/conference.rb b/app/models/conference.rb index c502157..ca498a9 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -10,10 +10,10 @@ class Conference < ActiveRecord::Base has_many :tracks has_many :halls - has_many :events, through: :tracks has_many :event_types + has_many :events, through: :tracks has_one :call_for_participation, dependent: :destroy - has_many :participant_profiles, class_name: 'PersonalProfile' + has_many :participants, class_name: 'User', through: :events accepts_nested_attributes_for :tracks, :halls, :event_types, reject_if: :all_blank, allow_destroy: true diff --git a/app/models/event.rb b/app/models/event.rb index c1c1f2c..3206c86 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -3,6 +3,12 @@ class Event < ActiveRecord::Base has_one :track, through: :proposition, source: :proposition_accepting, source_type: Track has_one :conference, through: :track + + has_many :participations + has_many :pending_participations, ->() { pending }, class_name: 'Participation' + has_many :approved_participations, ->() { approved }, class_name: 'Participation' + has_many :participants, through: :approved_participations + belongs_to :event_type validates :title, presence: true diff --git a/app/models/participation.rb b/app/models/participation.rb new file mode 100644 index 0000000..0e0b21f --- /dev/null +++ b/app/models/participation.rb @@ -0,0 +1,7 @@ +class Participation < ActiveRecord::Base + belongs_to :participant, class_name: User + belongs_to :event + + scope :approved, ->() { where approved: true } + scope :pending, ->() { where.not approved: true } +end diff --git a/db/migrate/20150729135346_create_participations.rb b/db/migrate/20150729135346_create_participations.rb new file mode 100644 index 0000000..e6ed3c2 --- /dev/null +++ b/db/migrate/20150729135346_create_participations.rb @@ -0,0 +1,11 @@ +class CreateParticipations < ActiveRecord::Migration + def change + create_table :participations do |t| + t.references :participant, index: true, foreign_key: true + t.references :event, index: true, foreign_key: true + t.boolean :approved, default: false + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20150729135818_create_participation_records.rb b/db/migrate/20150729135818_create_participation_records.rb new file mode 100644 index 0000000..33c395e --- /dev/null +++ b/db/migrate/20150729135818_create_participation_records.rb @@ -0,0 +1,18 @@ +class Participation < ActiveRecord::Base; end +class PersonalProfile < ActiveRecord::Base; end + +class CreateParticipationRecords < ActiveRecord::Migration + def up + event_to_speaker_profiles = execute 'SELECT * FROM events_speaker_profiles' + + event_to_speaker_profiles.each do |event_to_speaker_profile| + Participation.create! event_id: event_to_speaker_profile['event_id'], + participant_id: PersonalProfile.find(event_to_speaker_profile['speaker_profile_id']).user_id, + approved: true + end + end + + def down + Participation.destroy_all + end +end diff --git a/db/migrate/20150729145831_drop_join_table_events_sepaker_profiles.rb b/db/migrate/20150729145831_drop_join_table_events_sepaker_profiles.rb new file mode 100644 index 0000000..f04b11f --- /dev/null +++ b/db/migrate/20150729145831_drop_join_table_events_sepaker_profiles.rb @@ -0,0 +1,5 @@ +class DropJoinTableEventsSepakerProfiles < ActiveRecord::Migration + def up + drop_table :events_speaker_profiles + end +end diff --git a/spec/factories/participations.rb b/spec/factories/participations.rb new file mode 100644 index 0000000..2b1cd25 --- /dev/null +++ b/spec/factories/participations.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :participation do + association :participant, factory: :user + event + approved false + end +end