Skip to content

Commit 7d9478a

Browse files
committed
Fix partials to support all ransack predicates
1 parent 7605b61 commit 7d9478a

19 files changed

+127
-43
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,14 @@ end
5454
+ `attribute_types`: hash used to specify the filter fields, ex. `{ title: Administrate::Field::String }`
5555
+ `search_path`: the path to use for searching (form URL)
5656
+ `namespace`: the namespace used by Administrate, ex. `:supervisor`
57-
- For associations (_has many_/_belongs to_) the label used can be customized adding an `admin_label` method to the target model which returns a string while the collection can by filtered with `admin_scope`. Example:
57+
- For associations (_has many_/_belongs to_) the label used can be customized adding an `display_resource` method to the target dashboard which returns a string. Example:
5858

5959
```rb
60-
# Sample post model
61-
class Post < ApplicationRecord
62-
scope :admin_scope, -> { where(published: true) }
63-
64-
def admin_label
65-
title.upcase
60+
class PostDashboard < Administrate::BaseDashboard
61+
# Overwrite this method to customize how posts are displayed
62+
# across all pages of the admin dashboard.
63+
def display_resource(post)
64+
"##{post.id} #{post.title&.upcase}"
6665
end
6766
end
6867
```
@@ -139,9 +138,11 @@ end
139138
# In alternative prepare an hash in the dashboard like RANSACK_TYPES = {}
140139
attribute_types = {
141140
title: Administrate::Field::String,
141+
title_or_description_cont: Administrate::Field::String,
142142
author: Administrate::Field::BelongsTo,
143143
category: Administrate::Field::Select.with_options(collection: Post.categories.to_a),
144-
published: Administrate::Field::Boolean
144+
published: Administrate::Field::Boolean,
145+
updated_at_lteq: Administrate::Field::Date
145146
}
146147
attribute_labels = {
147148
author: 'Written by',

app/views/administrate_ransack/_filters.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<% attribute_types.each do |field, type| %>
2222
<% next if field == :id %>
2323

24-
<% label = attribute_labels.include?(field) ? attribute_labels[field] : field %>
24+
<% label = attribute_labels.include?(field) ? attribute_labels[field] : nil %>
2525
<% model = @ransack_results.klass %>
2626
<% input_type = type.is_a?(Administrate::Field::Deferred) ? type.deferred_class.to_s : type.to_s %>
2727
<% component = AdministrateRansack::FILTERS[input_type] || 'field_other' %>
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<% association = model.reflections[field.to_s] %>
22
<% if association %>
3-
<% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_id_eq" %>
4-
<% desc = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
5-
<% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
3+
<% field_key = AdministrateRansack.ransack?(model, {field => "1,2"}) ? field : "#{field}_id_eq" %>
4+
<% label ||= AdministrateRansack.ransack?(model, {field => "1,2"}) ? field : "#{field}_id" %>
5+
<% resource_field = type.new(field, nil, Administrate::Page::Collection.new(@dashboard), resource: model.new) %>
6+
<% collection = resource_field.associated_resource_options %>
67

78
<%= form.label(label, class: 'filter-label') %>
8-
<%= form.collection_select(field_key, collection, :id, desc, include_blank: true) %>
9+
<%= form.select("#{field}_id_eq", collection, { include_blank: true }, { class: 'selectize' }) %>
910
<% end %>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %>
1+
<% field_key = AdministrateRansack.ransack?(model, {field => "true"}) ? field : "#{field}_eq" %>
22
<% values = [[t('administrate_ransack.filters.no'), false], [t('administrate_ransack.filters.yes'), true]] %>
33

4-
<%= form.label(label, class: 'filter-label') %>
4+
<%= form.label(label || field, class: 'filter-label') %>
55
<%= form.select(field_key, values, include_blank: true) %>

app/views/administrate_ransack/components/_field_date.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<%= form.label(label, class: 'filter-label') %>
2-
<% if model.ransackable_scopes.include?(field) %>
1+
<%= form.label(label || field, class: 'filter-label') %>
2+
<% if AdministrateRansack.ransack?(model, {field => Date.today}) %>
33
<%= form.date_field(field, value: form.object.send(field)) %>
44
<% else %>
55
<%= form.date_field("#{field}_gteq") %>

app/views/administrate_ransack/components/_field_datetime.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<%= form.label(label, class: 'filter-label') %>
2-
<% if model.ransackable_scopes.include?(field) %>
1+
<%= form.label(label || field, class: 'filter-label') %>
2+
<% if AdministrateRansack.ransack?(model, {field => DateTime.now}) %>
33
<%= form.datetime_field(field, value: form.object.send(field)) %>
44
<% else %>
55
<%= form.datetime_field("#{field}_gteq") %>

app/views/administrate_ransack/components/_field_has_many.html.erb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
<% association = model.reflections[field.to_s] %>
22
<% if association %>
3-
<% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_id_in" %>
4-
<% desc = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
5-
<% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
3+
<% field_key = AdministrateRansack.ransack?(model, {field => "1,2"}) ? field : "#{field}_id_in" %>
4+
<% resource_field = type.new(field, nil, Administrate::Page::Collection.new(@dashboard), resource: model.new) %>
5+
<% collection = resource_field.associated_resource_options %>
66

7-
<%= form.label(label, class: 'filter-label') %>
7+
<%= form.label(label || field_key, class: 'filter-label') %>
88
<% if options&.include? 'select' %>
9-
<%= form.select(field_key, nil, {}, multiple: true) do %>
10-
<%= options_from_collection_for_select(collection, :id, desc) %>
11-
<% end %>
9+
<%= form.select(field_key, collection, {}, multiple: true) %>
1210
<% else %>
13-
<%= form.collection_check_boxes(field_key, collection, :id, desc) do |b| %>
11+
<%= form.collection_check_boxes(field_key, collection, :second, :first) do |b| %>
1412
<%= b.label do %>
1513
<%= b.check_box %>
16-
<span><%= b.object.send(desc) %></span>
14+
<span><%= b.text %></span>
1715
<% end %>
1816
<% end %>
1917
<% end %>
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %>
2-
3-
<%= form.label(label, class: 'filter-label') %>
4-
<%= form.number_field(field_key) %>
1+
<%= form.label(label || field, class: 'filter-label') %>
2+
<% if AdministrateRansack.ransack?(model, {field => "1"}) %>
3+
<%= form.number_field "#{field}" %>
4+
<% else %>
5+
<%= form.number_field "#{field}_gteq" %>
6+
<%= form.number_field "#{field}_lteq" %>
7+
<% end %>

app/views/administrate_ransack/components/_field_other.html.erb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
<%= form.label(label, class: 'filter-label') %>
2-
<%= form.search_field(field) %>
1+
<% if AdministrateRansack.ransack?(model, {field => "valid"}) %>
2+
<%= form.label(label || field, class: 'filter-label') %>
3+
<%= form.search_field(field) %>
4+
<% elsif AdministrateRansack.ransack?(model, {"#{field}_cont" => "valid"}) %>
5+
<%= form.label(label || "#{field}_cont", class: 'filter-label') %>
6+
<%= form.search_field("#{field}_cont") %>
7+
<% else %>
8+
<%# render nothing %>
9+
<% end %>
310

411
<%# unsupported Field::HasOne %>
512
<%# unsupported Field::Polymorphic %>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %>
22
<% collection = (type.respond_to?(:options) ? type.options[:collection] : []) || [] %>
33

4-
<%= form.label(label, class: 'filter-label') %>
4+
<%= form.label(label || field, class: 'filter-label') %>
55
<%= form.select(field_key, collection, include_blank: true) %>

0 commit comments

Comments
 (0)