-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce SingularAssociation and CollectionAssociation to tidy up th…
…e views
- Loading branch information
Showing
18 changed files
with
262 additions
and
177 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
41 changes: 5 additions & 36 deletions
41
app/views/rails_admin/main/_form_filtering_multiselect.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
25 changes: 6 additions & 19 deletions
25
app/views/rails_admin/main/_form_filtering_select.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 |
---|---|---|
@@ -1,34 +1,21 @@ | ||
<% | ||
config = field.associated_model_config | ||
source_abstract_model = RailsAdmin.config(form.object.class).abstract_model | ||
|
||
current_action = params[:action].in?(['create', 'new']) ? 'create' : 'update' | ||
|
||
edit_url = authorized?(:edit, config.abstract_model) ? edit_path(model_name: config.abstract_model.to_param, modal: true, id: '__ID__') : '' | ||
|
||
xhr = !field.associated_collection_cache_all | ||
|
||
collection = xhr ? [[field.formatted_value, field.selected_id]] : controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_primary_key).to_s] } | ||
|
||
js_data = { | ||
xhr: xhr, | ||
remote_source: index_path(config.abstract_model.to_param, source_object_id: form.object.id, source_abstract_model: source_abstract_model.to_param, associated_collection: field.name, current_action: current_action, compact: true), | ||
scopeBy: field.dynamic_scope_relationships | ||
} | ||
%> | ||
|
||
<div class="row"> | ||
<div class="col-sm-4"> | ||
<% selected_id = (hdv = field.form_default_value).nil? ? field.selected_id : hdv %> | ||
<%= form.select field.method_name, collection, { selected: selected_id, include_blank: true }, field.html_attributes.reverse_merge({ data: { filteringselect: true, options: js_data.to_json }, placeholder: t('admin.misc.search') }) %> | ||
<%= | ||
form.select field.method_name, field.collection, { selected: field.form_value, include_blank: true }, | ||
field.html_attributes.reverse_merge({ data: { filteringselect: true, options: field.widget_options.to_json }, placeholder: t('admin.misc.search') }) | ||
%> | ||
</div> | ||
<div class="col-sm-8 mt-2 mt-md-0 modal-actions"> | ||
<% if authorized?(:new, config.abstract_model) && field.inline_add %> | ||
<% path_hash = { model_name: config.abstract_model.to_param, modal: true }.merge!(field.associated_prepopulate_params) %> | ||
<%= link_to "<i class=\"fas fa-plus\"></i> ".html_safe + wording_for(:link, :new, config.abstract_model), '#', data: { link: new_path(path_hash) }, class: "btn btn-info create" %> | ||
<% end %> | ||
<% if edit_url.present? && field.inline_edit %> | ||
<%= link_to "<i class=\"fas fa-pencil-alt\"></i> ".html_safe + wording_for(:link, :edit, config.abstract_model), '#', data: { link: edit_url }, class: "btn btn-info update ms-2#{' disabled' if field.value.nil?}" %> | ||
<% if authorized?(:edit, config.abstract_model) && field.inline_edit %> | ||
<%= link_to "<i class=\"fas fa-pencil-alt\"></i> ".html_safe + wording_for(:link, :edit, config.abstract_model), '#', data: { link: edit_path(model_name: config.abstract_model.to_param, modal: true, id: '__ID__') }, class: "btn btn-info update ms-2#{' disabled' if field.value.nil?}" %> | ||
<% end %> | ||
</div> | ||
</div> |
33 changes: 12 additions & 21 deletions
33
app/views/rails_admin/main/_form_polymorphic_association.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 |
---|---|---|
@@ -1,32 +1,23 @@ | ||
<% | ||
type_collection = field.polymorphic_type_collection | ||
type_column = field.association.foreign_type.to_s | ||
selected_type = field.bindings[:object].send(type_column) | ||
selected = field.bindings[:object].send(field.association.name) | ||
collection = selected ? [[field.formatted_value, selected.id]] : [[]] | ||
column_type_dom_id = form.dom_id(field).sub(field.method_name.to_s, type_column) | ||
current_action = params[:action].in?(['create', 'new']) ? 'create' : 'update' | ||
|
||
default_options = { float_left: false } | ||
|
||
js_data = type_collection.inject({}) do |options, model| | ||
source_abstract_model = RailsAdmin.config(form.object.class).abstract_model | ||
options.merge(model.second.downcase.gsub('::', '-') => { | ||
xhr: true, | ||
remote_source: index_path(model.second.underscore, source_object_id: form.object.id, source_abstract_model: source_abstract_model.to_param, current_action: current_action, compact: true), | ||
float_left: false | ||
}) | ||
end | ||
column_type_dom_id = form.dom_id(field).sub(field.method_name.to_s, field.type_column) | ||
%> | ||
|
||
<div class="row"> | ||
<div class="col-sm-3"> | ||
<% js_data.each do |model, value| %> | ||
<% field.widget_options_for_types.each do |model, value| %> | ||
<div data-options="<%= value.to_json %>" id="<%= model %>-js-options"></div> | ||
<% end %> | ||
<%= form.select type_column, type_collection, {include_blank: true, selected: selected_type}, class: "form-select", id: column_type_dom_id, data: { polymorphic: true, urls: field.polymorphic_type_urls.to_json }, style: "float: left; margin-right: 10px;" %> | ||
<%= | ||
form.select field.type_column, field.type_collection, {include_blank: true, selected: field.selected_type}, | ||
class: "form-select", id: column_type_dom_id, data: { polymorphic: true, urls: field.type_urls.to_json }, | ||
style: "float: left; margin-right: 10px;" | ||
%> | ||
</div> | ||
<div class="col-sm-4"> | ||
<%= form.select field.method_name, collection, {include_blank: true, selected: selected.try(:id)}, class: "form-control", data: { filteringselect: true, options: js_data[selected_type.try(:downcase)] || default_options }, placeholder: 'Search' %> | ||
<%= | ||
form.select field.method_name, field.collection, {include_blank: true, selected: field.selected_id}, | ||
class: "form-control", data: { filteringselect: true, options: field.widget_options }, | ||
placeholder: 'Search' | ||
%> | ||
</div> | ||
</div> |
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
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,73 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_admin/config/fields/association' | ||
|
||
module RailsAdmin | ||
module Config | ||
module Fields | ||
class CollectionAssociation < Association | ||
# orderable associated objects | ||
register_instance_option :orderable do | ||
false | ||
end | ||
|
||
register_instance_option :partial do | ||
nested_form ? :form_nested_many : :form_filtering_multiselect | ||
end | ||
|
||
def collection(scope = nil) | ||
if scope | ||
super | ||
elsif associated_collection_cache_all | ||
selected = selected_ids | ||
i = 0 | ||
super.sort_by { |a| [selected.index(a[1]) || selected.size, i += 1] } | ||
else | ||
value.map { |o| [o.send(associated_object_label_method), o.send(associated_primary_key)] } | ||
end | ||
end | ||
|
||
def associated_prepopulate_params | ||
{associated_model_config.abstract_model.param_key => {association.foreign_key => bindings[:object].try(:id)}} | ||
end | ||
|
||
def multiple? | ||
true | ||
end | ||
|
||
def selected_ids | ||
value.map { |s| s.send(associated_primary_key).to_s } | ||
end | ||
|
||
def form_default_value | ||
(default_value if bindings[:object].new_record? && value.empty?) | ||
end | ||
|
||
def form_value | ||
form_default_value.nil? ? selected_ids : form_default_value | ||
end | ||
|
||
def widget_options | ||
{ | ||
xhr: !associated_collection_cache_all, | ||
'edit-url': (inline_edit && bindings[:view].authorized?(:edit, associated_model_config.abstract_model) ? bindings[:view].edit_path(model_name: associated_model_config.abstract_model.to_param, id: '__ID__') : ''), | ||
remote_source: bindings[:view].index_path(associated_model_config.abstract_model, source_object_id: bindings[:object].id, source_abstract_model: abstract_model.to_param, associated_collection: name, current_action: bindings[:view].current_action, compact: true), | ||
scopeBy: dynamic_scope_relationships, | ||
sortable: !!orderable, | ||
removable: !!removable, | ||
cacheAll: !!associated_collection_cache_all, | ||
regional: { | ||
add: ::I18n.t('admin.misc.add_new'), | ||
chooseAll: ::I18n.t('admin.misc.chose_all'), | ||
clearAll: ::I18n.t('admin.misc.clear_all'), | ||
down: ::I18n.t('admin.misc.down'), | ||
remove: ::I18n.t('admin.misc.remove'), | ||
search: ::I18n.t('admin.misc.search'), | ||
up: ::I18n.t('admin.misc.up'), | ||
}, | ||
} | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_admin/config/fields/association' | ||
|
||
module RailsAdmin | ||
module Config | ||
module Fields | ||
class SingularAssociation < Association | ||
register_instance_option :filter_operators do | ||
%w[_discard like not_like is starts_with ends_with] + (required? ? [] : %w[_separator _present _blank]) | ||
end | ||
|
||
register_instance_option :formatted_value do | ||
(o = value) && o.send(associated_model_config.object_label_method) | ||
end | ||
|
||
register_instance_option :partial do | ||
nested_form ? :form_nested_one : :form_filtering_select | ||
end | ||
|
||
def collection(scope = nil) | ||
if associated_collection_cache_all || scope | ||
super | ||
else | ||
[[formatted_value, selected_id]] | ||
end | ||
end | ||
|
||
def multiple? | ||
false | ||
end | ||
|
||
def selected_id | ||
raise NoMethodError # abstract | ||
end | ||
|
||
def form_value | ||
form_default_value.nil? ? selected_id : form_default_value | ||
end | ||
|
||
def widget_options | ||
{ | ||
xhr: !associated_collection_cache_all, | ||
remote_source: bindings[:view].index_path(associated_model_config.abstract_model, source_object_id: bindings[:object].id, source_abstract_model: abstract_model.to_param, associated_collection: name, current_action: bindings[:view].current_action, compact: true), | ||
scopeBy: dynamic_scope_relationships, | ||
} | ||
end | ||
end | ||
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
4 changes: 2 additions & 2 deletions
4
lib/rails_admin/config/fields/types/has_and_belongs_to_many_association.rb
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
Oops, something went wrong.