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    
neoteric-events / app / models / neoteric / event.rb
Size: Mime:
module Neoteric
  module Event
    def self.included(base)
      base.table_name = 'events'
      base.attr_accessible :title, :starts, :ends, :location, :venue_url, :description

      base.extend ::FriendlyId
      base.friendly_id :title, :use => :slugged

      base.send(:include, ::Neoteric::Archiver)
      base.archive_fields :starts => :starts,
                          :ends   => :ends

      base.send(:include, ::Neoteric::Navigator)
      base.navigate :field => :starts,
                    :order => 'starts DESC'

      base.validates :venue_url, :url => true
      base.validate :ends_after_start

      base.scope :upcoming, base.where('starts > ?', Time.now)
    end

    def ends_after_start
      return true if ends.blank? || starts.blank?

      unless ends > starts
        errors.add(:base, "Event should end later than it starts")
      end
    end
  end
end

class UrlValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    return true if value.blank?

    unless /^https?:\/\/\S+\.\w{2,}\S+?$/ =~ value
      record.errors.add(attribute, 'is invalid')
    end
  end
end