Skip to content

Commit

Permalink
Simplify rendering and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benlovell committed Feb 20, 2024
1 parent 0697a3e commit 13a9483
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ gemspec

ruby IO.read('.ruby-version').chomp

gem 'rails', '~> 6.0.0'
gem 'rails', '~> 6.1'

group :development, :test do
gem 'brakeman', require: false
Expand Down
36 changes: 6 additions & 30 deletions lib/dough/forms/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,7 @@ class Builder < ActionView::Helpers::FormBuilder
# <%= f.errors_summary %>
#
def errors_summary
view_renderer.render(view_context, partial: errors_summary_partial_name, locals: { errors: object_errors }) if object_errors.present?
end

def compiled_method_container
view_context.compiled_method_container
end

def in_rendering_context(options, &blk)
view_context.in_rendering_context(options, &blk)
end

def _run(*args, &blk)
view_context._run(*args, &blk)
ApplicationController.render(partial: errors_summary_partial_name, locals: { errors: object_errors }) if object_errors.present?
end

# This is the partial used to render the summary errors.
Expand All @@ -40,7 +28,7 @@ def _run(*args, &blk)
# end
#
def errors_summary_partial_name
'errors_summary'
'dough/forms/builder/errors_summary'
end

# Returns *all* error messages for the field passed on the argument.
Expand All @@ -52,7 +40,7 @@ def errors_summary_partial_name
#
def errors_for(field_name)
errors = object_errors.select { |error, _| error.field_name == field_name }
render(partial: errors_for_partial_name, collection: errors, as: 'error')
ApplicationController.render(partial: errors_for_partial_name, collection: errors, as: 'error')
end

# This is the partial used to render the summary errors.
Expand All @@ -66,15 +54,15 @@ def errors_for(field_name)
# end
#
def errors_for_partial_name
'errors_for'
'dough/forms/builder/errors_for'
end

def object_errors
object.errors.map.each_with_index do |error, index|
object_error_class.new(
object: object,
field_name: error[0],
message: error[1],
field_name: error.attribute,
message: error.message,
counter: index + 1,
prefix: object_name
)
Expand Down Expand Up @@ -115,18 +103,6 @@ def object_error_class
def partial_paths
[Dough::Engine.root.join('app/views/dough/forms/builder/')]
end

def lookup_context
ActionView::LookupContext.new(ActionController::Base.view_paths + partial_paths)
end

def view_context
ActionView::Base.new(lookup_context, {}, nil)
end

def view_renderer
ActionView::Renderer.new(lookup_context)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/dough/forms/builders/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def collate_model_errors(model)
field_order = Array(model.try(:field_order))

[].tap do |model_errors|
(field_order | model.errors.keys).each do |field|
(field_order | model.errors.attribute_names).each do |field|
model.errors.full_messages_for(field).each do |message|
model_errors << [field, message]
end
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/dough/forms/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CustomModel
let(:model) { invalid_model }

it 'returns the name of the partial' do
expect(subject.errors_summary_partial_name).to eq('errors_summary')
expect(subject.errors_summary_partial_name).to eq('dough/forms/builder/errors_summary')
end
end

Expand All @@ -84,7 +84,7 @@ class CustomModel
let(:model) { valid_model }

it 'returns no error messages' do
expect(inline_error).to be_nil
expect(inline_error).to eq(' ')
end
end

Expand All @@ -109,7 +109,7 @@ class CustomModel
let(:model) { invalid_model }

it 'returns the name of the partial' do
expect(subject.errors_for_partial_name).to eq('errors_for')
expect(subject.errors_for_partial_name).to eq('dough/forms/builder/errors_for')
end
end

Expand Down
22 changes: 11 additions & 11 deletions spec/lib/dough/forms/builders/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class Dough::Forms::Builders::ValidationBuilderModel
validates :field_one, numericality: true
end
m.valid?
m.errors[:base] << 'base error A'
m.errors[:field_one] << 'field_one error 1'
m.errors[:field_one] << 'field_one error 2'
m.errors[:field_two] << 'field_two error 1'
m.errors.add(:base, 'base error A')
m.errors.add(:field_one, 'field_one error 1')
m.errors.add(:field_one, 'field_one error 2')
m.errors.add(:field_two, 'field_two error 1')
end
end

Expand Down Expand Up @@ -73,8 +73,8 @@ class Dough::Forms::Builders::ValidationBuilderModel
end

it 'lists all errors for the object' do
model.errors.each do |_field, error|
expect(validation_summary).to include(error)
model.errors.each do |error|
expect(validation_summary).to include(error.message)
end

expect(validation_summary).to include('Field one is not a number')
Expand Down Expand Up @@ -149,9 +149,9 @@ class Dough::Forms::Builders::ValidationBuilderModel
context 'when there are multiple objects' do
let(:another_model) do
Dough::Forms::Builders::ValidationBuilderModel.new.tap do |m|
m.errors[:field_a] << 'field_a error a'
m.errors[:field_a] << 'field_a error b'
m.errors[:field_b] << 'field_b error a'
m.errors.add(:field_a, 'field_a error a')
m.errors.add(:field_a, 'field_a error b')
m.errors.add(:field_b, 'field_b error a')
end
end

Expand All @@ -162,8 +162,8 @@ class Dough::Forms::Builders::ValidationBuilderModel

it 'lists all errors for the objects' do
[model, another_model].each do |m|
m.errors.each do |_field, error|
expect(subject.validation_summary).to include(error)
m.errors.each do |error|
expect(subject.validation_summary).to include(error.message)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/dough/helpers/form_row_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def index_with_class

def index_with_error
@user = User.new
@user.errors[:name] << 'fail'
@user.errors.add(:name, 'fail')
render inline: '''
<%= form_for @user, url: "/" do |f| %>
<%= f.form_row(:name) do |row| %>
Expand Down

0 comments on commit 13a9483

Please sign in to comment.