Class: Ticket

Inherits:
ApplicationRecord show all
Defined in:
app/models/ticket.rb

Overview

This model manage the Happening Ticket for each User

Relations

belongs to Happening belongs to User

Validates

after save

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#by_editorObject

Returns the value of attribute by_editor.


33
34
35
# File 'app/models/ticket.rb', line 33

def by_editor
  @by_editor
end

#created_atDateTime

Returns when the record was created.

Returns:

  • (DateTime)

    when the record was created


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/ticket.rb', line 28

class Ticket < ApplicationRecord
  belongs_to :happening, counter_cache: true
  belongs_to :user
  delegate :fact, :fact_id, :max_seats, :max_seats_for_ticket, :saleable?, :seats_count, :start_at, :update_seats_count!, to: :happening, allow_nil: true

  attr_accessor :by_editor

  validates :happening, presence: true
  validates :user, presence: true, uniqueness: { scope: :happening_id }
  validates :seats, presence: true
  validates :seats, numericality: { less_than_or_equal_to: :max_seats_for_ticket }, unless: :by_editor?
  validate  :validate_saleability, unless: :by_editor?
  validate  :validate_total_seats, unless: :by_editor?
  validate  :validate_frequency, unless: :by_editor?

  after_commit :update_seats_count!

  # @return [Boolean] true if by_editor is true
  def by_editor?
    by_editor == true
  end

  private

  # Add an error unless {Happening.saleable?} is true
  def validate_saleability
    errors.add(:seats, 'Posti non assegnabili') unless saleable?
  end

  # Add error unless requested seats are minor than available seatc
  def validate_total_seats
    errors.add(:seats, 'Posti non disponibili') if seats_count + seats.to_i > max_seats
  end

  # check {User}'s ticket presence based of {Fact#tickets_frequency}
  def validate_frequency
    exist, message = case fact.tickets_frequency
                     when 'single'
                       [fact_ticket_exist?, I18n.t('site.ticket.errors.single')]
                     when 'daily'
                       [fact_ticket_exist?(start_at.beginning_of_day..start_at.end_of_day), I18n.t('site.ticket.errors.daily')]
                     when 'weekly'
                       [fact_ticket_exist?(start_at.-(7.days)..start_at.+(7.days)), I18n.t('site.ticket.errors.weekly')]
                     when 'monthly'
                       [fact_ticket_exist?(start_at.-(30.days)..start_at.+(30.days)), I18n.t('site.ticket.errors.montly')]
                     else
                       return true
                     end
    errors.add(:seats, message) if exist
  end

  # check if exists {Fact}'s {Ticket} for {User} in a time period
  def fact_ticket_exist?(period = nil)
    @when = { happenings: { start_at: period } } if period.present?
    fact.tickets.where(@when).where(user: user).exists?
  end
end

#happening_idInteger

Returns identifier of related Happening.

Returns:

  • (Integer)

    identifier of related Happening


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/ticket.rb', line 28

class Ticket < ApplicationRecord
  belongs_to :happening, counter_cache: true
  belongs_to :user
  delegate :fact, :fact_id, :max_seats, :max_seats_for_ticket, :saleable?, :seats_count, :start_at, :update_seats_count!, to: :happening, allow_nil: true

  attr_accessor :by_editor

  validates :happening, presence: true
  validates :user, presence: true, uniqueness: { scope: :happening_id }
  validates :seats, presence: true
  validates :seats, numericality: { less_than_or_equal_to: :max_seats_for_ticket }, unless: :by_editor?
  validate  :validate_saleability, unless: :by_editor?
  validate  :validate_total_seats, unless: :by_editor?
  validate  :validate_frequency, unless: :by_editor?

  after_commit :update_seats_count!

  # @return [Boolean] true if by_editor is true
  def by_editor?
    by_editor == true
  end

  private

  # Add an error unless {Happening.saleable?} is true
  def validate_saleability
    errors.add(:seats, 'Posti non assegnabili') unless saleable?
  end

  # Add error unless requested seats are minor than available seatc
  def validate_total_seats
    errors.add(:seats, 'Posti non disponibili') if seats_count + seats.to_i > max_seats
  end

  # check {User}'s ticket presence based of {Fact#tickets_frequency}
  def validate_frequency
    exist, message = case fact.tickets_frequency
                     when 'single'
                       [fact_ticket_exist?, I18n.t('site.ticket.errors.single')]
                     when 'daily'
                       [fact_ticket_exist?(start_at.beginning_of_day..start_at.end_of_day), I18n.t('site.ticket.errors.daily')]
                     when 'weekly'
                       [fact_ticket_exist?(start_at.-(7.days)..start_at.+(7.days)), I18n.t('site.ticket.errors.weekly')]
                     when 'monthly'
                       [fact_ticket_exist?(start_at.-(30.days)..start_at.+(30.days)), I18n.t('site.ticket.errors.montly')]
                     else
                       return true
                     end
    errors.add(:seats, message) if exist
  end

  # check if exists {Fact}'s {Ticket} for {User} in a time period
  def fact_ticket_exist?(period = nil)
    @when = { happenings: { start_at: period } } if period.present?
    fact.tickets.where(@when).where(user: user).exists?
  end
