Class: ActiveDryForm::Builder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
Dry::Core::Constants
Defined in:
lib/active_dry_form/builder.rb

Constant Summary collapse

ARRAY_NULL =
%w[null].freeze

Instance Method Summary collapse

Instance Method Details

#button(value = nil, options = {}, &block) ⇒ Object



69
70
71
72
# File 'lib/active_dry_form/builder.rb', line 69

def button(value = nil, options = {}, &block)
  options[:class] = [options[: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, fields_options = {}, &block)
  fields_options[:builder] ||= options[:builder]
  fields_options[:namespace] = options[:namespace]
  fields_options[:parent_builder] = self

  association = @object.public_send(association_name)

  if association.is_a?(BaseForm)
    fields_for_nested_model("#{@object_name}[#{association_name}]", association, fields_options, 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, fields_options, 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, options = {})
  case input_type(field)
  when 'date'              then input_date(field, options)
  when 'time'              then input_datetime(field, options)
  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, options)
  when 'number'            then input_number(field, options)
  when 'boolean'           then input_check_box(field, options)
  else
    case field.to_s
    when /password/ then input_password(field, options)
    when /email/    then input_email(field, options)
    when /phone/    then input_telephone(field, options)
    when /url/      then input_url(field, options)
    else input_text(field, options)
    end
  end
end

#input_check_box(field, options = {}) ⇒ Object



38
# File 'lib/active_dry_form/builder.rb', line 38

def input_check_box(field, options = {}); wrap_input(__method__, field, options) { |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, options = {})
  wrap_input(__method__, field, options, 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, options = {});      wrap_input(__method__, field, options) { |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, options = {});  wrap_input(__method__, field, options) { |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, options = {});     wrap_input(__method__, field, options) { |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, options = {});      wrap_input(__method__, field, options) { |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, options = {}); hidden_field(field, options) end

#input_integer(field, options = {}) ⇒ Object



29
# File 'lib/active_dry_form/builder.rb', line 29

def input_integer(field, options = {});   wrap_input(__method__, field, options) { |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, options = {});    wrap_input(__method__, field, options) { |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, options = {});  wrap_input(__method__, field, options) { |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, options = {}, html_options = {})
  wrap_input(__method__, field, html_options) do |opts|
    select(field, collection, options, opts)
  end
end

#input_telephone(field, options = {}) ⇒ Object



36
# File 'lib/active_dry_form/builder.rb', line 36

def input_telephone(field, options = {}); wrap_input(__method__, field, options) { |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, options = {});      wrap_input(__method__, field, options) { |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, options = {}); wrap_input(__method__, field, options) { |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, options = {});       wrap_input(__method__, field, options) { |opts| url_field(field, opts) } end

#show_base_errorsObject



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.(:div, class: ActiveDryForm.config.css_classes.base_error) do
    @template.(:ul) do
      # внутри ошибки может быть html
      @object.base_errors.map { @template.(: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