Class: Happening

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

Overview

This model manage the happenings for each Fact

Relations

belongs to Fact has many Ticket

Validates

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#created_atDateTime

Returns when the record was created.

Returns:

  • (DateTime)

    when the record was created


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#fact_idInteger

Returns identifier of related Fact.

Returns:

  • (Integer)

    identifier of related Fact


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#idInteger

Returns unique identifier for Happening.

Returns:

  • (Integer)

    unique identifier for Happening


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#max_seatsinteger

Returns number of seats for the Happening. If is null hasn't limit.

Returns:

  • (integer)

    number of seats for the Happening. If is null hasn't limit


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#max_seats_for_ticketInteger

Returns Max seats for each ticket, default is 1.

Returns:

  • (Integer)

    Max seats for each ticket, default is 1


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#repeat_forObject

Returns the value of attribute repeat_for.


51
52
53
# File 'app/models/happening.rb', line 51

def repeat_for
  @repeat_for
end

#repeat_inObject

Returns the value of attribute repeat_in.


51
52
53
# File 'app/models/happening.rb', line 51

def repeat_in
  @repeat_in
end

#seats_countInteger

Returns counter cache of sum of Ticket#seats.

Returns:


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#start_atDateTime

Returns when the Happening start.

Returns:


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#stop_sale_atDateTime

Returns since Ticket can no longer be sold.

Returns:

  • (DateTime)

    since Ticket can no longer be sold


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#ticketss_countInteger

Returns counter cache for Ticket.

Returns:

  • (Integer)

    counter cache for Ticket


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#titleString

Returns Is an optional description for Happening.

Returns:

  • (String)

    Is an optional description for Happening


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

#updated_atDateTime

Returns when the record was updated.

Returns:

  • (DateTime)

    when the record was updated


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

Class Method Details

.futureArray

Returns list of [Fact] with stop_on egual or greather than Time.zone.today, ordered by pinnes asc, stop_on asc.

Returns:

  • (Array)

    list of [Fact] with stop_on egual or greather than Time.zone.today, ordered by pinnes asc, stop_on asc


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

.historyArray

Returns list of [Fact] with stop_on minor than Time.zone.today, ordered by start_on desc.

Returns:

  • (Array)

    list of [Fact] with stop_on minor than Time.zone.today, ordered by start_on desc


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
85
86
87
88
89
90
91
92
93
94
# File 'app/models/happening.rb', line 38

class Happening < ApplicationRecord
  belongs_to :fact, counter_cache: true
  has_many   :tickets, dependent: :destroy
  validates  :fact, presence: true
  validates  :start_at, presence: true
  validates  :start_sale_at, presence: true
  validates  :stop_sale_at, presence: true
  validates  :max_seats, presence: true
  validates  :max_seats_for_ticket, presence: true
  validates :repeat_for, numericality: { greather_to: 0, only_integer: true }, on: :create

  after_create :add_repetitions

  attr_accessor :repeat_for, :repeat_in

  scope :future,  -> { where('start_at >= :from', from: Time.zone.now).order('start_at asc') }
  scope :history, -> { where('start_at < :from', from: Time.zone.now).order('start_at desc') }

  # update {seats_count} with sum of her {Ticket.seats}
  def update_seats_count!
    if persisted?
      self.seats_count = tickets.sum :seats
      save touch: false 
      broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
      broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
    end
  end

  # @return [Boolean] check seaelability time
  def saleable?
    Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
  end

  # @return [String] unique code to identify the {Happening}
  def code
    [fact_id, id].join(' - ')
  end

  private

  # save multiple copies of the elements
  def add_repetitions
    (1..repeat_for.to_i).each do |n|
      next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

      fact.happenings.create(
        detail: detail,
        start_at: start_at + n.days,
        start_sale_at: start_at + n.days,
        stop_sale_at: stop_sale_at + n.days,
        max_seats: max_seats,
        max_seats_for_ticket: max_seats_for_ticket,
        repeat_for: 0
      )
    end
  end
end

Instance Method Details

#add_repetitionsObject (private)

save multiple copies of the elements


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/happening.rb', line 79

def add_repetitions
  (1..repeat_for.to_i).each do |n|
    next unless repeat_in.include?(start_at.+(n.days).wday.to_s)

    fact.happenings.create(
      detail: detail,
      start_at: start_at + n.days,
      start_sale_at: start_at + n.days,
      stop_sale_at: stop_sale_at + n.days,
      max_seats: max_seats,
      max_seats_for_ticket: max_seats_for_ticket,
      repeat_for: 0
    )
  end
end

#codeString

Returns unique code to identify the Happening.

Returns:

  • (String)

    unique code to identify the Happening


72
73
74
# File 'app/models/happening.rb', line 72

def code
  [fact_id, id].join(' - ')
end

#saleable?Boolean

Returns check seaelability time.

Returns:

  • (Boolean)

    check seaelability time


67
68
69
# File 'app/models/happening.rb', line 67

def saleable?
  Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at
end

#update_seats_count!Object

update #seats_count with sum of her Ticket#seats


57
58
59
60
61
62
63
64
# File 'app/models/happening.rb', line 57

def update_seats_count!
  if persisted?
    self.seats_count = tickets.sum :seats
    save touch: false 
    broadcast_replace_to 'facts', target: "counter_happening_#{id}", partial: 'happenings/counter',  locals: { happening: self }
    broadcast_replace_to 'facts', target: "nav_fact_#{fact_id}", partial: 'facts/nav',  locals: { fact: fact }
  end
end