Skip to content

Commit a9eda33

Browse files
committed
Add support for Ransack 4
`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.
1 parent 99b547f commit a9eda33

File tree

6 files changed

+44
-0
lines changed

6 files changed

+44
-0
lines changed

app/models/alchemy/base_record.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ def self.table_name_prefix
55
end
66

77
class BaseRecord < ActiveRecord::Base
8+
extend Alchemy::SearchableResource
9+
810
self.abstract_class = true
911
end
1012
end

lib/alchemy/searchable_resource.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
module Alchemy
4+
# Defines the methods that are needed to
5+
# make a model searchable in Alchemy's admin search by Ransack.
6+
module SearchableResource
7+
SEARCHABLE_COLUMN_TYPES = %i[string text]
8+
9+
# Allow all string and text attributes to be searchable by Ransack.
10+
def ransackable_attributes(_auth_object = nil)
11+
searchable_alchemy_resource_attributes
12+
end
13+
14+
# Allow all attributes to be sortable by Ransack.
15+
def ransortable_attributes(_auth_object = nil)
16+
columns.map(&:name)
17+
end
18+
19+
# Allow all associations defined in +alchemy_resource_relations+ to be searchable by Ransack.
20+
def ransackable_associations(_auth_object = nil)
21+
searchable_alchemy_resource_associations
22+
end
23+
24+
protected
25+
26+
def searchable_alchemy_resource_attributes
27+
columns.select { |c| c.type.in?(SEARCHABLE_COLUMN_TYPES) }.map(&:name)
28+
end
29+
30+
def searchable_alchemy_resource_associations
31+
if respond_to?(:alchemy_resource_relations)
32+
alchemy_resource_relations.keys.map!(&:to_s)
33+
else
34+
[]
35+
end
36+
end
37+
end
38+
end

lib/alchemy_cms.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
require_relative "alchemy/paths"
5252
require_relative "alchemy/permissions"
5353
require_relative "alchemy/resource"
54+
require_relative "alchemy/searchable_resource"
5455
require_relative "alchemy/tinymce"
5556
require_relative "alchemy/taggable"
5657
require_relative "alchemy/version"

spec/dummy/app/models/booking.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
22

33
class Booking < ActiveRecord::Base
4+
extend Alchemy::SearchableResource
45
end

spec/dummy/app/models/event.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
class Event < ActiveRecord::Base
4+
extend Alchemy::SearchableResource
45
include Alchemy::Taggable
56

67
validates_presence_of :name

spec/dummy/app/models/location.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
class Location < ActiveRecord::Base
4+
extend Alchemy::SearchableResource
45
include Alchemy::Taggable
56
has_many :events
67
end

0 commit comments

Comments
 (0)