Class: Ransack::Helpers::FormBuilder
- Inherits:
-
Object
- Object
- Ransack::Helpers::FormBuilder
- Defined in:
- lib/ransack/helpers/form_builder.rb
Instance Method Summary collapse
- #attribute_fields(*args, &block) ⇒ Object
- #attribute_select(options = nil, html_options = nil, action = nil) ⇒ 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_direction_select(options = {}, html_options = {}) ⇒ Object
- #sort_fields(*args, &block) ⇒ Object
- #sort_link(attribute, *args) ⇒ Object
- #sort_select(options = {}, html_options = {}) ⇒ Object
- #sort_url(attribute, *args) ⇒ Object
- #submit(value = nil, options = {}) ⇒ Object
- #value_fields(*args, &block) ⇒ Object
Instance Method Details
#attribute_fields(*args, &block) ⇒ Object
104 105 106 |
# File 'lib/ransack/helpers/form_builder.rb', line 104 def attribute_fields(*args, &block) search_fields(:a, args, block) end |
#attribute_select(options = nil, html_options = nil, action = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ransack/helpers/form_builder.rb', line 47 def attribute_select( = nil, = nil, action = nil) ||= {} ||= {} action ||= Constants::SEARCH default = .delete(:default) raise ArgumentError, ( "#{action}_select") unless object.respond_to?(:context) [:include_blank] = true unless .has_key?(:include_blank) bases = [''.freeze].freeze + association_array([:associations]) if bases.size > 1 collection = attribute_collection_for_bases(action, bases) object.name ||= default if can_use_default?( default, :name, mapped_values(collection.flatten(2)) ) template_grouped_collection_select(collection, , ) else collection = collection_for_base(action, bases.first) object.name ||= default if can_use_default?( default, :name, mapped_values(collection) ) template_collection_select(:name, collection, , ) end end |
#combinator_select(options = {}, html_options = {}) ⇒ Object
157 158 159 160 |
# File 'lib/ransack/helpers/form_builder.rb', line 157 def combinator_select( = {}, = {}) template_collection_select( :m, combinator_choices, , ) end |
#condition_fields(*args, &block) ⇒ Object
96 97 98 |
# File 'lib/ransack/helpers/form_builder.rb', line 96 def condition_fields(*args, &block) search_fields(:c, args, block) end |
#grouping_fields(*args, &block) ⇒ Object
100 101 102 |
# File 'lib/ransack/helpers/form_builder.rb', line 100 def grouping_fields(*args, &block) search_fields(:g, args, block) end |
#label(method, *args, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/ransack/helpers/form_builder.rb', line 31 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
108 109 110 |
# File 'lib/ransack/helpers/form_builder.rb', line 108 def predicate_fields(*args, &block) search_fields(:p, args, block) end |
#predicate_select(options = {}, html_options = {}) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ransack/helpers/form_builder.rb', line 131 def predicate_select( = {}, = {}) [:compounds] = true if [:compounds].nil? default = .delete(:default) || Constants::CONT keys = if [:compounds] Predicate.names else Predicate.names.reject { |k| k.match(/_(any|all)$/) } end if only = [: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)$/, ''.freeze) } end end collection = keys.map { |k| [k, Translate.predicate(k)] } object.predicate ||= Predicate.named(default) if can_use_default?(default, :predicate, keys) template_collection_select(:p, collection, , ) end |
#search_fields(name, args, block) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/ransack/helpers/form_builder.rb', line 116 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}]" objects.inject(ActiveSupport::SafeBuffer.new) do |output, child| output << @template.fields_for("#{name}[#{[:child_index] || nested_child_index(name)}]", child, , &block) end end |
#sort_direction_select(options = {}, html_options = {}) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/ransack/helpers/form_builder.rb', line 71 def sort_direction_select( = {}, = {}) unless object.respond_to?(:context) raise ArgumentError, ('sort_direction'.freeze) end template_collection_select(:dir, sort_array, , ) end |
#sort_fields(*args, &block) ⇒ Object
84 85 86 |
# File 'lib/ransack/helpers/form_builder.rb', line 84 def sort_fields(*args, &block) search_fields(:s, args, block) end |
#sort_link(attribute, *args) ⇒ Object
88 89 90 |
# File 'lib/ransack/helpers/form_builder.rb', line 88 def sort_link(attribute, *args) @template.sort_link @object, attribute, *args end |
#sort_select(options = {}, html_options = {}) ⇒ Object
79 80 81 82 |
# File 'lib/ransack/helpers/form_builder.rb', line 79 def sort_select( = {}, = {}) attribute_select(, , 'sort'.freeze) + sort_direction_select(, ) end |
#sort_url(attribute, *args) ⇒ Object
92 93 94 |
# File 'lib/ransack/helpers/form_builder.rb', line 92 def sort_url(attribute, *args) @template.sort_url @object, attribute, *args end |
#submit(value = nil, options = {}) ⇒ Object
41 42 43 44 45 |
# File 'lib/ransack/helpers/form_builder.rb', line 41 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
112 113 114 |
# File 'lib/ransack/helpers/form_builder.rb', line 112 def value_fields(*args, &block) search_fields(:v, args, block) end |