Skip to content
New issue

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

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

Already on GitHub? # to your account

Enum should allow the conventionally case-sensitive operators #434

Merged
merged 2 commits into from
Mar 18, 2024
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
2 changes: 1 addition & 1 deletion lib/graphiti/adapters/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def self.default_operators
:not_match
],
uuid: [:eq, :not_eq],
enum: [:eq, :not_eq],
enum: [:eq, :not_eq, :eql, :not_eql],
integer_id: numerical_operators,
integer: numerical_operators,
big_decimal: numerical_operators,
Expand Down
2 changes: 2 additions & 0 deletions lib/graphiti/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def filter_eq(scope, attribute, value)
alias_method :filter_boolean_eq, :filter_eq
alias_method :filter_uuid_eq, :filter_eq
alias_method :filter_enum_eq, :filter_eq
alias_method :filter_enum_eql, :filter_eq

def filter_not_eq(scope, attribute, value)
scope.where.not(attribute => value)
Expand All @@ -37,6 +38,7 @@ def filter_not_eq(scope, attribute, value)
alias_method :filter_boolean_not_eq, :filter_not_eq
alias_method :filter_uuid_not_eq, :filter_not_eq
alias_method :filter_enum_not_eq, :filter_not_eq
alias_method :filter_enum_not_eql, :filter_not_eq

def filter_string_eq(scope, attribute, value, is_not: false)
column = column_for(scope, attribute)
Expand Down
27 changes: 27 additions & 0 deletions spec/filtering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,33 @@ def self.name
end
end

context "when filtering on an string_enum field" do
before do
resource.config[:filters] = {}
resource.filter :first_name, :string_enum, single: true, allow: ["William", "Harold"] do
eq do |scope, value|
scope[:conditions][:first_name] = value
scope
end
end
end

it "accepts values in the allowlist with eq operator" do
params[:filter] = {first_name: {eq: "William"}}
expect(records.map(&:id)).to eq([employee3.id])
end

it "accepts values in the allowlist with eql operator" do
params[:filter] = {first_name: {eql: "Harold"}}
expect(records.map(&:id)).to eq([employee4.id])
end

it "accepts values in the allowlist with not_eql operator" do
params[:filter] = {first_name: {not_eql: "Harold"}}
expect(records.map(&:id)).to eq([employee1.id, employee2.id, employee3.id])
end
end

context "when only allowing single values" do
before do
resource.filter :first_name, :string, single: true do
Expand Down
13 changes: 13 additions & 0 deletions spec/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def apply_filtering(records, params)
end
if value.is_a?(Array)
value.include?(db_value)
elsif value.is_a?(Hash) && value[:not]
db_value != value[:not]
else
db_value == value
end
Expand Down Expand Up @@ -305,6 +307,13 @@ def filter(scope, name, value)
scope[:conditions][name] = value
scope
end

def filter_not_eq(scope, name, value)
scope[:conditions] ||= {}
scope[:conditions][name] = {not: value}
scope
end

alias_method :filter_integer_eq, :filter
alias_method :filter_string_eq, :filter
alias_method :filter_big_decimal_eq, :filter
Expand All @@ -314,6 +323,10 @@ def filter(scope, name, value)
alias_method :filter_boolean_eq, :filter
alias_method :filter_hash_eq, :filter
alias_method :filter_array_eq, :filter
alias_method :filter_enum_eq, :filter
alias_method :filter_enum_not_eq, :filter_not_eq
alias_method :filter_enum_eql, :filter
alias_method :filter_enum_not_eql, :filter_not_eq

# No need for actual logic to fire
def count(scope, attr)
Expand Down
Loading