Class: Ransack::Helpers::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Ransack::Helpers::FormBuilder
- Defined in:
- lib/ransack/helpers/form_builder.rb
Instance Method Summary collapse
- #attribute_fields(*args, &block) ⇒ Object
- #attribute_select(options = {}, html_options = {}) ⇒ Object
- #combinator_select(options = {}, html_options = {}) ⇒ Object
- #condition_fields(*args, &block) ⇒ Object
- #grouping_fields(*args, &block) ⇒ Object
- #label(method, *args, &block) ⇒ Object
- #predicate_fields(*args, &block) ⇒ Object
- #predicate_select(options = {}, html_options = {}) ⇒ Object
- #search_fields(name, args, block) ⇒ Object
- #sort_fields(*args, &block) ⇒ Object
- #sort_link(attribute, *args) ⇒ Object
- #sort_select(options = {}, html_options = {}) ⇒ Object
- #submit(value = nil, options = {}) ⇒ Object
- #value_fields(*args, &block) ⇒ Object
Instance Method Details
#attribute_fields(*args, &block) ⇒ Object
88 89 90 |
# File 'lib/ransack/helpers/form_builder.rb', line 88 def attribute_fields(*args, &block) search_fields(:a, args, block) end |
#attribute_select(options = {}, html_options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ransack/helpers/form_builder.rb', line 20 def attribute_select( = {}, = {}) raise ArgumentError, "attribute_select must be called inside a search FormBuilder!" unless object.respond_to?(:context) [:include_blank] = true unless .has_key?(:include_blank) bases = [''] + association_array([:associations]) if bases.size > 1 @template.grouped_collection_select( @object_name, :name, attribute_collection_for_bases(bases), :last, :first, :first, :last, (), @default_options.merge() ) else collection = object.context.searchable_attributes(bases.first).map do |c| [ attr_from_base_and_column(bases.first, c), Translate.attribute(attr_from_base_and_column(bases.first, c), :context => object.context) ] end @template.collection_select( @object_name, :name, collection, :first, :last, (), @default_options.merge() ) end end |
#combinator_select(options = {}, html_options = {}) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/ransack/helpers/form_builder.rb', line 128 def combinator_select( = {}, = {}) @template.collection_select( @object_name, :m, combinator_choices, :first, :last, (), @default_options.merge() ) end |
#condition_fields(*args, &block) ⇒ Object
80 81 82 |
# File 'lib/ransack/helpers/form_builder.rb', line 80 def condition_fields(*args, &block) search_fields(:c, args, block) end |
#grouping_fields(*args, &block) ⇒ Object
84 85 86 |
# File 'lib/ransack/helpers/form_builder.rb', line 84 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) = args. text = args.first i18n = [:i18n] || {} text ||= object.translate(method, i18n.reverse_merge(:include_associations => true)) if object.respond_to? :translate super(method, text, , &block) end |
#predicate_fields(*args, &block) ⇒ Object
92 93 94 |
# File 'lib/ransack/helpers/form_builder.rb', line 92 def predicate_fields(*args, &block) search_fields(:p, args, block) end |
#predicate_select(options = {}, html_options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ransack/helpers/form_builder.rb', line 116 def predicate_select( = {}, = {}) [:compounds] = true if [:compounds].nil? keys = predicate_keys() # If condition is newly built with build_condition(), # then replace the default predicate with the first in the ordered list @object.predicate_name = keys.first if @object.default? @template.collection_select( @object_name, :p, keys.map {|k| [k, Translate.predicate(k)]}, :first, :last, (), @default_options.merge() ) end |
#search_fields(name, args, block) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ransack/helpers/form_builder.rb', line 100 def search_fields(name, args, block) args << {} unless args.last.is_a?(Hash) args.last[:builder] ||= [:builder] args.last[:parent_builder] = self = args. objects = args.shift objects ||= @object.send(name) objects = [objects] unless Array === objects name = "#{[:object_name] || object_name}[#{name}]" output = ActiveSupport::SafeBuffer.new objects.each do |child| output << @template.fields_for("#{name}[#{[:child_index] || nested_child_index(name)}]", child, , &block) end output end |
#sort_fields(*args, &block) ⇒ Object
72 73 74 |
# File 'lib/ransack/helpers/form_builder.rb', line 72 def sort_fields(*args, &block) search_fields(:s, args, block) end |
#sort_link(attribute, *args) ⇒ Object
76 77 78 |
# File 'lib/ransack/helpers/form_builder.rb', line 76 def sort_link(attribute, *args) @template.sort_link @object, attribute, *args end |
#sort_select(options = {}, html_options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ransack/helpers/form_builder.rb', line 43 def sort_select( = {}, = {}) raise ArgumentError, "sort_select must be called inside a search FormBuilder!" unless object.respond_to?(:context) [:include_blank] = true unless .has_key?(:include_blank) bases = [''] + association_array([:associations]) if bases.size > 1 @template.grouped_collection_select( @object_name, :name, attribute_collection_for_bases(bases), :last, :first, :first, :last, (), @default_options.merge() ) + @template.collection_select( @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last, (), @default_options.merge() ) else collection = object.context.searchable_attributes(bases.first).map do |c| [ attr_from_base_and_column(bases.first, c), Translate.attribute(attr_from_base_and_column(bases.first, c), :context => object.context) ] end @template.collection_select( @object_name, :name, collection, :first, :last, (), @default_options.merge() ) + @template.collection_select( @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last, (), @default_options.merge() ) 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, ={}) value, = nil, value if value.is_a?(Hash) value ||= Translate.word(:search).titleize super(value, ) end |
#value_fields(*args, &block) ⇒ Object
96 97 98 |
# File 'lib/ransack/helpers/form_builder.rb', line 96 def value_fields(*args, &block) search_fields(:v, args, block) end |