-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`extend` your class with `Alchemy::SearchableResource` to provide necessary methods for Ransack to allow searching and sorting attributes and associations. By default it allows all string and text attributes to be searched and all columns to be sorted. No association are allowed by default. Existing `searchable_alchemy_resource_attributes` and `alchemy_resource_relations` definitions are respected.
- Loading branch information
Showing
6 changed files
with
44 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
# Defines the methods that are needed to | ||
# make a model searchable in Alchemy's admin search by Ransack. | ||
module SearchableResource | ||
SEARCHABLE_COLUMN_TYPES = %i[string text] | ||
|
||
# Allow all string and text attributes to be searchable by Ransack. | ||
def ransackable_attributes(auth_object = nil) | ||
searchable_alchemy_resource_attributes | ||
end | ||
|
||
# Allow all attributes to be sortable by Ransack. | ||
def ransortable_attributes(auth_object = nil) | ||
columns.map(&:name) | ||
end | ||
|
||
# Allow all associations defined in +alchemy_resource_relations+ to be searchable by Ransack. | ||
def ransackable_associations(auth_object = nil) | ||
searchable_alchemy_resource_associations | ||
end | ||
|
||
protected | ||
|
||
def searchable_alchemy_resource_attributes | ||
columns.select { |c| c.type.in?(SEARCHABLE_COLUMN_TYPES) }.map(&:name) | ||
end | ||
|
||
def searchable_alchemy_resource_associations | ||
if respond_to?(:alchemy_resource_relations) | ||
alchemy_resource_relations.keys.map!(&:to_s) | ||
else | ||
[] | ||
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
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,4 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class Booking < ActiveRecord::Base | ||
extend Alchemy::SearchableResource | ||
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
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,6 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class Location < ActiveRecord::Base | ||
extend Alchemy::SearchableResource | ||
include Alchemy::Taggable | ||
has_many :events | ||
end |