Class: Event

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

Overview

This model manage the events

Relations

has many Happening

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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

#happenings_countInteger

Returns counter cache for Happening.

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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

#idInteger

Returns unique identifier for Event.

Returns:

  • (Integer)

    unique identifier for Event



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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

#pinnedBoolean

Returns [true] if this Event has priority.

Returns:

  • (Boolean)
    true

    if this Event has priority



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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

#start_ondate

Returns when start Event.

Returns:

  • (date)

    when start Event



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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

#stop_ondate

Returns when end Event.

Returns:

  • (date)

    when end Event



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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

#tickets_frequencyString

Returns enum of tickets frequency statuses.

Returns:

  • (String)

    enum of tickets frequency statuses



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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

#titleString

Returns title of Event.

Returns:

  • (String)

    title of Event



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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

#wherestring

Returns where the Event is located.

Returns:

  • (string)

    where the Event is located



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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

Class Method Details

.searchable(from: nil, to: nil, group_id: nil, text: nil, editor: false) ⇒ Object

Search event with params

Parameters:

  • from (String) (defaults to: nil)

    (nil) [stop_on] is greather than value to date. If value is nil [stop_on] set as Time.zone.now.to_date unless editor params is true

  • to (String) (defaults to: nil)

    (nil) if present, [start_at] is minor than value to date.

  • group_id (Integer) (defaults to: nil)

    (nil) if present, search Event with valorized [group_id]

  • text (String) (defaults to: nil)

    (nil) if present search text in [title]

  • editor (Boolean) (defaults to: false)

    (false) if true skip default from_date as Time.zone.now, Editors can view Event without Happening

  • reserver (Boolean)

    (false) reserved event are excluded unless unless is true



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
# File 'app/models/event.rb', line 38

class Event < ApplicationRecord
  has_rich_text :body
  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
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  enum :tickets_frequency, %i[any single daily weekly monthly]

  scope :searchable, ->(from: nil, to: nil, group_id: nil, text: nil, editor: false, reserved: false) do
    from = Time.zone.now unless editor || from.present?
    by_keys = {}
    by_keys[:stop_on]  = (from.try(:to_date)..) if from.present?
    by_keys[:start_on] = (..to.try(:to_date)) if to.present?
    by_keys[:group_id] = group_id  if group_id.present?
    by_keys[:reserved] = false unless reserved
    by_text = text.present? ? [ "title ilike :text", { text: "%#{text}%" } ] : nil
    where(by_text).where(by_keys)
  end

  # @return {String} image path with variand or default image
  def image_url(_variant = :card)
    image.present? ? image.variant(:card) : "default.png"
  end
end

Instance Method Details

#image_url(_variant = :card) ⇒ String

Returns image path with variand or default image.

Returns:

  • (String)

    image path with variand or default image



63
64
65
# File 'app/models/event.rb', line 63

def image_url(_variant = :card)
  image.present? ? image.variant(:card) : "default.png"
end