module RailsScopy::ClassMethods

Public Instance Methods

perform() click to toggle source
# File lib/rails_scopy.rb, line 12
def perform
  return if abstract_class?
  columns.each do |column|
    name = column.name
    perform_order(name)
    perform_base(name)
    method = "perform_#{column.type}"
    valid = respond_to?(method, :include_private)
    send(method, name) if valid
  end
end

Private Instance Methods

perform_base(name) click to toggle source
# File lib/rails_scopy.rb, line 31
def perform_base(name)
  scope("#{name}_eq", ->(value) { where(name => value) })
  scope("#{name}_not_eq", ->(value) { where.not(name => value) })
  scope("#{name}_null", -> { where(name => nil) })
  scope("#{name}_not_null", -> { where.not(name => nil) })
  scope("#{name}_present", -> { where("#{name} IS NOT NULL AND #{name} != ''") })
  scope("#{name}_blank", -> { where("#{name} IS NULL OR #{name} = ''") })
end
perform_boolean(name) click to toggle source
# File lib/rails_scopy.rb, line 79
def perform_boolean(name)
  scope :"not_#{name}", -> { where(name => false) }
  scope :"#{name}", -> { where(name => true) }
end
perform_date(name)
Alias for: perform_datetime
perform_datetime(name) click to toggle source
# File lib/rails_scopy.rb, line 69
def perform_datetime(name)
  scope :"#{name}_to", ->(value) { where("#{name} <= ?", value) }
  scope :"#{name}_from", ->(value) { where("#{name} >= ?", value) }
  scope :"#{name}_after", ->(value) { where("#{name} > ?", value) }
  scope :"#{name}_before", ->(value) { where("#{name} < ?", value) }
  scope :"#{name}_between", ->(from, to) { where("#{name} BETWEEN ? AND ?", from, to) }
end
Also aliased as: perform_date, perform_time
perform_float(name) click to toggle source
# File lib/rails_scopy.rb, line 52
def perform_float(name)
  perform_numeric(name)
  scope :"#{name}_scale", ->(value) { where("SCALE(#{name}) = ?", value) }
end
perform_integer(name) click to toggle source
# File lib/rails_scopy.rb, line 48
def perform_integer(name)
  perform_numeric(name)
end
perform_json(name) click to toggle source
# File lib/rails_scopy.rb, line 84
def perform_json(name)
  scope :"#{name}_has_key", ->(value) { where("(#{name} #>'{#{value}}') IS NOT NULL") }
  scope :"#{name}_has_not_key", ->(value) { where("(#{name} #>'{#{value}}') IS NULL") }
end
perform_numeric(name) click to toggle source
# File lib/rails_scopy.rb, line 40
def perform_numeric(name)
  scope :"#{name}_to", ->(value) { where("#{name} <= ?", value) }
  scope :"#{name}_from", ->(value) { where("#{name} >= ?", value) }
  scope :"#{name}_above", ->(value) { where("#{name} > ?", value) }
  scope :"#{name}_below", ->(value) { where("#{name} < ?", value) }
  scope :"#{name}_between", ->(from, to) { where("#{name} BETWEEN ? AND ?", from, to) }
end
perform_order(name) click to toggle source
# File lib/rails_scopy.rb, line 26
def perform_order(name)
  scope :"ascend_by_#{name}", -> { order(name => :asc) }
  scope :"descend_by_#{name}", -> { order(name => :desc) }
end
perform_string(name) click to toggle source
# File lib/rails_scopy.rb, line 57
def perform_string(name)
  scope :"#{name}_contains", ->(value) { where("#{name} LIKE ?", "%#{sanitize_sql_like(value)}%") }
  scope :"#{name}_not_contains", ->(value) { where("#{name} NOT LIKE ?", "%#{sanitize_sql_like(value)}%") }
  scope :"#{name}_starts_with", ->(value) { where("#{name} LIKE ?", "#{sanitize_sql_like(value)}%") }
  scope :"#{name}_not_starts_with", ->(value) { where("#{name} NOT LIKE ?", "#{sanitize_sql_like(value)}%") }
  scope :"#{name}_ends_with", ->(value) { where("#{name} LIKE ?", "%#{sanitize_sql_like(value)}") }
  scope :"#{name}_not_ends_with", ->(value) { where("#{name} NOT LIKE ?", "%#{sanitize_sql_like(value)}") }
  scope :"#{name}_length", ->(value) { where("LENGTH(#{name}) = ?", value) }
  scope :"#{name}_between_length", ->(from, to) { where("LENGTH(#{name}) BETWEEN ? AND ?", from, to) }
end
Also aliased as: perform_text
perform_text(name)
Alias for: perform_string
perform_time(name)
Alias for: perform_datetime