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

Add ability to inherit custom fields from RailsAdmin::Config::Fields::Association #1726

Merged
merged 1 commit into from
Jun 12, 2014
Merged

Add ability to inherit custom fields from RailsAdmin::Config::Fields::Association #1726

merged 1 commit into from
Jun 12, 2014

Conversation

scarfacedeb
Copy link
Contributor

I'm writing new custom field for globalize3 integration and I noticed that you get undefined method [] for nil:NilClass error due to empty @properties variable that association method returns in label method:

register_instance_option :label do
  (@label ||= {})[::I18n.locale] ||= abstract_model.model.human_attribute_name association[:name]
end

field.properties instance variable is lost when rails_admin calls configure method inside edit block:

edit do
  configure :translations, :globalize3_tabs
end

field method is called inside configure method, which removes this field from _fields array (because its type hasn't been found in the default stack), and creates it again as my custom type.

In the process, it gets properties for this field from an abstract_model.properties, but it's nil, because it's not a abstract_model's property, but it's an association.

…:Association

field.properties instance variable is lost when you configure new custom field
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.34%) when pulling e8f5f17 on scarfaceDeb:custom_field_from_associations into 0be9e22 on sferik:master.

@scarfacedeb
Copy link
Contributor Author

I'm refactoring rails_admin_globalize_field and I've run into this issue again.

Can somebody address it?


I suggest the same solution as before - just use already defined field.properties value, because I don't see any reason to assign new value to properties.
In the current case, it assigns the same value as field.properties for the simple fields (returned by model.columns), and it fails to find any value in my case, because it ignores associations.
But we've already dealt with this inside self.factory method!

If there's a reason why we can't reuse field.properties, I think we can add additional properties find for associations inside 'field' method.

@scarfacedeb
Copy link
Contributor Author

A possible workaround for now:

# inside RailsAdmin::Config::Fields::Types::CustomType
def initialize(parent, name, properties)
  properties = parent.abstract_model.associations.detect { |p| name == p[:name] }
  super
end

@mshibuya mshibuya merged commit e8f5f17 into railsadminteam:master Jun 12, 2014
mshibuya added a commit that referenced this pull request Jun 12, 2014
@mshibuya
Copy link
Member

Thanks and sorry for being late!

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

Successfully merging this pull request may close these issues.

None yet

3 participants