-
-
Notifications
You must be signed in to change notification settings - Fork 17
Support all ransack predicates #25
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| <% association = model.reflections[field.to_s] %> | ||
| <% if association %> | ||
| <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_id_eq" %> | ||
| <% desc = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %> | ||
| <% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %> | ||
| <% field_key = AdministrateRansack.ransack?(model, {field => "1,2"}) ? field : "#{field}_id_eq" %> | ||
| <% label ||= AdministrateRansack.ransack?(model, {field => "1,2"}) ? field : "#{field}_id" %> | ||
| <% resource_field = type.new(field, nil, Administrate::Page::Collection.new(@dashboard), resource: model.new) %> | ||
| <% collection = resource_field.associated_resource_options %> | ||
|
|
||
| <%= form.label(label, class: 'filter-label') %> | ||
| <%= form.collection_select(field_key, collection, :id, desc, include_blank: true) %> | ||
| <%= form.select("#{field}_id_eq", collection, { include_blank: true }, { class: 'selectize' }) %> | ||
| <% end %> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %> | ||
| <% field_key = AdministrateRansack.ransack?(model, {field => "true"}) ? field : "#{field}_eq" %> | ||
| <% values = [[t('administrate_ransack.filters.no'), false], [t('administrate_ransack.filters.yes'), true]] %> | ||
|
|
||
| <%= form.label(label, class: 'filter-label') %> | ||
| <%= form.label(label || field, class: 'filter-label') %> | ||
| <%= form.select(field_key, values, include_blank: true) %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %> | ||
|
|
||
| <%= form.label(label, class: 'filter-label') %> | ||
| <%= form.number_field(field_key) %> | ||
| <%= form.label(label || field, class: 'filter-label') %> | ||
| <% if AdministrateRansack.ransack?(model, {field => "1"}) %> | ||
| <%= form.number_field "#{field}" %> | ||
| <% else %> | ||
| <%= form.number_field "#{field}_gteq" %> | ||
| <%= form.number_field "#{field}_lteq" %> | ||
| <% end %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %> | ||
| <% collection = (type.respond_to?(:options) ? type.options[:collection] : []) || [] %> | ||
|
|
||
| <%= form.label(label, class: 'filter-label') %> | ||
| <%= form.label(label || field, class: 'filter-label') %> | ||
| <%= form.select(field_key, collection, include_blank: true) %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_cont" %> | ||
|
|
||
| <%= form.label(label, class: 'filter-label') %> | ||
| <%= form.search_field(field_key) %> | ||
| <% if AdministrateRansack.ransack?(model, {field => "valid"}) %> | ||
| <%= form.label(label || field, class: 'filter-label') %> | ||
| <%= form.search_field(field) %> | ||
| <% elsif AdministrateRansack.ransack?(model, {"#{field}_cont" => "valid"}) %> | ||
| <%= form.label(label || "#{field}_cont", class: 'filter-label') %> | ||
| <%= form.search_field("#{field}_cont") %> | ||
| <% else %> | ||
| <%# render nothing %> | ||
| <% end %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module AdministrateRansack | ||
| class << self | ||
| def ransack?(model, params = {}, options = {}) | ||
| ransack = model.ransack(params, **options) | ||
| ransack.instance_variable_get(:@scope_args).present? || ransack.base.c.present? | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The general idea is good but I prefer to avoid accessing internal Ransack details (like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is a feature currently missing in Ransack, and I'm hoping it will eventually be implemented there — though it hasn't happened yet (there are some related PRs, though). It might be faster to contribute directly to Ransack, but if we want to avoid that for now, how about something like the following as a workaround? def ransack?(model, params, **options)
begin
model.ransack!(params, **options)
true
rescue StandardError
false
end
end
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm... perhaps you could try something like: def ransack?(model, field)
ctx = Ransack::Context.for(model)
ctx.ransackable_attribute?(field, model) ||
ctx.ransackable_association?(field, model) ||
ctx.ransackable_scope?(field, model)
end
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, that only checks the model definition, but it doesn’t reflect which field names can actually be used in Ransack. One of the goals of this PR is to support Ransack-style search conditions like |
||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <section class="main-content__body main-content__body--flush"> | ||
| <%= render( | ||
| "collection", | ||
| collection_presenter: page, | ||
| collection_field_name: resource_name, | ||
| page: page, | ||
| resources: resources, | ||
| table_title: "page-title" | ||
| ) %> | ||
|
|
||
| <%= paginate resources, param_name: '_page' %> | ||
| </section> | ||
|
|
||
| <%= render( | ||
| 'administrate_ransack/filters', | ||
| attribute_types: @dashboard.class::RANSACK_TYPES | ||
| ) %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change, I prefer to have a specific PR for it. And it can be merged only with the next major version.