Class: Fact

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

Overview

This model manage the facts (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


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

#happenings_countInteger

Returns counter cache for Happening.

Returns:


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

#idInteger

Returns unique identifier for Fact.

Returns:

  • (Integer)

    unique identifier for Fact


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

#pinnedBoolean

Returns [true] if this Fact has priority.

Returns:

  • (Boolean)
    true

    if this Fact has priority


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

#start_ondate

Returns when start Fact.

Returns:

  • (date)

    when start Fact


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

#stop_ondate

Returns when end Fact.

Returns:

  • (date)

    when end Fact


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

#tickets_frequencyString

Returns enum of tickets frequency statuses.

Returns:

  • (String)

    enum of tickets frequency statuses


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

#titleString

Returns title of Fact.

Returns:

  • (String)

    title of Fact


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

#updated_atDateTime

Returns when the record was updated.

Returns:

  • (DateTime)

    when the record was updated


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

#wherestring

Returns where the Fact is located.

Returns:

  • (string)

    where the Fact is located


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  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


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  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


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/fact.rb', line 36

class Fact < ApplicationRecord
  has_rich_text :body
  has_one_attached :image
  belongs_to :group
  has_many :happenings, dependent: :destroy
  has_many :tickets, through: :happenings
  validates :title, presence: true
  validates :start_on, presence: true
  validates :stop_on, presence: true
  enum tickets_frequency: %i[any single daily weekly monthly]

  after_create :add_default_image, unless: proc { |fact| fact.image.attached? }

  scope :future,  -> { where('stop_on >= :from', from: Time.zone.today).order('pinned desc, stop_on asc') }
  scope :history, -> { where('stop_on < :from', from: Time.zone.today).order('start_on desc') }

  private

  # add a default image
  def add_default_image
    image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
  end
end

Instance Method Details

#add_default_imageObject (private)

add a default image


55
56
57
# File 'app/models/fact.rb', line 55

def add_default_image
  image.attach(io: File.open(Rails.root.join('app', 'javascript', 'images', 'default.png')), filename: 'default.png', content_type: 'image/png')
end