Skip to content

Commit

Permalink
Support for Ransack v4
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Sjöblom committed Oct 12, 2023
1 parent 39d377c commit 227d290
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion alchemy_cms.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency "kaminari", ["~> 1.1"]
gem.add_runtime_dependency "originator", ["~> 3.1"]
gem.add_runtime_dependency "non-stupid-digest-assets", ["~> 1.0.8"]
gem.add_runtime_dependency "ransack", [">= 1.8", "< 4.0"]
gem.add_runtime_dependency "ransack", [">= 1.8", "< 5.0"]
gem.add_runtime_dependency "request_store", ["~> 1.2"]
gem.add_runtime_dependency "responders", [">= 2.0", "< 4.0"]
gem.add_runtime_dependency "sassc-rails", ["~> 2.1"]
Expand Down
14 changes: 7 additions & 7 deletions app/controllers/alchemy/admin/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TagsController < ResourcesController
before_action :load_tag, only: [:edit, :update, :destroy]

def index
@query = Gutentag::Tag.ransack(search_filter_params[:q])
@query = Tag.ransack(search_filter_params[:q])
@query.sorts = default_sort_order if @query.sorts.empty?
@tags = @query
.result
Expand All @@ -16,21 +16,21 @@ def index
end

def new
@tag = Gutentag::Tag.new
@tag = Tag.new
end

def create
@tag = Gutentag::Tag.create(tag_params)
@tag = Tag.create(tag_params)
render_errors_or_redirect @tag, admin_tags_path, Alchemy.t("New Tag Created")
end

def edit
@tags = Gutentag::Tag.order("name ASC").to_a - [@tag]
@tags = Tag.order("name ASC").to_a - [@tag]
end

def update
if tag_params[:merge_to]
@new_tag = Gutentag::Tag.find(tag_params[:merge_to])
@new_tag = Tag.find(tag_params[:merge_to])
Tag.replace(@tag, @new_tag)
operation_text = Alchemy.t("Replaced Tag") % {old_tag: @tag.name, new_tag: @new_tag.name}
@tag.destroy
Expand All @@ -57,7 +57,7 @@ def autocomplete
private

def load_tag
@tag = Gutentag::Tag.find(params[:id])
@tag = Tag.find(params[:id])
end

def tag_params
Expand All @@ -67,7 +67,7 @@ def tag_params
def tags_from_term(term)
return [] if term.blank?

Gutentag::Tag.where(["LOWER(name) LIKE ?", "#{term.downcase}%"])
Tag.where(["LOWER(name) LIKE ?", "#{term.downcase}%"])
end

def json_for_autocomplete(items, attribute)
Expand Down
2 changes: 2 additions & 0 deletions app/models/alchemy/base_record.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true
module Alchemy
extend Alchemy::SearchableResource

def self.table_name_prefix
"alchemy_"
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/alchemy/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
# The original Tag model is Gutentag::Tag
module Alchemy
class Tag < Gutentag::Tag
def self.ransackable_attributes(_auth_object = nil)
%w[created_at id name taggings_count updated_at]
end

def self.ransackable_associations(_auth_object = nil)
%w[taggings]
end

# Replaces tag with new tag on all models tagged with tag.
def self.replace(tag, new_tag)
tag.taggings.collect(&:taggable).each do |taggable|
Expand Down
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 @@ -53,6 +53,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
1 change: 1 addition & 0 deletions spec/dummy/app/models/series.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
class Series < ActiveRecord::Base
extend Alchemy::SearchableResource
end

0 comments on commit 227d290

Please sign in to comment.