Class: ActiveDryForm::Builder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- ActiveDryForm::Builder
- Includes:
- Dry::Core::Constants
- Defined in:
- lib/active_dry_form/builder.rb
Constant Summary collapse
- ARRAY_NULL =
%w[null].freeze
Instance Method Summary collapse
- #button(value = nil, options = {}, &block) ⇒ Object
- #fields_for(association_name, fields_options = {}, &block) ⇒ Object
- #input(field, options = {}) ⇒ Object
- #input_check_box(field, options = {}) ⇒ Object
- #input_check_box_inline(field, options = {}) ⇒ Object
- #input_date(field, options = {}) ⇒ Object
- #input_datetime(field, options = {}) ⇒ Object
- #input_email(field, options = {}) ⇒ Object
- #input_file(field, options = {}) ⇒ Object
- #input_hidden(field, options = {}) ⇒ Object
- #input_integer(field, options = {}) ⇒ Object
- #input_number(field, options = {}) ⇒ Object
- #input_password(field, options = {}) ⇒ Object
- #input_select(field, collection, options = {}, html_options = {}) ⇒ Object
- #input_telephone(field, options = {}) ⇒ Object
- #input_text(field, options = {}) ⇒ Object
- #input_text_area(field, options = {}) ⇒ Object
- #input_url(field, options = {}) ⇒ Object
- #show_base_errors ⇒ Object
- #show_error(field) ⇒ Object
Instance Method Details
#button(value = nil, options = {}, &block) ⇒ Object
69 70 71 72 |
# File 'lib/active_dry_form/builder.rb', line 69 def (value = nil, = {}, &block) [:class] = [[:class], 'button'].compact super end |
#fields_for(association_name, fields_options = {}, &block) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_dry_form/builder.rb', line 74 def fields_for(association_name, = {}, &block) [:builder] ||= [:builder] [:namespace] = [:namespace] [:parent_builder] = self association = @object.public_send(association_name) if association.is_a?(BaseForm) fields_for_nested_model("#{@object_name}[#{association_name}]", association, , block) elsif association.respond_to?(:to_ary) field_name_regexp = Regexp.new(Regexp.escape("#{@object_name}[#{association_name}][") << '\d+\]') # хак для замены хеша на массив output = ActiveSupport::SafeBuffer.new Array.wrap(association).each do |child| output << fields_for_nested_model("#{@object_name}[#{association_name}][]", child, , block) .gsub(field_name_regexp, "#{@object_name}[#{association_name}][]").html_safe end output end end |
#input(field, options = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_dry_form/builder.rb', line 8 def input(field, = {}) case input_type(field) when 'date' then input_date(field, ) when 'time' then input_datetime(field, ) when 'date-time' then raise DateTimeNotAllowedError, 'use :time instead of :date_time (does not apply timezone) in params block' when 'integer' then input_integer(field, ) when 'number' then input_number(field, ) when 'boolean' then input_check_box(field, ) else case field.to_s when /password/ then input_password(field, ) when /email/ then input_email(field, ) when /phone/ then input_telephone(field, ) when /url/ then input_url(field, ) else input_text(field, ) end end end |
#input_check_box(field, options = {}) ⇒ Object
38 |
# File 'lib/active_dry_form/builder.rb', line 38 def input_check_box(field, = {}); wrap_input(__method__, field, ) { |opts| check_box(field, opts) } end |
#input_check_box_inline(field, options = {}) ⇒ Object
42 43 44 45 46 |
# File 'lib/active_dry_form/builder.rb', line 42 def input_check_box_inline(field, = {}) wrap_input(__method__, field, , label_last: true) do |opts| check_box(field, opts) end end |
#input_date(field, options = {}) ⇒ Object
27 |
# File 'lib/active_dry_form/builder.rb', line 27 def input_date(field, = {}); wrap_input(__method__, field, ) { |opts| date_field(field, opts) } end |
#input_datetime(field, options = {}) ⇒ Object
28 |
# File 'lib/active_dry_form/builder.rb', line 28 def input_datetime(field, = {}); wrap_input(__method__, field, ) { |opts| datetime_field(field, opts) } end |
#input_email(field, options = {}) ⇒ Object
32 |
# File 'lib/active_dry_form/builder.rb', line 32 def input_email(field, = {}); wrap_input(__method__, field, ) { |opts| email_field(field, opts) } end |
#input_file(field, options = {}) ⇒ Object
35 |
# File 'lib/active_dry_form/builder.rb', line 35 def input_file(field, = {}); wrap_input(__method__, field, ) { |opts| file_field(field, opts) } end |
#input_hidden(field, options = {}) ⇒ Object
40 |
# File 'lib/active_dry_form/builder.rb', line 40 def input_hidden(field, = {}); hidden_field(field, ) end |
#input_integer(field, options = {}) ⇒ Object
29 |
# File 'lib/active_dry_form/builder.rb', line 29 def input_integer(field, = {}); wrap_input(__method__, field, ) { |opts| number_field(field, opts) } end |
#input_number(field, options = {}) ⇒ Object
30 |
# File 'lib/active_dry_form/builder.rb', line 30 def input_number(field, = {}); wrap_input(__method__, field, ) { |opts| number_field(field, opts) } end |
#input_password(field, options = {}) ⇒ Object
31 |
# File 'lib/active_dry_form/builder.rb', line 31 def input_password(field, = {}); wrap_input(__method__, field, ) { |opts| password_field(field, opts) } end |
#input_select(field, collection, options = {}, html_options = {}) ⇒ Object
48 49 50 51 52 |
# File 'lib/active_dry_form/builder.rb', line 48 def input_select(field, collection, = {}, = {}) wrap_input(__method__, field, ) do |opts| select(field, collection, , opts) end end |
#input_telephone(field, options = {}) ⇒ Object
36 |
# File 'lib/active_dry_form/builder.rb', line 36 def input_telephone(field, = {}); wrap_input(__method__, field, ) { |opts| telephone_field(field, opts) } end |
#input_text(field, options = {}) ⇒ Object
34 |
# File 'lib/active_dry_form/builder.rb', line 34 def input_text(field, = {}); wrap_input(__method__, field, ) { |opts| text_field(field, opts) } end |
#input_text_area(field, options = {}) ⇒ Object
37 |
# File 'lib/active_dry_form/builder.rb', line 37 def input_text_area(field, = {}); wrap_input(__method__, field, ) { |opts| text_area(field, opts) } end |
#input_url(field, options = {}) ⇒ Object
33 |
# File 'lib/active_dry_form/builder.rb', line 33 def input_url(field, = {}); wrap_input(__method__, field, ) { |opts| url_field(field, opts) } end |
#show_base_errors ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/active_dry_form/builder.rb', line 54 def show_base_errors return if @object.base_errors.empty? @template.content_tag(:div, class: ActiveDryForm.config.css_classes.base_error) do @template.content_tag(:ul) do # внутри ошибки может быть html @object.base_errors.map { @template.content_tag(:li, _1.html_safe) }.join.html_safe end end end |
#show_error(field) ⇒ Object
65 66 67 |
# File 'lib/active_dry_form/builder.rb', line 65 def show_error(field) ActiveDryForm::Input.new(self, nil, field, {}).error_text end |