Sha256: 6d23440cef7889a0d41a40736e3dce98306d68450360ab7e9c15dccf19529601
Contents?: true
Size: 1.16 KB
Versions: 1
Compression:
Stored size: 1.16 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( 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( 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, self, *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.alpha2 | lib/datetime-scopes/active_record_extension.rb |