Skip to content

Commit

Permalink
perf tags, remove include ActionView
Browse files Browse the repository at this point in the history
  • Loading branch information
ermolaev committed Dec 11, 2024
1 parent 0f8c5ac commit f775893
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
10 changes: 4 additions & 6 deletions lib/active_dry_form/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
module ActiveDryForm
class Builder < ActionView::Helpers::FormBuilder

include ActionView::Helpers::TagHelper
include ActionView::Context
include Dry::Core::Constants

def input(field, options = {})
Expand Down Expand Up @@ -56,16 +54,16 @@ def input_select(field, collection, options = {}, html_options = {})
def show_base_errors
return if @object.base_errors.empty?

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

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

def button(value = nil, options = {}, &block)
Expand Down
4 changes: 1 addition & 3 deletions lib/active_dry_form/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ def active_dry_form_for(name, options = {}, &block)
options[:builder] = ActiveDryForm::Builder
options[:html] = html_options(options)

# Array.wrap because Hash === name, it breaks polymorphic_path
# TODO: refactor to options[:url]
form_for(Array.wrap(name), options) do |f|
form_for(name, options) do |f|
concat f.show_base_errors
instance_exec(f, &block)
end
Expand Down
13 changes: 8 additions & 5 deletions lib/active_dry_form/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class Input

def initialize(builder, builder_method, field, options)
@builder = builder
@form_object = builder.object
@template = builder.instance_variable_get(:@template)

@builder_method = builder_method
@field = field

Expand All @@ -25,7 +28,7 @@ def css_classes
end

def wrap_tag(input, label_last: nil)
@builder.tag.div class: css_classes do
@template.content_tag(:div, class: css_classes) do
[
label_last ? input : label,
label_last ? label : input,
Expand All @@ -42,23 +45,23 @@ def label
def hint_text
return unless @hint_text

@builder.tag.small @hint_text, class: ActiveDryForm.config.css_classes.hint
@template.content_tag(:small, @hint_text, class: ActiveDryForm.config.css_classes.hint)
end

def error_text
return unless error?(@field)

obj_error_text =
case e = @builder.object.errors[@field]
case e = @form_object.errors[@field]
when Hash then e.values
else e
end

@builder.tag.div obj_error_text.join('<br />').html_safe, class: ActiveDryForm.config.css_classes.error
@template.content_tag(:div, obj_error_text.join('<br />').html_safe, class: ActiveDryForm.config.css_classes.error)
end

def error?(field)
@builder.object.errors.key?(field)
@form_object.errors.key?(field)
end

end
Expand Down

0 comments on commit f775893

Please sign in to comment.