diff --git a/Gemfile b/Gemfile index 2b0f4d2d7..dab801514 100644 --- a/Gemfile +++ b/Gemfile @@ -30,6 +30,8 @@ gem 'omniauth-github' gem 'omniauth-twitter' gem "omniauth-rails_csrf_protection", "~> 2.0" +gem 'stateful_enum' + gem 'actionview-encoded_mail_to' gem 'active_model_serializers', '~> 0.10.16' gem 'bootsnap', '~> 1.20', require: false diff --git a/Gemfile.lock b/Gemfile.lock index f36ac69ba..8643d0ba1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -428,6 +428,7 @@ GEM snaky_hash (2.0.1) hashie version_gem (~> 1.1, >= 1.1.1) + stateful_enum (0.7.0) stringio (3.2.0) temple (0.10.4) thor (1.4.0) @@ -508,6 +509,7 @@ DEPENDENCIES sendgrid-ruby sidekiq simple_form + stateful_enum turbo-rails RUBY VERSION diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 995616faf..b2e0459d2 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -1,12 +1,9 @@ class Invitation < ApplicationRecord - enum :state, {pending: 'pending', accepted: 'accepted', declined: 'declined'} + enum :state, {pending: 'pending', accepted: 'accepted', declined: 'declined'}, default: :pending belongs_to :proposal belongs_to :user, optional: true - scope :not_accepted, -> { where(state: [:pending, :declined]) } - - before_create :set_default_state before_create :set_slug validates :email, presence: true @@ -30,10 +27,6 @@ def decline def set_slug self.slug = Digest::SHA1.hexdigest([email, rand(1000)].map(&:to_s).join('-'))[0, 10] end - - def set_default_state - self.state = :pending if state.nil? - end end # == Schema Information diff --git a/app/models/program_session.rb b/app/models/program_session.rb index 044c02d4f..77f3f108f 100644 --- a/app/models/program_session.rb +++ b/app/models/program_session.rb @@ -6,7 +6,22 @@ class ProgramSession < ApplicationRecord unconfirmed_waitlisted: 'unconfirmed waitlisted', confirmed_waitlisted: 'confirmed waitlisted', declined: 'declined' - }, default: :draft + }, default: :draft do + event :confirm do + transition :unconfirmed_waitlisted => :confirmed_waitlisted + transition :unconfirmed_accepted => :live + end + + event :promote do + before do + proposal.promote if proposal + end + + transition :draft => :live + transition :unconfirmed_waitlisted => :unconfirmed_accepted + transition :confirmed_waitlisted => :live + end + end STATE_GROUPS = { live: 'program', @@ -17,16 +32,6 @@ class ProgramSession < ApplicationRecord declined: 'declined' }.with_indifferent_access - PROMOTIONS = { - draft: :live, - unconfirmed_waitlisted: :unconfirmed_accepted, - confirmed_waitlisted: :live - }.with_indifferent_access - - CONFIRMATIONS = { - unconfirmed_waitlisted: :confirmed_waitlisted, - unconfirmed_accepted: :live - }.with_indifferent_access belongs_to :event belongs_to :proposal, optional: true @@ -82,25 +87,6 @@ def self.create_from_proposal(proposal) end end - def can_confirm? - CONFIRMATIONS.key?(state) - end - - def confirm - update(state: CONFIRMATIONS[state]) if can_confirm? - end - - def can_promote? - PROMOTIONS.key?(state) - end - - def promote - if proposal.present? - proposal.promote - end - update(state: PROMOTIONS[state]) - end - def multiple_speakers? speakers.count > 1 end diff --git a/spec/models/program_session_spec.rb b/spec/models/program_session_spec.rb index 5efb0a5b4..8e75f47ba 100644 --- a/spec/models/program_session_spec.rb +++ b/spec/models/program_session_spec.rb @@ -189,8 +189,7 @@ end it "promotes it's proposal" do - ps = create(:program_session, proposal: proposal, track: proposal.track) - proposal = create(:proposal_with_track, program_session: ps) + ps = create(:program_session, state: :draft, proposal: proposal, track: proposal.track) expect(proposal).to receive(:promote) ps.promote