Module: DefaultForm::Builder::Helper

Includes:
Default, Wrap
Included in:
FormBuilder
Defined in:
lib/default_form/builder/helper.rb

Constant Summary collapse

INPUT_FIELDS =
[
  :text_field,
  :password_field,
  :color_field,
  :search_field,
  :telephone_field,
  :phone_field,
  :time_field,
  :datetime_field,
  :datetime_local_field,
  :month_field,
  :week_field,
  :url_field,
  :email_field,
  :range_field,
  :file_field,
  :date_select
].freeze

Constants included from Default

Default::VALIDATIONS

Instance Method Summary collapse

Methods included from Default

#default_help, #default_label, #default_options, #default_step, #default_value, #default_without_method

Methods included from Wrap

#help_tag, #object_has_errors?, #offset, #wrapping, #wrapping_all

Instance Method Details

#check_box(method, options = {}, checked_value = '1', unchecked_value = '0') ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/default_form/builder/helper.rb', line 50

def check_box(method, options = {}, checked_value = '1', unchecked_value = '0')
  wrap_all_with(method, options) do |css|
    default_options(method, options)
    options[:class] = css.dig(:origin, :checkbox) unless options.key?(:class)
    r = options.delete(:label)
    if r.present?
      label_text = (:span, r)
    else
      label_text = ''
    end
    checkbox_content = wrapping(:checkbox, super + label_text, wrap: css[:wrap], tag: 'label')

    offset(css.dig(:offset, :submit)) + checkbox_content
  end
end

#collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



66
67
68
69
70
# File 'lib/default_form/builder/helper.rb', line 66

def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  wrap_with(method, options) do |css|
    wrapping(:checkboxes, super, wrap: css[:wrap])
  end
end

#collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



80
81
82
83
84
# File 'lib/default_form/builder/helper.rb', line 80

def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  wrap_with(method, options) do |css|
    wrapping(:radios, super, wrap: css[:wrap])
  end
end

#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/default_form/builder/helper.rb', line 100

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  wrap_with(method, options) do |css|
    html_options[:class] = if html_options[:multiple]
      css.dig(:origin, :multi_select)
    else
      css.dig(:origin, :select)
    end unless html_options.key?(:class)
    options[:include_blank] = I18n.t('helpers.select.prompt') if options[:include_blank] == true

    wrapping(:select, super, wrap: css[:wrap])
  end
end

#date_field(method, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/default_form/builder/helper.rb', line 137

def date_field(method, options = {})
  wrap_with(method, options) do |css|
    options[:class] = css.dig(:origin, :input) unless options.key?(:class)
    if method.end_with?('(date)')
      real_method = method.to_s.sub('(date)', '')
      options[:data] = {}
      options[:data].merge! controller: 'datetime', action: 'datetime#default' if object.column_for_attribute(real_method).type == :datetime
      options[:value] = object.read_attribute(real_method)&.to_date
    end

    wrapping(:input, super, wrap: css[:wrap])
  end
end

#fields(scope = nil, model: nil, **options, &block) ⇒ Object



27
28
29
30
# File 'lib/default_form/builder/helper.rb', line 27

def fields(scope = nil, model: nil, **options, &block)
  options[:theme] ||= theme
  super
end

#hidden_field(method, options = {}) ⇒ Object



132
133
134
135
# File 'lib/default_form/builder/helper.rb', line 132

def hidden_field(method, options = {})
  options[:autocomplete] = on_options[:autocomplete] unless options.key?(:autocomplete)
  super
end

#label(method, text = nil, options = {}, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/default_form/builder/helper.rb', line 32

def label(method, text = nil, options = {}, &block)
  origin = (options.delete(:origin) || {}).with_defaults!(origin_css)
  wrap = (options.delete(:wrap) || {}).with_defaults!(wrap_css)
  options[:class] = origin[:label] unless options.key?(:class)

  wrapping(:label, super, wrap: wrap)
end

#number_field(method, options = {}) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/default_form/builder/helper.rb', line 151

