Class: SimpleFormRansack::FormProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_form_ransack/form_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ FormProxy

Returns a new instance of FormProxy.



2
3
4
5
6
7
8
9
10
# File 'lib/simple_form_ransack/form_proxy.rb', line 2

def initialize(args)
  @resource = args[:resource]
  @object = @resource.object
  @class = @resource.klass
  @params = args[:params]
  @form = args[:form]
  
  raise "No params given in arguments: #{args.keys}" unless @params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object



38
39
40
# File 'lib/simple_form_ransack/form_proxy.rb', line 38

def method_missing(method_name, *args, &blk)
  @form.__send__(method_name, *args, &blk)
end

Instance Method Details

#input(name, *args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/simple_form_ransack/form_proxy.rb', line 12

def input(name, *args)
  if args.last.is_a?(Hash)
    opts = args.pop
  else
    opts = {}
  end
  
  attribute_name = real_name(name, opts)
  as = as_from_opts(opts)
  
  input_html = opts.delete(:input_html) || {}
  input_html[:name] = "q[#{name}]" unless input_html.key?(:name)
  
  if as == "select"
    opts[:selected] = @params[name] if !input_html.key?(:selected) && @params[name]
  else
    input_html[:value] = @params[name] if !input_html.key?(:value) && @params[name]
  end
  
  opts[:input_html] = input_html
  opts[:required] = false unless opts.key?(:required)
  
  args << opts
  return @form.input(attribute_name, *args)
end