Sha256: 83854b75be164158b4e3370325bf2b3e306322c126e401e7097448ab01da7e44

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module ActsAsInterval
  module ActsAsInterval
    extend ActiveSupport::Concern

    included do
    end

    module ClassMethods
      def acts_as_interval(**options)
        cattr_accessor :start_field, :end_field
        self.start_field = options[:start]
        self.end_field = options[:end]

        include ActsAsInterval::LocalInstanceMethods
      end
    end

    module LocalInstanceMethods
      def self.included(klass)
        klass_name = klass.name.underscore.pluralize
        define_method "past_#{klass_name}" do
          self.class.where('ends_at <= ?', self.send(start_field))
        end
        define_method "future_#{klass_name}" do
          self.class.where('starts_at >= ?', self.send(end_field))
        end
        define_method "overlapping_#{klass_name}" do
          self.class.where("DATEDIFF(#{start_field}, :my_end) * DATEDIFF(:my_start, #{end_field}) >= 0", my_start: self.send(start_field), my_end: self.send(end_field)).where.not(id: self.id)
        end
        alias_method :intervals_before, "past_#{klass_name}"
        alias_method :intervals_after, "future_#{klass_name}"
      end
    end
  end
end
ActiveRecord::Base.send :include, ActsAsInterval::ActsAsInterval

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_interval-0.0.2 lib/acts_as_interval/acts_as_interval.rb