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

5.3.0 - Custom Inputs Fail to Load #1824

Open
BeatyThomas opened this issue Nov 8, 2023 · 3 comments
Open

5.3.0 - Custom Inputs Fail to Load #1824

BeatyThomas opened this issue Nov 8, 2023 · 3 comments

Comments

@BeatyThomas
Copy link

Environment

  • Ruby [2.7.8]
  • Rails [5.2]
  • Simple Form [5.3.0]

Current behavior

This commit is a breaking change that prevents custom inputs in app/input from being loaded.

Expected behavior

Adding the removed snippit returns normal functionality.

@lmrodriguezr
Copy link

I seem to be experiencing this issue, but only in development, not in production. I have looked everywhere for possible configurations in the environments that could account for the difference but I can't find it anywhere. Any ideas?

lmrodriguezr added a commit to seq-code/registry that referenced this issue Dec 8, 2023
But downgrade `simple_form`, see heartcombo/simple_form#1824
@mark-dce
Copy link

mark-dce commented Jan 9, 2024

I'm having the issue in both development and test environments. It appears to be related to how Rails lazy-loads classes in those environments vs. eager loading in production. My current quick-and-dirty work-around is to reference the custom input class names in an initializer, e.g.:

config/iniitilizers/simple_form.rb

# ...
SimpleForm.setup do |config|
    # ...setup stuff here..
end

# Added at the bottom of the file
# Explicitly reference the custom input classes
CurrencyInput
CustomInput
MultiValueInput
# End of File

Alternatively, it also works to locally override the SimpleForm::FormBuilder#attempt_mapping method with the code from v5.2.0:

def attempt_mapping(mapping, at)
return if SimpleForm.inputs_discovery == false && at == Object
begin
at.const_get(mapping)
rescue NameError => e
raise if e.message !~ /#{mapping}$/
end
end

This works because it always attempts to load the class and raises an error if it's not present.

As opposed to the v5.3.0 version which never executes because the lazy loaded class constant isn't loaded, and therefore isn't defined yet:

def attempt_mapping(mapping, at)
return if SimpleForm.inputs_discovery == false && at == Object
at.const_get(mapping) if at.const_defined?(mapping)
end

@dwkoogt
Copy link

dwkoogt commented Mar 18, 2024

I use a custom form builder that has custom fields defined in it and this change breaks it too.
It seems the change will pass the specs but you won't see it break unless you actually have custom inputs or custom form builder in your dev environment.

The intention of the calling method find_mapping attempts to map a field name to an input class and it usually finds it before getting to the attempt_mapping method. The attempt_mapping method is reached when you defined some custom field hence you have to have a custom input class or have it defined in a custom form builder. These are loaded lazily in dev so the code change that broke it made the method not do what it was supposed to do.

So, for general input types attempt_mapping not being called.

If you do not want this feature, you'd simply set inputs_discovery option to false.
You can also create a custom builder and map custom input classes using map_type class method.
Or there may a configuration option to map as well such that attempt_mapping is not called (not sure this exists).

The try catch is used because it is attempting to find a class as the method name implies.

If the argument for the change is performance, you'd only see little improvement unless you have a ton of custom inputs. So, to me the change is not really worthwhile and should be reverted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants