Skip to content

feat(ransack_allow_*): ransackable_* class method definition helpers #1546

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions lib/ransack/adapters/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,51 @@ def self.extended(base)
class_attribute :_ransack_aliases
self._ransackers ||= {}
self._ransack_aliases ||= {}

# Allow attributes to be searched by ransack
#
# Defines the ransackable_attributes class method.
# @see Base.ransackable_attributes
# @param [Array<Symbol>] attributes the attributes to be allowed
# @example allow column_a and column_b to be searched
# class MyModel < ApplicationRecord
# ransack_allow_attributes :column_a, :column_b
# end
def self.ransack_allow_attributes(*attributes)
define_singleton_method(:ransackable_attributes) do |auth_object = nil|
attributes.map(&:to_s)
end
end

# Allow associations to be searched by ransack
#
# Defines the ransackable_associations class method.
# @see Base.ransackable_associations
# @param [Array<Symbol>] associations the associations to be allowed
# @example allow association_a and association_b to be searched
# class MyModel < ApplicationRecord
# ransack_allow_associations :association_a, :association_b
# end
def self.ransack_allow_associations(*associations)
define_singleton_method(:ransackable_associations) do |auth_object = nil|
associations.map(&:to_s)
end
end

# Allow scopes to be searched by ransack
#
# Defines the ransackable_scopes class method.
# @see Base.ransackable_scopes
# @param [Array<Symbol>] scopes the scopes to be allowed
# @example allow scope_a and scope_b to be used with ransack
# class MyModel < ApplicationRecord
# ransack_allow_scopes :scope_a, :scope_b
# end
def self.ransack_allow_scopes(*scopes)
define_singleton_method(:ransackable_scopes) do |auth_object = nil|
scopes.map(&:to_s)
end
end
end
end

Expand Down
27 changes: 27 additions & 0 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,15 @@ def self.simple_escaping?
end
end

describe '.ransack_allow_attributes' do
subject { Article }

it 'has a class method returning an array with title and subject_header elements' do
subject.ransack_allow_attributes :title, :subject_header
expect(subject.ransackable_attributes).to match_array(%w[title subject_header])
end
end

describe '#ransortable_attributes' do
context 'when auth_object is nil' do
subject { Person.ransortable_attributes }
Expand Down Expand Up @@ -773,12 +782,30 @@ def self.simple_escaping?
end
end

describe '.ransack_allow_associations' do
subject { Article }

it 'has a class method returning an array with string "person" element' do
subject.ransack_allow_associations :person
expect(subject.ransackable_associations).to match_array(%w[person])
end
end

describe '#ransackable_scopes' do
subject { Person.ransackable_scopes }

it { should eq [] }
end

describe '.ransack_allow_scopes' do
subject { Article }

it 'has a class method returning an array with string "latest_comment_cont" element' do
subject.ransack_allow_scopes :latest_comment_cont
expect(subject.ransackable_scopes).to match_array(%w[latest_comment_cont])
end
end

describe '#ransackable_scopes_skip_sanitize_args' do
subject { Person.ransackable_scopes_skip_sanitize_args }

Expand Down