end

#idInteger

Returns unique identifier for Ticket.

Returns:

  • (Integer)

    unique identifier for Ticket


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/ticket.rb', line 28

class Ticket < ApplicationRecord
  belongs_to :happening, counter_cache: true
  belongs_to :user
  delegate :fact, :fact_id, :max_seats, :max_seats_for_ticket, :saleable?, :seats_count, :start_at, :update_seats_count!, to: :happening, allow_nil: true

  attr_accessor :by_editor

  validates :happening, presence: true
  validates :user, presence: true, uniqueness: { scope: :happening_id }
  validates :seats, presence: true
  validates :seats, numericality: { less_than_or_equal_to: :max_seats_for_ticket }, unless: :by_editor?
  validate  :validate_saleability, unless: :by_editor?
  validate  :validate_total_seats, unless: :by_editor?
  validate  :validate_frequency, unless: :by_editor?

  after_commit :update_seats_count!

  # @return [Boolean] true if by_editor is true
  def by_editor?
    by_editor == true
  end

  private

  # Add an error unless {Happening.saleable?} is true
  def validate_saleability
    errors.add(:seats, 'Posti non assegnabili') unless saleable?
  end

  # Add error unless requested seats are minor than available seatc
  def validate_total_seats
    errors.add(:seats, 'Posti non disponibili') if seats_count + seats.to_i > max_seats
  end

  # check {User}'s ticket presence based of {Fact#tickets_frequency}
  def validate_frequency
    exist, message = case fact.tickets_frequency
                     when 'single'
                       [fact_ticket_exist?, I18n.t('site.ticket.errors.single')]
                     when 'daily'
                       [fact_ticket_exist?(start_at.beginning_of_day..start_at.end_of_day), I18n.t('site.ticket.errors.daily')]
                     when 'weekly'
                       [fact_ticket_exist?(start_at.-(7.days)..start_at.+(7.days)), I18n.t('site.ticket.errors.weekly')]
                     when 'monthly'
                       [fact_ticket_exist?(start_at.-(30.days)..start_at.+(30.days)), I18n.t('site.ticket.errors.montly')]
                     else
                       return true
                     end
    errors.add(:seats, message) if exist
  end

  # check if exists {Fact}'s {Ticket} for {User} in a time period
  def fact_ticket_exist?(period = nil)
    @when = { happenings: { start_at: period } } if period.present?
    fact.tickets.where(@when).where(user: user).exists?
  end
end

#seatsInteger

Returns number of seats for this Ticket.

Returns:

  • (Integer)

    number of seats for this Ticket


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/ticket.rb', line 28

