Skip to content

Instantly share code, notes, and snippets.

@jvans1
Forked from dhh/ticket.rb
Last active August 29, 2015 13:57

Revisions

  1. jvans1 revised this gist Mar 25, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ticket.rb
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@ def initialize(ticket)
    end

    def can_confirm?
    confirmation_errors.clear
    run_confirmations
    confirm_errors.any?
    end
  2. jvans1 revised this gist Mar 25, 2014. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions ticket.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    class Ticket < ActiveRecord::Base
    class TicketConfirmer
    attr_reader :ticket
    attr_reader :ticket, :confirmation_errors
    delegate :user, :grouper :to => :ticket
    def initialize(ticket)
    @ticket = ticket
    @@ -12,10 +12,6 @@ def can_confirm?
    confirm_errors.any?
    end

    def confirmation_errors
    @confirmation_errors
    end

    private
    def run_confirmations
    confirmation_errors << [:user, "can't book a Grouper at this time"] if user.blacklisted?
  3. jvans1 revised this gist Mar 25, 2014. 1 changed file with 0 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions ticket.rb
    Original file line number Diff line number Diff line change
    @@ -29,15 +29,6 @@ def run_confirmations
    belongs_to :user

    validate :can_confirm , :on => :confirmation

    def confirm
    update confirmed: true if confirmable?
    end

    def confirmable?
    valid? :confirmation
    end


    private
    def can_confirm
  4. jvans1 revised this gist Mar 25, 2014. 1 changed file with 33 additions and 25 deletions.
    58 changes: 33 additions & 25 deletions ticket.rb
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,34 @@
    class Ticket < ActiveRecord::Base
    belongs_to :grouper
    belongs_to :user
    class TicketConfirmer
    attr_reader :ticket
    delegate :user, :grouper :to => :ticket
    def initialize(ticket)
    @ticket = ticket
    @confirmation_errors = []
    end

    validate :user_cant_be_blacklisted, on: :confirmation
    validate :user_cant_double_book, on: :confirmation
    def can_confirm?
    run_confirmations
    confirm_errors.any?
    end

    validate :grouper_cant_be_full, on: :confirmation
    validate :grouper_cant_have_occurred, on: :confirmation
    def confirmation_errors
    @confirmation_errors
    end

    validate :ticket_cant_have_been_confirmed, on: :confirmation
    private
    def run_confirmations
    confirmation_errors << [:user, "can't book a Grouper at this time"] if user.blacklisted?
    confirmation_errors << [:user, 'are already going to a Grouper on that day'] if user.has_existing_grouper?(grouper)
    confirmation_errors << [:grouper, 'has already occurred!'] if grouper.full?
    confirmation_errors << [:grouper, 'has already occurred!'] if grouper.past?
    confirmation_errors << [:user, 'have already confirmed this ticket'] if confirmed?
    end
    end
    belongs_to :grouper
    belongs_to :user

    validate :can_confirm , :on => :confirmation

    def confirm
    update confirmed: true if confirmable?
    @@ -21,23 +40,12 @@ def confirmable?


    private
    def user_cant_be_blacklisted
    errors.add :user, "can't book a Grouper at this time" if user.blacklisted?
    end

    def user_cant_double_book
    errors.add :user, 'are already going to a Grouper on that day' if user.has_existing_grouper?(grouper)
    end

    def grouper_cant_be_full
    errors.add :grouper, 'has already occurred!' if grouper.full?
    end

    def grouper_cant_have_occurred
    errors.add :grouper, 'has already occurred!' if grouper.past?
    end

    def ticket_cant_have_been_confirmed
    errors.add :user, 'have already confirmed this ticket' if confirmed?
    def can_confirm
    if (ticket_confirmer = TicketConfirmer.new(self)).can_confirm?
    ticket_confirmer.confirmation_errors.each do |object, error|
    errors.add(object, error)
    end
    end
    end

    end
  5. @dhh dhh created this gist Mar 20, 2014.
    43 changes: 43 additions & 0 deletions ticket.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    class Ticket < ActiveRecord::Base
    belongs_to :grouper
    belongs_to :user

    validate :user_cant_be_blacklisted, on: :confirmation
    validate :user_cant_double_book, on: :confirmation

    validate :grouper_cant_be_full, on: :confirmation
    validate :grouper_cant_have_occurred, on: :confirmation

    validate :ticket_cant_have_been_confirmed, on: :confirmation


    def confirm
    update confirmed: true if confirmable?
    end

    def confirmable?
    valid? :confirmation
    end


    private
    def user_cant_be_blacklisted
    errors.add :user, "can't book a Grouper at this time" if user.blacklisted?
    end

    def user_cant_double_book
    errors.add :user, 'are already going to a Grouper on that day' if user.has_existing_grouper?(grouper)
    end

    def grouper_cant_be_full
    errors.add :grouper, 'has already occurred!' if grouper.full?
    end

    def grouper_cant_have_occurred
    errors.add :grouper, 'has already occurred!' if grouper.past?
    end

    def ticket_cant_have_been_confirmed
    errors.add :user, 'have already confirmed this ticket' if confirmed?
    end
    end