-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for other types of association fields (#1176)
After merging thoughtbot/administrate#945, a problem surfaced with plugins that provide new types of association fields, such as https://github.com/pablobm/administrate-field-nested_has_many. `Administrate::BaseDashboard` hard-codes the possible types of association fields in a list that is later used to determined the permitted params. This means that new types of fields cannot make permitted params and things break. My proposed solution (see `lib/administrate/base_dashboard.rb`) does a programmatic search for field classes that inherit from `Administrate::Field::Associative`. This allows plugin authors not to worry about adding their new type to a list or any other sort of setup. A potential problem with this approach is that it includes `Administrate::Field::Polymorphic` on the list. I don't know if this can cause problems down the line or not.
- Loading branch information
svqualitydev
committed
Oct 5, 2018
1 parent
bde8417
commit 78ba21c
Showing
7 changed files
with
71 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
spec/example_app/app/views/fields/has_many_variant/_form.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<%# | ||
Just a copy of the HasMany _form partial, here to test that | ||
things won't break if the app adds new types of association fields | ||
%> | ||
|
||
<div class="field-unit__label"> | ||
<%= f.label field.attribute_key, field.attribute %> | ||
</div> | ||
<div class="field-unit__field"> | ||
<%= f.select(field.attribute_key, nil, {}, multiple: true) do %> | ||
<%= options_for_select(field.associated_resource_options, field.selected_options) %> | ||
<% end %> | ||
</div> |
6 changes: 6 additions & 0 deletions
6
spec/example_app/app/views/fields/has_many_variant/_index.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<%# | ||
Just a copy of the HasMany _index partial, here to test that | ||
things won't break if the app adds new types of association fields | ||
%> | ||
|
||
<%= pluralize(field.data.size, field.attribute.to_s.humanize.downcase.singularize) %> |
23 changes: 23 additions & 0 deletions
23
spec/example_app/app/views/fields/has_many_variant/_show.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<%# | ||
Just a copy of the HasMany _show partial, here to test that | ||
things won't break if the app adds new types of association fields | ||
%> | ||
|
||
<% if field.resources.any? %> | ||
<% order = field.order_from_params(params.fetch(field.name, {})) %> | ||
<% page_number = params.fetch(field.name, {}).fetch(:page, nil) %> | ||
<%= render( | ||
"collection", | ||
collection_presenter: field.associated_collection(order), | ||
collection_field_name: field.name, | ||
page: page, | ||
resources: field.resources(page_number, order), | ||
table_title: field.name, | ||
) %> | ||
<% if field.more_than_limit? %> | ||
<%= paginate field.resources(page_number), param_name: "#{field.name}[page]" %> | ||
<% end %> | ||
|
||
<% else %> | ||
<%= t("administrate.fields.has_many.none", default: "–") %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "administrate/field/has_many" | ||
|
||
module Administrate | ||
module Field | ||
class HasManyVariant < HasMany | ||
# Only here to test that this works out of the box | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters