Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ransackable attributes to tags #2487

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
21 changes: 14 additions & 7 deletions spec/controllers/alchemy/admin/tags_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ module Admin

before { authorize_user(:as_admin) }

describe "#index" do
it "renders index template" do
get :index
expect(response).to be_successful
end
end

describe "#create" do
context "without required params" do
render_views
Expand All @@ -23,30 +30,30 @@ module Admin
it "creates tag and redirects to tags view" do
expect {
post :create, params: {tag: {name: "Foo"}}
}.to change { Gutentag::Tag.count }.by(1)
}.to change { Alchemy::Tag.count }.by(1)
expect(response).to redirect_to admin_tags_path
end
end
end

describe "#edit" do
let(:tag) { Gutentag::Tag.create(name: "Sputz") }
let(:another_tag) { Gutentag::Tag.create(name: "Hutzl") }
let(:tag) { Alchemy::Tag.create(name: "Sputz") }
let(:another_tag) { Alchemy::Tag.create(name: "Hutzl") }

before do
another_tag
tag
end

it "loads alls tags but not the one editing" do
it "loads alls tags but not the one editing", :aggregate_failures do
get :edit, params: {id: tag.id}
expect(assigns(:tags)).to include(another_tag)
expect(assigns(:tags)).not_to include(tag)
end
end

describe "#update" do
let(:tag) { Gutentag::Tag.create(name: "Sputz") }
let(:tag) { Alchemy::Tag.create(name: "Sputz") }

it "changes tags name" do
put :update, params: {id: tag.id, tag: {name: "Foo"}}
Expand All @@ -55,11 +62,11 @@ module Admin
end

context "with merg_to param given" do
let(:another_tag) { Gutentag::Tag.create(name: "Hutzl") }
let(:another_tag) { Alchemy::Tag.create(name: "Hutzl") }

it "replaces tag with other tag" do
expect(Alchemy::Tag).to receive(:replace)
expect_any_instance_of(Gutentag::Tag).to receive(:destroy)
expect_any_instance_of(Alchemy::Tag).to receive(:destroy)
put :update, params: {id: tag.id, tag: {merge_to: another_tag.id}}
expect(response).to redirect_to(admin_tags_path)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/app/models/series.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Series < ActiveRecord::Base
extend Alchemy::SearchableResource
end