Sha256: fa435b6ee725cf46e77a556983d91c0160b2f7c2432f85492c5a878e277e7bdc

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module Timely
  module Extensions
    # Add a WeekDays attribute
    #
    # By default it will use attribute_bit_array as db field, but this can
    # be overridden by specifying :db_field => 'somthing_else'
    def weekdays_field(attribute, options={})
      db_field = options[:db_field] || attribute.to_s + '_bit_array'
      self.composed_of(attribute,
        :class_name => "::Timely::WeekDays",
        :mapping    => [[db_field, 'weekdays_int']],
        :converter  => Proc.new {|field| ::Timely::WeekDays.new(field)}
      )
    end

    def acts_as_seasonal
      belongs_to :season, :class_name => 'Timely::Season', optional: true
      accepts_nested_attributes_for :season
      validates_associated :season

      if ::ActiveRecord::VERSION::MAJOR >= 3
        scope :season_on, lambda { |*args|
          date = args.first || ::Date.current # Can't assign in block in Ruby 1.8
          joins(:season => :date_groups).where("date_groups.start_date <= ? AND date_groups.end_date >= ?", date, date)
        }

        scope :available_from, lambda { |*args|
          date = args.first || ::Date.current # Can't assign in block in Ruby 1.8
          where("boundary_end >= ?", date)
        }
      else
        named_scope :season_on, lambda { |*args|
          date = args.first || ::Date.current # Can't assign in block in Ruby 1.8
          {
            :joins => {:season => :date_groups},
            :conditions => ["date_groups.start_date <= ? AND date_groups.end_date >= ?", date, date]
          }
        }

        named_scope :available_from, lambda { |*args|
          date = args.first || ::Date.current # Can't assign in block in Ruby 1.8
          {:conditions => ["boundary_end >= ?", date]}
        }
      end

      before_save do |object|
        if object.season
          object.boundary_start = object.season.boundary_start
          object.boundary_end   = object.season.boundary_end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
timely-0.5.0 lib/timely/rails/extensions.rb
timely-0.4.2 lib/timely/rails/extensions.rb