Class: Happening
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Happening
- Defined in:
- app/models/happening.rb
Overview
Instance Attribute Summary collapse
-
#created_at ⇒ DateTime
When the record was created.
-
#event_id ⇒ Integer
Identifier of related Event.
-
#id ⇒ Integer
Unique identifier for Happening.
-
#max_seats ⇒ integer
Number of seats for the Happening.
-
#max_seats_for_ticket ⇒ Integer
Max seats for each ticket, default is 1.
-
#seats_count ⇒ Integer
Counter cache of sum of Ticket.seats.
-
#start_at ⇒ DateTime
When the Happening start.
-
#stop_sale_at ⇒ DateTime
Since Ticket can no longer be sold.
-
#ticketss_count ⇒ Integer
Counter cache for Ticket.
-
#title ⇒ String
Is an optional description for Happening.
-
#updated_at ⇒ DateTime
When the record was updated.
Class Method Summary collapse
-
.future ⇒ Array
List of [Event] with stop_on egual or greather than Time.zone.today, ordered by pinnes, stop_on.
-
.history ⇒ Array
List of [Event] with stop_on minor than Time.zone.today, ordered by start_on desc.
- .massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ⇒ Object
Instance Method Summary collapse
-
#active? ⇒ Boolean
Check if the happening is bookable.
-
#code ⇒ String
Unique code to identify the Happening.
- #image_url(_variant = :card) ⇒ Object
-
#saleable? ⇒ Boolean
Check seaelability time.
- #tickets_available ⇒ Object
-
#tickets_available? ⇒ Boolean
Check if there are tickets available.
- #update_event_date ⇒ Object
Instance Attribute Details
#created_at ⇒ DateTime
Returns when the record was created.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#event_id ⇒ Integer
Returns identifier of related Event.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#id ⇒ Integer
Returns unique identifier for Happening.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#max_seats ⇒ integer
Returns number of seats for the Happening. If is null hasn’t limit.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#max_seats_for_ticket ⇒ Integer
Returns Max seats for each ticket, default is 1.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#seats_count ⇒ Integer
Returns counter cache of sum of Ticket.seats.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#start_at ⇒ DateTime
Returns when the Happening start.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#stop_sale_at ⇒ DateTime
Returns since Ticket can no longer be sold.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#ticketss_count ⇒ Integer
Returns counter cache for Ticket.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#title ⇒ String
Returns Is an optional description for Happening.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
#updated_at ⇒ DateTime
Returns when the record was updated.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
Class Method Details
.future ⇒ Array
Returns list of [Event] with stop_on egual or greather than Time.zone.today, ordered by pinnes, stop_on.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
.history ⇒ Array
Returns list of [Event] with stop_on minor than Time.zone.today, ordered by start_on desc.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/happening.rb', line 39 class Happening < ApplicationRecord has_rich_text :body belongs_to :event, counter_cache: true has_many :tickets, dependent: :destroy has_one_attached :image do |attachable| attachable.variant :card, resize_to_limit: [ 417, 278 ] attachable.variant :aside, resize_to_limit: [ 318, 318 ] attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end validates :event, presence: true validates :start_at, presence: true validates :start_sale_at, presence: true validates :stop_sale_at, presence: true validates :max_tickets, presence: true validates :max_tickets_for_user, presence: true after_save :update_event_date delegate :group_id, to: :event default_scope { left_joins(:event).order("start_at asc") } # scope :between, ->(from = nil, to = nil) { where start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } # scope :by_event, ->(value = nil) { where(event_id: value) if value.present? } # scope :by_group, ->(value = nil) { where(events: { group_id: value }) if value.present? } # scope :by_text, ->(value = nil) { where [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{value}%" } ] if value.present? } scope :searchable, ->(from: nil, to: nil, event_id: nil, group_id: nil, text: nil) do by_keys = { start_at: (from.try(:to_date) || Date.today)..to.try(:to_date).try(:end_of_day) } by_keys[:event_id] = event_id if event_id.present? by_keys[:events] = { group_id: group_id } if group_id.present? by_text = text.present? ? [ "happenings.title ilike :text or events.title ilike :text", { text: "%#{text}%" } ] : nil where(by_keys).where(by_text) end # @return [Boolean] check seaelability time def saleable? active? && tickets_available? end # @return [Boolean] check if the happening is bookable def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end # @return [Boolean] Check if there are tickets available def tickets_available? max_tickets >= tickets_count end def tickets_available max_tickets - tickets_count end # @return [String] unique code to identify the {Happening} def code [ event_id, id ].join(" - ") end def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end end |
.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/models/happening.rb', line 96 def self.massive_create(from:, to:, start_sale_before:, stop_sale_before:, repeat_in: [], minutes: [], **opts) ary = [] (from.to_date..to.to_date).map do |day| next unless repeat_in.include?(day.wday.to_s) minutes.map do |minute| start_at = day + minute.to_i.minutes start_sale_at = day - start_sale_before.to_i.days stop_sale_at = day - stop_sale_before.to_i.days ary << opts.merge(start_at:, start_sale_at:, stop_sale_at:) end end create ary end |
Instance Method Details
#active? ⇒ Boolean
Returns check if the happening is bookable.
78 79 80 |
# File 'app/models/happening.rb', line 78 def active? Time.zone.now >= start_sale_at && Time.zone.now <= stop_sale_at end |
#code ⇒ String
Returns unique code to identify the Happening.
92 93 94 |
# File 'app/models/happening.rb', line 92 def code [ event_id, id ].join(" - ") end |
#image_url(_variant = :card) ⇒ Object
116 117 118 |
# File 'app/models/happening.rb', line 116 def image_url(_variant = :card) image.present? ? image.variant(:card) : event.image_url end |
#saleable? ⇒ Boolean
Returns check seaelability time.
73 74 75 |
# File 'app/models/happening.rb', line 73 def saleable? active? && tickets_available? end |
#tickets_available ⇒ Object
87 88 89 |
# File 'app/models/happening.rb', line 87 def tickets_available max_tickets - tickets_count end |
#tickets_available? ⇒ Boolean
Returns Check if there are tickets available.
83 84 85 |
# File 'app/models/happening.rb', line 83 def tickets_available? max_tickets >= tickets_count end |
#update_event_date ⇒ Object
110 111 112 113 114 |
# File 'app/models/happening.rb', line 110 def update_event_date event.stop_on = start_at if event.stop_on.blank? || start_at.to_date > event.stop_on event.start_on = start_at if event.start_on.blank? || start_at.to_date < event.start_on event.save if event.changed? end |