Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
getfitter-core / app / models / core / event.rb
Size: Mime:
module Core
  class Event < ActiveRecord::Base
    include PgSearch
    include Calories

    create_time_scopes

    belongs_to :organisation_venue
    has_one :organisation, through: :organisation_venue
    has_one :venue, through: :organisation_venue
    has_many :assignments
    has_many :instructors, through: :assignments
    has_many :bookings

    validates :name, presence: true
    validates :start_time, presence: true
    validates :end_time, presence: true
    validates :price, presence: true
    validates :availability, inclusion: { in: %w(unknown spaces full waitlist),
                                          message: '%{value} is not a valid '\
                                                   'availability type' }

    accepts_nested_attributes_for :instructors

    before_destroy :check_for_bookings

    def self.availabilities
      %w(unknown spaces full waitlist)
    end

    def self.known_activity_types
      %w(elite yoga pilates cardio full_body unknown
         dance barre spin bootcamp one_to_one)
    end

    default_scope { order('created_at') }
    scope :close_to, -> (latitude, longitude, distance_in_meters = 2000) {
      joins(:venue).where(%{
        ST_DWithin(
          ST_GeographyFromText(
            'SRID=4326;POINT(' || core_venues.longitude
              || ' ' || core_venues.latitude || ')'
          ),
          ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
          %d
        )
      } % [longitude, latitude, distance_in_meters])
    }

    pg_search_scope :search, against: [:name, :description],
                             associated_against: { instructors: :name },
                             using: { tsearch: { prefix: true } }

    private

    def check_for_bookings
      unless bookings.empty?
        self.errors[:base] << 'Cannot delete an event containing bookings'

        return false
      end
    end
  end
end