class Ticket < ApplicationRecord
  belongs_to :happening, counter_cache: true
  belongs_to :user
  delegate :fact, :fact_id, :max_seats, :max_seats_for_ticket, :saleable?, :seats_count, :start_at, :update_seats_count!, to: :happening, allow_nil: true

  attr_accessor :by_editor

  validates :happening, presence: true
  validates :user, presence: true, uniqueness: { scope: :happening_id }
  validates :seats, presence: true
  validates :seats, numericality: { less_than_or_equal_to: :max_seats_for_ticket }, unless: :by_editor?
  validate  :validate_saleability, unless: :by_editor?
  validate  :validate_total_seats, unless: :by_editor?
  validate  :validate_frequency, unless: :by_editor?

  after_commit :update_seats_count!

  # @return [Boolean] true if by_editor is true
  def by_editor?
    by_editor == true
  end

  private

  # Add an error unless {Happening.saleable?} is true
  def validate_saleability
    errors.add(:seats, 'Posti non assegnabili') unless saleable?
  end

  # Add error unless requested seats are minor than available seatc
  def validate_total_seats
    errors.add(:seats, 'Posti non disponibili') if seats_count + seats.to_i > max_seats
  end

  # check {User}'s ticket presence based of {Fact#tickets_frequency}
  def validate_frequency
    exist, message = case fact.tickets_frequency
                     when 'single'
                       [fact_ticket_exist?, I18n.t('site.ticket.errors.single')]
                     when 'daily'
                       [fact_ticket_exist?(start_at.beginning_of_day..start_at.end_of_day), I18n.t('site.ticket.errors.daily')]
                     when 'weekly'
                       [fact_ticket_exist?(start_at.-(7.days)..start_at.+(7.days)), I18n.t('site.ticket.errors.weekly')]
                     when 'monthly'
                       [fact_ticket_exist?(start_at.-(30.days)..start_at.+(30.days)), I18n.t('site.ticket.errors.montly')]
                     else
                       return true
                     end
    errors.add(:seats, message) if exist
  end

  # check if exists {Fact}'s {Ticket} for {User} in a time period
  def fact_ticket_exist?(period = nil)
    @when = { happenings: { start_at: period } } if period.present?
    fact.tickets.where(@when).where(user: user).exists?
  end
end

#updated_atDateTime

Returns when the record was created.

Returns:

  • (DateTime)

    when the record was created


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/ticket.rb', line 28

class Ticket < ApplicationRecord
  belongs_to :happening, counter_cache: true
  belongs_to :user
  delegate :fact, :fact_id, :max_seats, :max_seats_for_ticket, :saleable?, :seats_count, :start_at, :update_seats_count!, to: :happening, allow_nil: true

  attr_accessor :by_editor

  validates :happening, presence: true
  validates :user, presence: true, uniqueness: { scope: :happening_id }
  validates :seats, presence: true
  validates :seats, numericality: { less_than_or_equal_to: :max_seats_for_ticket }, unless: :by_editor?
  validate  :validate_saleability, unless: :by_editor?
  validate  :validate_total_seats, unless: :by_editor?
  validate  :validate_frequency, unless: :by_editor?

  after_commit :update_seats_count!

  # @return [Boolean] true if by_editor is true
  def by_editor?
    by_editor == true
  end

  private

  # Add an error unless {Happening.saleable?} is true
  def validate_saleability
    errors.add(:seats, 'Posti non assegnabili') unless saleable?
  end

  # Add error unless requested seats are minor than available seatc
  def validate_total_seats
    errors.add(:seats, 'Posti non disponibili') if seats_count + seats.to_i > max_seats
  end

  # check {User}'s ticket presence based of {Fact#tickets_frequency}
  def validate_frequency
    exist, message = case fact.tickets_frequency
                     when 'single'
                       [fact_ticket_exist?, I18n.t('site.ticket.errors.single')]
                     when 'daily'
                       [fact_ticket_exist?(start_at.beginning_of_day..start_at.end_of_day), I18n.t('site.ticket.errors.daily')]
                     when 'weekly'
                       [fact_ticket_exist?(start_at.-(7.days)..start_at.+(7.days)), I18n.t('site.ticket.errors.weekly')]
                     when 'monthly'
                       [fact_ticket_exist?(start_at.-(30.days)..start_at.+(30.days)), I18n.t('site.ticket.errors.montly')]
                     else
                       return true
                     end
    errors.add(:seats, message) if exist
  end

  # check if exists {Fact}'s {Ticket} for {User} in a time period
  def fact_ticket_exist?(period = nil)
    @when = { happenings: { start_at: period } } if period.present?
    fact.tickets.where(@when).where(user: user).exists?
  end
end

#user_idInteger

Returns identifier of related User.

Returns:

  • (Integer)

    identifier of related User


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/ticket.rb', line 28

