Class: Ransack::Helpers::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/ransack/helpers/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#attribute_fields(*args, &block) ⇒ Object



78
79
80
# File 'lib/ransack/helpers/form_builder.rb', line 78

def attribute_fields(*args, &block)
  search_fields(:a, args, block)
end

#attribute_select(options = {}, html_options = {}) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ransack/helpers/form_builder.rb', line 20

def attribute_select(options = {}, html_options = {})
  raise ArgumentError, "attribute_select must be called inside a search FormBuilder!" unless object.respond_to?(:context)
  options[:include_blank] = true unless options.has_key?(:include_blank)
  bases = [''] + association_array(options[:associations])
  if bases.size > 1
    @template.grouped_collection_select(
      @object_name, :name, searchable_attribute_collection_for_bases(bases), :last, :first, :first, :last,
      objectify_options(options), @default_options.merge(html_options)
    )
  else
    collection = searchable_attribute_collection_for_base(bases.first)
    @template.collection_select(
      @object_name, :name, collection, :first, :last,
      objectify_options(options), @default_options.merge(html_options)
    )
  end
end

#combinator_select(options = {}, html_options = {}) ⇒ Object



124
125
126
127
128
129
# File 'lib/ransack/helpers/form_builder.rb', line 124

def combinator_select(options = {}, html_options = {})
  @template.collection_select(
    @object_name, :m, combinator_choices, :first, :last,
    objectify_options(options), @default_options.merge(html_options)
  )
end

#condition_fields(*args, &block) ⇒ Object



70
71
72
# File 'lib/ransack/helpers/form_builder.rb', line 70

def condition_fields(*args, &block)
  search_fields(:c, args, block)
end

#grouping_fields(*args, &block) ⇒ Object



74
75
76
# File 'lib/ransack/helpers/form_builder.rb', line 74

def grouping_fields(*args, &block)
  search_fields(:g, args, block)
end

#label(method, *args, &block) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/ransack/helpers/form_builder.rb', line 6

def label(method, *args, &block)
  options = args.extract_options!
  text = args.first
  i18n = options[:i18n] || {}
  text ||= object.translate(method, i18n.reverse_merge(:include_associations => true)) if object.respond_to? :translate
  super(method, text, options, &block)
end

#predicate_fields(*args, &block) ⇒ Object



82
83
84
# File 'lib/ransack/helpers/form_builder.rb', line 82

def predicate_fields(*args, &block)
  search_fields(:p, args, block)
end

#predicate_select(options = {}, html_options = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ransack/helpers/form_builder.rb', line 106

def predicate_select(options = {}, html_options = {})
  options[:compounds] = true if options[:compounds].nil?
  keys = options[:compounds] ? Predicate.names : Predicate.names.reject {|k| k.match(/_(any|all)$/)}
  if only = options[:only]
    if only.respond_to? :call
      keys = keys.select {|k| only.call(k)}
    else
      only = Array.wrap(only).map(&:to_s)
      keys = keys.select {|k| only.include? k.sub(/_(any|all)$/, '')}
    end
  end

  @template.collection_select(
    @object_name, :p, keys.map {|k| [k, Translate.predicate(k)]}, :first, :last,
    objectify_options(options), @default_options.merge(html_options)
  )
end

#search_fields(name, args, block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ransack/helpers/form_builder.rb', line 90

def search_fields(name, args, block)
  args << {} unless args.last.is_a?(Hash)
  args.last[:builder] ||= options[:builder]
  args.last[:parent_builder] = self
  options = args.extract_options!
  objects = args.shift
  objects ||= @object.send(name)
  objects = [objects] unless Array === objects
  name = "#{options[:object_name] || object_name}[#{name}]"
  output = ActiveSupport::SafeBuffer.new
  objects.each do |child|
    output << @template.fields_for("#{name}[#{options[:child_index] || nested_child_index(name)}]", child, options, &block)
  end
  output
end

#sort_fields(*args, &block) ⇒ Object



62
63
64
# File 'lib/ransack/helpers/form_builder.rb', line 62

def sort_fields(*args, &block)
  search_fields(:s, args, block)
end


66
67
68
# File 'lib/ransack/helpers/form_builder.rb', line 66

def sort_link(attribute, *args)
  @template.sort_link @object, attribute, *args
end

#sort_select(options = {}, html_options = {}) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ransack/helpers/form_builder.rb', line 38

def sort_select(options = {}, html_options = {})
  raise ArgumentError, "sort_select must be called inside a search FormBuilder!" unless object.respond_to?(:context)
  options[:include_blank] = true unless options.has_key?(:include_blank)
  bases = [''] + association_array(options[:associations])
  if bases.size > 1
    @template.grouped_collection_select(
      @object_name, :name, sortable_attribute_collection_for_bases(bases), :last, :first, :first, :last,
      objectify_options(options), @default_options.merge(html_options)
    ) + @template.collection_select(
      @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last,
      objectify_options(options), @default_options.merge(html_options)
    )
  else
    collection = sortable_attribute_collection_for_base(bases.first)
    @template.collection_select(
      @object_name, :name, collection, :first, :last,
      objectify_options(options), @default_options.merge(html_options)
    ) + @template.collection_select(
      @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last,
      objectify_options(options), @default_options.merge(html_options)
    )
  end
end

#submit(value = nil, options = {}) ⇒ Object



14
15
16
17
18
# File 'lib/ransack/helpers/form_builder.rb', line 14

def submit(value=nil, options={})
  value, options = nil, value if value.is_a?(Hash)
  value ||= Translate.word(:search).titleize
  super(value, options)
end

#value_fields(*args, &block) ⇒ Object



86
87
88
# File 'lib/ransack/helpers/form_builder.rb', line 86

def value_fields(*args, &block)
  search_fields(:v, args, block)
end