Class: RansackWrap::Search

Inherits:
Object
  • Object
show all
Includes:
Ransack::Naming
Defined in:
lib/ransack_wrap/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, params = {}) ⇒ Search

Returns a new instance of Search.



7
8
9
10
11
12
13
14
15
16
# File 'lib/ransack_wrap/search.rb', line 7

def initialize(object, params = {})
  @base = scope_class.new(object)
  
  params ||= {}
  ransack_params = params.slice! *base.attributes.keys.map(&:to_sym)
  ransack_params ||= {}
  
  build params.with_indifferent_access 
  ransack.build ransack_params.with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ransack_wrap/search.rb', line 36

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
  else
    super
  end
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/ransack_wrap/search.rb', line 5

def base
  @base
end

Instance Method Details

#build(params = {}) ⇒ Object



26
27
28
# File 'lib/ransack_wrap/search.rb', line 26

def build(params = {})
  params.each {|key, value| base.send "#{key}=", value }
end

#build_for_ransackObject



30
31
32
33
34
# File 'lib/ransack_wrap/search.rb', line 30

def build_for_ransack
  base.attributes
    .collect             {|name, args|  base.try_scope name, args }.compact
    .inject(base.scoped) {|scope, subq| scope.merge subq }
end

#ransackObject



18
19
20
# File 'lib/ransack_wrap/search.rb', line 18

def ransack
  @ransack ||= build_for_ransack.ransack
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/ransack_wrap/search.rb', line 49

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) || false
  end
end

#resultObject



22
23
24
# File 'lib/ransack_wrap/search.rb', line 22

def result
  ransack.result
end