Skip to content

Commit

Permalink
Add support for Ransack 4
Browse files Browse the repository at this point in the history
`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
tvdeyen committed Mar 11, 2023
1 parent 99b547f commit a9eda33
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/alchemy/base_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ def self.table_name_prefix
end

class BaseRecord < ActiveRecord::Base
extend Alchemy::SearchableResource

self.abstract_class = true
end
end
38 changes: 38 additions & 0 deletions lib/alchemy/searchable_resource.rb
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
1 change: 1 addition & 0 deletions lib/alchemy_cms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
require_relative "alchemy/paths"
require_relative "alchemy/permissions"
require_relative "alchemy/resource"
require_relative "alchemy/searchable_resource"
require_relative "alchemy/tinymce"
require_relative "alchemy/taggable"
require_relative "alchemy/version"
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/models/booking.rb
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
1 change: 1 addition & 0 deletions spec/dummy/app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class Event < ActiveRecord::Base
extend Alchemy::SearchableResource
include Alchemy::Taggable

validates_presence_of :name
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/models/location.rb
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

0 comments on commit a9eda33

Please sign in to comment.