Class: ActiveDryForm::Builder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::TagHelper, 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



71
72
73
74
# File 'lib/active_dry_form/builder.rb', line 71

def button(value = nil, options = {}, &block)
  options[:class] = [options[:class], 'button'].compact
  super
end

#fields_for(association_name, fields_options = {}, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/active_dry_form/builder.rb', line 76

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_dry_form/builder.rb', line 10

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



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

def input_check_box(field, options = {}); wrap_input(__method__, field, options) { |opts| check_box(field, opts) } end

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



44
45
46
47
48
# File 'lib/active_dry_form/builder.rb', line 44

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



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

def input_date(field, options = {});      wrap_input(__method__, field, options) { |opts| date_field(field, opts) } end

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



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

def input_datetime(field, options = {});  wrap_input(__method__, field, options) { |opts| datetime_field(field, opts) } end

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



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

def input_email(field, options = {});     wrap_input(__method__, field, options) { |opts| email_field(field, opts) } end

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



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

def input_file(field, options = {});      wrap_input(__method__, field, options) { |opts| file_field(field, opts) } end

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



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

def input_hidden(field, options = {}); hidden_field(field, options) end

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



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

def input_integer(field, options = {});   wrap_input(__method__, field, options) { |opts| number_field(field, opts) } end

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



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

def input_number(field, options = {});    wrap_input(__method__, field, options) { |opts| number_field(field, opts) } end

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



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

def input_password(field, options = {});  wrap_input(__method__, field, options) { |opts| password_field(field, opts) } end

#input_select(field, collection, options = {}, html_options = {}) ⇒ Object



50
51
52
53
54
# File 'lib/active_dry_form/builder.rb', line 50

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



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

def input_telephone(field, options = {}); wrap_input(__method__, field, options) { |opts| telephone_field(field, opts) } end

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



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

def input_text(field, options = {});      wrap_input(__method__, field, options) { |opts| text_field(field, opts) } end

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



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

def input_text_area(field, options = {}); wrap_input(__method__, field, options) { |opts| text_area(field, opts) } end

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



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

def input_url(field, options = {});       wrap_input(__method__, field, options) { |opts| url_field(field, opts) } end

#show_base_errorsObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/active_dry_form/builder.rb', line 56

def show_base_errors
  return if @object.base_errors.empty?

  tag.div class: ActiveDryForm.config.css_classes.base_error do
    tag.ul do
      # внутри ошибки может быть html
      @object.base_errors.map { tag.li _1.html_safe }.join.html_safe
    end
  end
end

#show_error(field) ⇒ Object



67
68
69
# File 'lib/active_dry_form/builder.rb', line 67

def show_error(field)
  ActiveDryForm::Input.new(self, __method__, field, {}).error_text
end