class Microscope::Scope::DatetimeScope

Public Class Methods

new(*args) click to toggle source
Calls superclass method Microscope::Scope::new
# File lib/microscope/scope/datetime_scope.rb, line 4
def initialize(*args)
  super

  @now = 'Time.now'
  @now_suffix = '_now'
  @specific_suffix = '_at'
  @cropped_field_regex = /_at$/
  @formatted_time = 'time'
end

Public Instance Methods

apply() click to toggle source
# File lib/microscope/scope/datetime_scope.rb, line 14
def apply
  return unless @field.name =~ @cropped_field_regex

  validate_field_name!(cropped_field, @field.name)
  model.class_eval(apply_scopes)
end

Private Instance Methods

apply_scopes() click to toggle source
# File lib/microscope/scope/datetime_scope.rb, line 23
      def apply_scopes
        <<-RUBY
          scope "#{cropped_field}_before", lambda { |time| where('#{quoted_field} < ?', #{@formatted_time}) }
          scope "#{cropped_field}_before_or#{@specific_suffix}", lambda { |time| where('#{quoted_field} <= ?', #{@formatted_time}) }
          scope "#{cropped_field}_before#{@now_suffix}", lambda { where('#{quoted_field} < ?', #{@now}) }
          scope "#{cropped_field}_before_or#{@now_suffix}", lambda { where('#{quoted_field} <= ?', #{@now}) }

          scope "#{cropped_field}_after", lambda { |time| where('#{quoted_field} > ?', #{@formatted_time}) }
          scope "#{cropped_field}_after_or#{@specific_suffix}", lambda { |time| where('#{quoted_field} >= ?', #{@formatted_time}) }
          scope "#{cropped_field}_after#{@now_suffix}", lambda { where('#{quoted_field} > ?', #{@now}) }
          scope "#{cropped_field}_after_or#{@now_suffix}", lambda { where('#{quoted_field} >= ?', #{@now}) }

          scope "#{cropped_field}_between", lambda { |range| where("#{@field.name}" => range) }

          scope "#{cropped_field}", lambda { where('#{quoted_field} IS NOT NULL AND #{quoted_field} <= ?', #{@now}) }
          scope "not_#{cropped_field}", lambda { where('#{quoted_field} IS NULL OR #{quoted_field} > ?', #{@now}) }
          scope "un#{cropped_field}", lambda { not_#{cropped_field} }
        RUBY
      end