Sha256: ed433e9eebd41113dc463ac05e2bf62f8de7289e76c0ecb8285f57cfd2dd0041

Contents?: true

Size: 1.23 KB

Versions: 29

Compression:

Stored size: 1.23 KB

Contents

module Tenon
  class Event < ActiveRecord::Base
    # Scopes, attachments, etc.
    scope :published, -> { where('publish_at <= ?', Time.now) }
    scope :upcoming, -> { where(['ends_at > ?', Time.now]).order(:starts_at) }
    scope :past, -> { where(['ends_at < ?', Time.now]).order(:starts_at) }
    default_scope -> { order 'starts_at DESC' }
    tenon_content :description
    has_history includes: [:description_tenon_content_rows]

    # Validations
    validates_presence_of :title, :starts_at, :ends_at

    def self.on(year, month = nil, day = nil)
      fail ArgumentError, 'must pass a month to pass a day' if day && !month
      time = Time.mktime(year, month, day)
      # delta = day ? 1.day : month ? 1.month : year ? 1.year : fail
      if day
        delta = 1.day
      elsif month
        delta = 1.month
      elsif year
        delta = 1.year
      else
        fail
      end

      limit = time + delta

      where('starts_at > ? AND starts_at < ?', time, limit).order('starts_at')
    end

    def to_param
      "#{id}-#{title.parameterize}"
    end

    def next
      Event.published.where('starts_at > ?', starts_at).first
    end

    def previous
      Event.published.where('starts_at < ?', starts_at).last
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
tenon-1.0.28 app/models/tenon/event.rb
tenon-1.0.27 app/models/tenon/event.rb
tenon-1.0.26 app/models/tenon/event.rb
tenon-1.0.25 app/models/tenon/event.rb
tenon-1.0.24 app/models/tenon/event.rb
tenon-1.0.23 app/models/tenon/event.rb
tenon-1.0.22 app/models/tenon/event.rb
tenon-1.0.21 app/models/tenon/event.rb
tenon-1.0.20 app/models/tenon/event.rb
tenon-1.0.19 app/models/tenon/event.rb
tenon-1.0.18 app/models/tenon/event.rb
tenon-1.0.17 app/models/tenon/event.rb
tenon-1.0.16 app/models/tenon/event.rb
tenon-1.0.15 app/models/tenon/event.rb
tenon-1.0.14 app/models/tenon/event.rb
tenon-1.0.13 app/models/tenon/event.rb
tenon-1.0.12 app/models/tenon/event.rb
tenon-1.0.11 app/models/tenon/event.rb
tenon-1.0.10 app/models/tenon/event.rb
tenon-1.0.9 app/models/tenon/event.rb