Class: Ransack::Helpers::FormBuilder

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

Instance Method Summary collapse

Instance Method Details

#attribute_fields(*args, &block) ⇒ Object



95
96
97
# File 'lib/ransack/helpers/form_builder.rb', line 95

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

#attribute_select(options = nil, html_options = nil, action = nil) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ransack/helpers/form_builder.rb', line 42

def attribute_select(options = nil, html_options = nil, action = nil)
  options = options || {}
  html_options = html_options || {}
  action = action || Constants::SEARCH
  default = options.delete(:default)
  raise ArgumentError, formbuilder_error_message(
    "#{action}_select") unless object.respond_to?(:context)
  options[:include_blank] = true unless options.has_key?(:include_blank)
  bases = [Constants::EMPTY] + association_array(options[: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, options, html_options)
  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, options, html_options)
  end
end

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



148
149
150
151
# File 'lib/ransack/helpers/form_builder.rb', line 148

def combinator_select(options = {}, html_options = {})
  template_collection_select(
    :m, combinator_choices, options, html_options)
end

#condition_fields(*args, &block) ⇒ Object



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

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

#grouping_fields(*args, &block) ⇒ Object



91
92
93
# File 'lib/ransack/helpers/form_builder.rb', line 91

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

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



26
27
28
29
30
31
32
33
34
# File 'lib/ransack/helpers/form_builder.rb', line 26

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



99
100
101
# File 'lib/ransack/helpers/form_builder.rb', line 99

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

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ransack/helpers/form_builder.rb', line 122

def predicate_select(options = {}, html_options = {})
  options[:compounds] = true if options[:compounds].nil?
  default = options.delete(:default) || Constants::CONT

  keys =
  if options[:compounds]
    Predicate.names
  else
    Predicate.names.reject { |k| k.match(/_(any|all)$/) }
  end
  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)$/, Constants::EMPTY)
      }
    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, options, html_options)
end

#search_fields(name, args, block) ⇒ Object



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

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}]"
  objects.inject(ActiveSupport::SafeBuffer.new) do |output, child|
    output << @template.fields_for("#{name}[#{options[:child_index] ||
    nested_child_index(name)}]", child, options, &block)
  end
end

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



66
67
68
69
70
71
72
# File 'lib/ransack/helpers/form_builder.rb', line 66

def sort_direction_select(options = {}, html_options = {})
  unless object.respond_to?(:context)
    raise ArgumentError,
    formbuilder_error_message(Constants::SORT_DIRECTION)
  end
  template_collection_select(:dir, sort_array, options, html_options)
end

#sort_fields(*args, &block) ⇒ Object



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

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


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

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

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



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

def sort_select(options = {}, html_options = {})
  attribute_select(options, html_options, Constants::SORT) +
  sort_direction_select(options, html_options)
end

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



36
37
38
39
40
# File 'lib/ransack/helpers/form_builder.rb', line 36

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



103
104
105
# File 'lib/ransack/helpers/form_builder.rb', line 103

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