Sha256: c3bcbe9c2752fb8702bd16dd66bd77d734c3ff13f479c9b9a080860dbadc4b64

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 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
      accepts_nested_attributes_for :season
      validates_associated :season

      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]}
      }

      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

1 entries across 1 versions & 1 rubygems

Version Path
timely-0.0.2 lib/timely/rails/extensions.rb