def number_field(method, options = {})
  wrap_with(method, options) do |css|
    options[:class] = css.dig(:origin, :input) unless options.key?(:class)
    options[:step] = default_step(method) unless options.key?(:step)
    wrapping(:input, super, wrap: css[:wrap])
  end
end

#radio_button(method, tag_value, options = {}) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/default_form/builder/helper.rb', line 72

def radio_button(method, tag_value, options = {})
  wrap_with(method, options) do |css|
    options[:class] = css.dig(:origin, :radio) unless options.key?(:class)
    value_content = label(method, tag_value, class: nil)
    wrapping(:radio, super + value_content, wrap: css[:wrap])
  end
end

#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/default_form/builder/helper.rb', line 86

def select(method, choices = nil, options = {}, html_options = {}, &block)
  wrap_with(method, options) do |css|
    options[:selected] ||= default_value(method)
    if html_options[:multiple]
      html_options[:class] = css.dig(:origin, :multi_select)
    else
      html_options[:class] = css.dig(:origin, :select)
    end unless html_options.key?(:class)
    options[:include_blank] = I18n.t('helpers.select.prompt') if options[:include_blank] == true

    wrapping(:select, super, wrap: css[:wrap])
  end
end

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



40
41
42
43
44
45
46
47
48
# File 'lib/default_form/builder/helper.rb', line 40

def submit(value = nil, options = {})
  wrap_all_with(nil, options) do |css|
    options[:class] = css.dig(:origin, :submit) unless options.key?(:class)
    css[:wrap][:all] = css.dig(:wrap, :all_submit)

    submit_content = wrapping(:submit, super, wrap: css[:wrap])
    offset(css.dig(:offset, :submit)) + submit_content
  end
end

#text_area(method, options = {}) ⇒ Object



159
160
161
162
163
164
# File 'lib/default_form/builder/helper.rb', line 159

def text_area(method, options = {})
  wrap_with(method, options) do |css|
    options[:class] = css.dig(:origin, :textarea) unless options.key?(:class)
    wrapping(:input, super, wrap: css[:wrap])
  end
end

#time_select(method, options = {}, html_options = {}) ⇒ Object



125
126
127
128
129
130
# File 'lib/default_form/builder/helper.rb', line 125

def time_select(method, options = {}, html_options = {})
  wrap_with(method, options) do |css|
    html_options[:class] = css.dig(:origin, :select) unless html_options.key?(:class)
    wrapping(:select, super, wrap: css[:wrap])
  end
end

#time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/default_form/builder/helper.rb', line 113

def time_zone_select(method, priority_zones = nil, options = {}, html_options = {})
  wrap_with(method, options) do |css|
    html_options[:class] = if html_options[:multiple]
      css.dig(:origin, :multi_select)
    else
      css.dig(:origin, :select)
    end unless html_options.key?(:class)

    wrapping(:select, super, wrap: css[:wrap])
  end
end

#wrap_all_with(method, options) ⇒ Object

block 应返回 label_content + input_content 的内容



183
184
185
186
187
188
189
190
191
192
# File 'lib/default_form/builder/helper.rb', line 183

def wrap_all_with(method, options)
  css = {}
  css[:origin] = origin_css.merge options.delete(:origin) || {}
  css[:wrap] = wrap_css.merge options.delete(:wrap) || {}
  css[:error] = error_css.merge options.delete(:error) || {}
  css[:offset] = offset_css.merge options.delete(:offset) || {}
  inner_content = yield css

  wrapping_all inner_content, method, wrap: css[:wrap], required: options[:required]
end

#wrap_with(method, options = {}) ⇒ Object

block 应返回 input with wrapper 的内容



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/default_form/builder/helper.rb', line 167

def wrap_with(method, options = {})
  wrap_all_with(method, options) do |css|
    default_options(method, options)
    if options[:label]
      label_content = label method, options.delete(:label), options.slice(:origin, :wrap)
    else
      options.delete(:label)
      label_content = ''.html_safe
    end
    input_content = yield css

    label_content + input_content
  end
end