Skip to content

Commit

Permalink
Add a field.virtual? method
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSD committed Sep 7, 2018
1 parent feb5ef6 commit da5ad2f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/scoped_search/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ def key_klass
end
end

# Returns true if this is a virtual field.
def virtual?
!ext_method.nil?
end

# Returns the ActiveRecord column definition that corresponds to this field.
def column
@column ||= begin
Expand Down
6 changes: 3 additions & 3 deletions lib/scoped_search/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def order_by(order, &block)
# By default, it will simply look up the correct SQL operator in the SQL_OPERATORS
# hash, but this can be overridden by a database adapter.
def sql_operator(operator, field)
raise ScopedSearch::QueryNotSupported, "the operator '#{operator}' is not supported for field type '#{field.type}'" if !field.ext_method and [:like, :unlike].include?(operator) and !field.textual?
raise ScopedSearch::QueryNotSupported, "the operator '#{operator}' is not supported for field type '#{field.type}'" if !field.virtual? and [:like, :unlike].include?(operator) and !field.textual?
SQL_OPERATORS[operator]
end

Expand Down Expand Up @@ -210,7 +210,7 @@ def set_test(field, operator,value, &block)
# <tt>operator</tt>:: The operator used for comparison.
# <tt>value</tt>:: The value to compare the field with.
def sql_test(field, operator, value, lhs, &block) # :yields: finder_option_type, value
return field.to_ext_method_sql(lhs, sql_operator(operator, field), value, &block) if field.ext_method
return field.to_ext_method_sql(lhs, sql_operator(operator, field), value, &block) if field.virtual?

yield(:keyparameter, lhs.sub(/^.*\./,'')) if field.key_field

Expand Down Expand Up @@ -554,7 +554,7 @@ def sql_test(field, operator, value, lhs, &block)
# Switches out the default LIKE operator in the default <tt>sql_operator</tt>
# method for ILIKE or @@ if full text searching is enabled.
def sql_operator(operator, field)
raise ScopedSearch::QueryNotSupported, "the operator '#{operator}' is not supported for field type '#{field.type}'" if !field.ext_method and [:like, :unlike].include?(operator) and !field.textual?
raise ScopedSearch::QueryNotSupported, "the operator '#{operator}' is not supported for field type '#{field.type}'" if !field.virtual? and [:like, :unlike].include?(operator) and !field.textual?
return '@@' if [:like, :unlike].include?(operator) && field.full_text_search
case operator
when :like then 'ILIKE'
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/query_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

it "should validate value if validator selected" do
field = double('field')
field.stub(:virtual?).and_return(false)
field.stub(:only_explicit).and_return(true)
field.stub(:field).and_return(:test_field)
field.stub(:validator).and_return(->(_value) { false })
Expand All @@ -49,6 +50,7 @@

it "should validate value if validator selected" do
field = double('field')
field.stub(:virtual?).and_return(false)
field.stub(:only_explicit).and_return(true)
field.stub(:field).and_return(:test_field)
field.stub(:ext_method).and_return(nil)
Expand All @@ -65,6 +67,7 @@

it "should display custom error from validator" do
field = double('field')
field.stub(:virtual?).and_return(false)
field.stub(:only_explicit).and_return(true)
field.stub(:field).and_return(:test_field)
field.stub(:validator).and_return(->(_value) { raise ScopedSearch::QueryNotSupported, 'my custom message' })
Expand Down

0 comments on commit da5ad2f

Please sign in to comment.