class RansackWrap::Search

Attributes

base[R]

Public Class Methods

alias_to_ransack(new_name, old_name, writer: true) click to toggle source
# File lib/ransack_wrap/search.rb, line 65
def self.alias_to_ransack(new_name, old_name, writer: true)
  new_hash = {}
  new_hash[new_name.to_s] = old_name.to_s
  new_hash["#{new_name}=".to_s] = "#{old_name}=" if writer
  ransack_aliases.merge! new_hash
end
new(object, params = {}) click to toggle source
# File lib/ransack_wrap/search.rb, line 10
def initialize(object, params = {})
  @base = scope_class.new(object)
  
  params = {} unless params.is_a? Hash
  ransack_params = params.slice! *scope_keys+ransack_aliases.keys.map(&:to_sym)
  
  params.slice!(*scope_keys).each do |key, val|
    ransack_params[ransack_aliases[key].to_sym] = val
  end
  
  build params.with_indifferent_access
  ransack.build ransack_params.with_indifferent_access
end

Public Instance Methods

build(params = {}) click to toggle source
# File lib/ransack_wrap/search.rb, line 32
def build(params = {})
  params.each {|key, value| base.send "#{key}=", value }
end
build_for_ransack() click to toggle source
# File lib/ransack_wrap/search.rb, line 36
def build_for_ransack
  base.attributes
    .collect             {|name, args|  base.try_scope name, args }.compact
    .inject(base.scoped) {|scope, subq| scope.merge subq }
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/ransack_wrap/search.rb, line 42
def method_missing(method, *args)
  name = method.to_s
  writer = name.sub! /\=$/, ''
  
  if base.attribute_method? name
    base.send method, *args
  elsif ransack.respond_to? name
    ransack.send method, *args
  elsif ransack_alias_method?(name)
    ransack.send ransack_aliases[name], *args
  else
    super
  end
end
ransack() click to toggle source
# File lib/ransack_wrap/search.rb, line 24
def ransack
  @ransack ||= build_for_ransack.ransack
end
respond_to?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/ransack_wrap/search.rb, line 57
def respond_to?(method, include_private = false)
  super or begin
    name = method.to_s
    writer = name.sub!(/\=$/, '')
    base.attribute_method?(name) || ransack.respond_to?(name) || ransack_alias_method?(name) || false
  end
end
result() click to toggle source
# File lib/ransack_wrap/search.rb, line 28
def result
  ransack.result
end

Private Instance Methods

ransack_alias_method?(name) click to toggle source
# File lib/ransack_wrap/search.rb, line 73
def ransack_alias_method?(name)
  ransack_aliases.key?(name) && ransack.respond_to?(ransack_aliases[name])
end
scope_class() click to toggle source
# File lib/ransack_wrap/search.rb, line 81
def scope_class
  raise NameError unless self.class.const_defined?(:Scopes)
  self.class.const_get(:Scopes)
end
scope_keys() click to toggle source
# File lib/ransack_wrap/search.rb, line 77
def scope_keys
  @scope_keys ||= base.attributes.keys.map(&:to_sym)
end