Sha256: 91e576df53ee3051f4cef9375d018f8e9fcae0f104e2980833352e78cc22e1f5

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require_relative "datetime_proxy"
require_relative "date_proxy"

module DateTimeScopes
  module ActiveRecordExtension
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def datetime_scopes(attr, prefix: nil, time_zone: ::Time.zone)
        proxy = DateTimeProxy.new(
          klass: self,
          attribute: "#{table_name}.#{attr}",
          time_zone: time_zone
        )
        prefix = datetime_scope_prefix(attr) if prefix.nil?
        declare_datetime_scopes prefix, proxy
      end

      def date_scopes(attr, prefix: nil, time_zone: nil)
        proxy = DateProxy.new(
          klass: self,
          attribute: "#{table_name}.#{attr}",
          time_zone: time_zone
        )
        prefix = datetime_scope_prefix(attr) if prefix.nil?
        declare_datetime_scopes prefix, proxy
      end

      private

      def declare_datetime_scopes(prefix, proxy)
        proxy.proxy_methods.each do |method_name|
          scope_name = "#{prefix}_#{method_name}"
          scope scope_name, -> (*attrs) { proxy.send method_name, *attrs }
        end
      end

      def datetime_scope_prefix(attr)
        attr.to_s.sub /_(at|on|time|date)$/, ""
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datetime-scopes-1.0.0.alpha1 lib/datetime-scopes/active_record_extension.rb