class Ticket < ApplicationRecord
  belongs_to :happening, counter_cache: true
  belongs_to :user
  delegate :fact, :fact_id, :max_seats, :max_seats_for_ticket, :saleable?, :seats_count, :start_at, :update_seats_count!, to: :happening, allow_nil: true

  attr_accessor :by_editor

  validates :happening, presence: true
  validates :user, presence: true, uniqueness: { scope: :happening_id }
  validates :seats, presence: true
  validates :seats, numericality: { less_than_or_equal_to: :max_seats_for_ticket }, unless: :by_editor?
  validate  :validate_saleability, unless: :by_editor?
  validate  :validate_total_seats, unless: :by_editor?
  validate  :validate_frequency, unless: :by_editor?

  after_commit :update_seats_count!

  # @return [Boolean] true if by_editor is true
  def by_editor?
    by_editor == true
  end

  private

  # Add an error unless {Happening.saleable?} is true
  def validate_saleability
    errors.add(:seats, 'Posti non assegnabili') unless saleable?
  end

  # Add error unless requested seats are minor than available seatc
  def validate_total_seats
    errors.add(:seats, 'Posti non disponibili') if seats_count + seats.to_i > max_seats
  end

  # check {User}'s ticket presence based of {Fact#tickets_frequency}
  def validate_frequency
    exist, message = case fact.tickets_frequency
                     when 'single'
                       [fact_ticket_exist?, I18n.t('site.ticket.errors.single')]
                     when 'daily'
                       [fact_ticket_exist?(start_at.beginning_of_day..start_at.end_of_day), I18n.t('site.ticket.errors.daily')]
                     when 'weekly'
                       [fact_ticket_exist?(start_at.-(7.days)..start_at.+(7.days)), I18n.t('site.ticket.errors.weekly')]
                     when 'monthly'
                       [fact_ticket_exist?(start_at.-(30.days)..start_at.+(30.days)), I18n.t('site.ticket.errors.montly')]
                     else
                       return true
                     end
    errors.add(:seats, message) if exist
  end

  # check if exists {Fact}'s {Ticket} for {User} in a time period
  def fact_ticket_exist?(period = nil)
    @when = { happenings: { start_at: period } } if period.present?
    fact.tickets.where(@when).where(user: user).exists?
  end
end

Instance Method Details

#by_editor?Boolean

Returns true if by_editor is true.

Returns:

  • (Boolean)

    true if by_editor is true


46
47
48
# File 'app/models/ticket.rb', line 46

def by_editor?
  by_editor == true
end

#fact_ticket_exist?(period = nil) ⇒ Boolean (private)

check if exists Fact's Ticket for User in a time period

Returns:

  • (Boolean)

80
81
82
83
# File 'app/models/ticket.rb', line 80

def fact_ticket_exist?(period = nil)
  @when = { happenings: { start_at: period } } if period.present?
  fact.tickets.where(@when).where(user: user).exists?
end

#validate_frequencyObject (private)

check User's ticket presence based of Fact#tickets_frequency


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/ticket.rb', line 63

def validate_frequency
  exist, message = case fact.tickets_frequency
                   when 'single'
                     [fact_ticket_exist?, I18n.t('site.ticket.errors.single')]
                   when 'daily'
                     [fact_ticket_exist?(start_at.beginning_of_day..start_at.end_of_day), I18n.t('site.ticket.errors.daily')]
                   when 'weekly'
                     [fact_ticket_exist?(start_at.-(7.days)..start_at.+(7.days)), I18n.t('site.ticket.errors.weekly')]
                   when 'monthly'
                     [fact_ticket_exist?(start_at.-(30.days)..start_at.+(30.days)), I18n.t('site.ticket.errors.montly')]
                   else
                     return true
                   end
  errors.add(:seats, message) if exist
end

#validate_saleabilityObject (private)

Add an error unless Happening#saleable? is true


53
54
55
# File 'app/models/ticket.rb', line 53

def validate_saleability
  errors.add(:seats, 'Posti non assegnabili') unless saleable?
end

#validate_total_seatsObject (private)

Add error unless requested seats are minor than available seatc


58
59
60
# File 'app/models/ticket.rb', line 58

def validate_total_seats
  errors.add(:seats, 'Posti non disponibili') if seats_count + seats.to_i > max_seats
end