Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt errors for active admin #44

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/yaaf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'active_record'
require 'yaaf/form'
require 'yaaf/version'
require 'yaaf/active_admin'

# Namespace for all objects in YAAF
module YAAF
Expand Down
31 changes: 31 additions & 0 deletions lib/yaaf/active_admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module YAAF
module ActiveAdmin
extend ActiveSupport::Concern

included do
after_validation :sanitize_errors

delegate_missing_to :main_model

def self.reflect_on_association(*args)
main_model_class.reflect_on_association(*args)
end

private

def sanitize_errors
form_attributes_validations = self.class.validators.map(&:attributes).flatten
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
form_attributes_validations = self.class.validators.map(&:attributes).flatten
form_attributes_validations = self.class.validators.flat_map(&:attributes)

form_attributes_validations.each do |form_attributes_validation|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question, imagine I have the model User with an attribute username with a presence validation, and the form object UserForm with a length validation on the username. both errors will end up in user_form.errors[:username] right? In that case we don't want to move it up to the base because it already has a field were to be displayed, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @santib, you're right about this. We should figure out another way to only move to base the form object validations errors. Maybe running this sanitize_errors before we move the model errors into the form, wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andresg4 that will solve the model error to be displayed in the correct field, but the form validation shouldn't be moved either if it has a specific field where to be displayed. My thinking is that if an error is set in an attribute that will be displayed in the page we don't do anything, if it doesn't have a place in the page we move it to the base. The question is how can we know that 🤔

message = errors.messages[form_attributes_validation]
errors.add(:base, message) if message.present?
end
end

def main_model
models.first
end
end
end
end