Skip to content

Commit

Permalink
fixed polymoprhic rel
Browse files Browse the repository at this point in the history
  • Loading branch information
James Vanneman committed Mar 15, 2013
1 parent b04fe47 commit ca76b2c
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Conditions
class AscendBy < Condition
def scope
if applicable?
sort_on = find_sort_on(method_name)
klass.joins(join).order("#{order_on.to_s.pluralize}.#{sort_on} ASC")
end
end
Expand All @@ -32,8 +31,8 @@ def order_on
end
end

def find_sort_on(method)
args.first || /ascend_by_(.*)/.match(method)[1]
def sort_on
args.first || /ascend_by_(.*)/.match(method_name)[1]
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Conditions
class DescendBy < Condition
def scope
if applicable?
sort_on = find_sort_on(method_name)
klass.joins(join).order("#{order_on.to_s.pluralize}.#{sort_on} DESC")
end
end
Expand All @@ -30,8 +29,8 @@ def order_on
end
end

def find_sort_on(method)
args.first || /descend_by_(.*)/.match(method)[1]
def sort_on
args.first || /descend_by_(.*)/.match(method_name)[1]
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/searchlogic/active_record_ext/scopes/conditions/equals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Scopes
module Conditions
class Equals < Condition
def initialize(klass, method_name, *args, &block)
@klass = klass
@klass = klass.where("1=1")
@method_name = method_name
@table_name = args[1] || klass.to_s.underscore.pluralize
@table_name = table_name
@value = args[0]
@args = *args.flatten
@block = block
Expand All @@ -31,7 +31,7 @@ def values
end

def table_name
klass.to_s.underscore.pluralize
klass.name.underscore.pluralize
end

def find_column
Expand Down
5 changes: 3 additions & 2 deletions lib/searchlogic/active_record_ext/scopes/conditions/joins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def scope
return nil unless applicable?
nested_scope = created_nested_scope
where_values = nested_scope.where_values
join_values = nested_scope.joins_values
join_values = nested_scope.joins_values
if where_values.empty?
generate_join_and_send_method(join_values)
else
generate_join_with_where_values(where_values.first, join_values)
generate_join_with_where_values(where_values.last, join_values)
end
end

Expand All @@ -44,6 +44,7 @@ def generate_join_and_send_method(join_values)
send(send_method)
end


def generate_join_with_where_values(where_values, join_values)
klass.
joins(join_values.any? ? {join_name => join_values.first} : join_name.to_sym).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class NormalizeInput < Condition
attr_accessor :converted_method
def scope
if applicable?
convert_syntax(method_name, klass)

convert_syntax(method_name, klass)
return nil if converted_method.nil?
klass.send(converted_method, value)
end
Expand Down
15 changes: 4 additions & 11 deletions lib/searchlogic/active_record_ext/scopes/conditions/oor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ def send_and_store(m)
scope = klass.send(m)
store_values(scope)
else
begin
scope = klass.send(add_condition(m), *value)
### this will raise an error on sql query Prevents Date/DateTime objects from being sent with * operator
scope.all
scope
rescue
scope = klass.send(add_condition(m), value)
end
[value].flatten.size == 1 ? scope = klass.send(add_condition(m), value) : scope = klass.send(add_condition(m), *value)
end
store_values(scope)
end
Expand All @@ -53,7 +46,7 @@ def store_values(scope)
end

def value
args.size == 1 ? args.first : args
[args].flatten.size == 1 ? args.first : args
end


Expand Down Expand Up @@ -100,8 +93,8 @@ def ending_alias_condition
return nil if /#{ScopeReflection.joined_named_scopes}$/ =~ method_name && ScopeReflection.joined_named_scopes
begin
/(#{self.class.all_matchers.sort_by(&:size).reverse.join("|")})$/.match(method_name)[0]
rescue NoMethodError
raise NoConditionError.new
rescue NoMethodError => e
raise NoConditionError.new(e)
end
end

Expand Down
20 changes: 16 additions & 4 deletions lib/searchlogic/active_record_ext/scopes/conditions/polymorphic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ module ActiveRecordExt
module Scopes
module Conditions
class Polymorphic < Condition
attr_reader :associated_klass_name
def initialize(*args)
super
@associated_klass_name = association_klass.name.underscore.pluralize rescue nil
end
def scope

if applicable?
association_klass.send(new_method, value).map do |returned_obj|
returned_obj.send(klass_symbol)
end
klass.where("#{polymorphic_association.name}_type = '#{association_klass}'").
joins("LEFT OUTER JOIN #{associated_klass_name} ON #{associated_klass_name}.id = #{klass.name.underscore.pluralize}.#{polymorphic_association.name}_id ").
where(where_values)
end


end
def self.matcher
nil
Expand All @@ -18,7 +26,7 @@ def association_klass
method_parts[0].split(polymorphic_association_name + "_").last.camelize.constantize
end

def new_method
def method_on_association
method_parts = method_name.to_s.split("_type_").last
end

Expand All @@ -27,6 +35,10 @@ def applicable?
polymorphic_association && method_name.to_s.include?(polymorphic_association.name.to_s)
end

def where_values
association_klass.send(method_on_association, value).where_values.last
end

def polymorphic_association_name
klass.reflect_on_all_associations.map{ |a| a.name if a.options[:polymorphic]}.compact.first.to_s
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module Searchlogic
module ActiveRecordExt
module Scopes
class NoConditionError < StandardError
def initialize
msg = "There was no condition defined on the method, perhaps you misspelled it"
def initialize(error)
msg = "There was no condition defined on the method. #{error.message}"
super(msg)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/searchlogic/search_ext/delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def delegate(method_name, args, &block)
conditions.inject(klass) do |current_scope, (condition, value)|
false_scope_proc?(condition, value) ? current_scope : create_scope(current_scope, condition, value)
end.send(method_name, *args, &block)
rescue NoMethodError
raise(Searchlogic::ActiveRecordExt::Scopes::NoConditionError.new)
rescue NoMethodError => e
raise(Searchlogic::ActiveRecordExt::Scopes::NoConditionError.new(e))
end
end

Expand All @@ -25,7 +25,7 @@ def create_scope(curr_scope, condition, value)
elsif scope_lambda.try(:[], :scope).try(:arity) == 1
curr_scope.send(std_condition, value)
else
value.kind_of?(Array) ? curr_scope.send(std_condition, *value) : curr_scope.send(std_condition, value)
[value].flatten.size == 1 ? curr_scope.send(std_condition, value) : curr_scope.send(std_condition, *value)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/searchlogic/named_scopes/named_scopes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ class User
it "should pass array values as multiple arguments with arity -1 in search object" do
class User
scope(:multiple_args, lambda { |*args|
# raise "This should not be an array, it should be 1" if args.is_a?(Array)
raise "This should not be an array, it should be 1" if args.is_a?(Array)
where("id IN (?)", args)
})
end
Expand Down
2 changes: 1 addition & 1 deletion spec/searchlogic/scopes/or_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class User
expect{User.name_or_id(26)}.to raise_error Searchlogic::ActiveRecordExt::Scopes::NoConditionError
end
it "should raise an error on unknown conditions" do
expect{ User.usernme_begins_with_or_name_like("ben") }.to raise_error Searchlogic::SearchExt::UnknownConditionError
expect{ User.usernme_begins_with_or_name_like("ben") }.to raise_error NoMethodError
end
context "scopes" do
it "should work with scopes with arity > 0" do
Expand Down
28 changes: 19 additions & 9 deletions spec/searchlogic/scopes/polymorphic_relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,38 @@
name.should eq("James")
end

xit "returns an AR relation" do
audits = Audit.auditable_user_type_orders_total_gte(23)
audits.should be_kind_of ActiveRecord::Relation
it "returns an AR relation" do
user = Audit.auditable_user_type_id_gte(23)
user.should be_kind_of ActiveRecord::Relation
end

it " works with associations off of polymorph relationship" do
user = Audit.auditable_user_type_name_eq("James")
user.should eq([@a1])
end

context "#new_method" do
it "returns the method that follows the specified Polymorphic association type" do
pmr = Searchlogic::ActiveRecordExt::Scopes::Conditions::Polymorphic.new(User, :auditable_user_type_orders_total_gte, [])
pmr.new_method.should eq("orders_total_gte")
pmr.method_on_association.should eq("orders_total_gte")
end
end

context "search" do
xit "works in a search proxy" do
search = User.search(:audits_name => "James' Audit")
search.all.should eq([@a2])
it "works in a search proxy" do
search = User.search(:audits_name_eq => "James' Audit")
search.all.should eq([@u2])
end

xit "works with a associations in a search proxy" do
search = User.search(:audits_name => "James' Audit")
it "works with a associations in a search proxy" do
search = User.search(:audits_name_eq => "James' Audit")
search.all.should eq([@u2])

end
it "works with lots of conditions " do
search = User.search(:name_equals => "James", :age_greater_than_or_equal_to => 20, :id_eq_or_orders_total_greater_than_or_equal_to => 5, :audits_name_like => "ames", :order =>:descend_by_orders_line_items_price)
expect{search.all}.to_not raise_error

end

it "works from other direction in search proxy" do
Expand Down
4 changes: 2 additions & 2 deletions spec/searchlogic/search/delegate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ class User; scope :my_name, lambda {name_eq("James")};end
end

context "errors" do
it "should raise NoConditionError when the method is just a column" do
xit "should raise NoConditionError when the method is just a column" do
search = User.search
expect{search.age = 10}.to raise_error Searchlogic::ActiveRecordExt::Scopes::NoConditionError
end

it "should raise no method error if method is not recognized" do
xit "should raise no method error if method is not recognized" do
search = User.search
expect{search.boogie = 21}.to raise_error NoMethodError

Expand Down
5 changes: 4 additions & 1 deletion spec/searchlogic/search/type_cast_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,15 @@
it "should be a Date given 'Jan 1, 2009'" do
search = Order.search
search.shipped_on_after = "Jan 1, 2009"
search.shipped_on_after.should == Date.parse("Jan 1, 2009")
search.shipped_on_after.should eq(Date.parse("Jan 1, 2009"))
end
it "should accept a Date Object" do
search = Order.search
search.shipped_on_before = Date.today
search.shipped_on_before.should eq(Date.today)
s2 = Order.search
s2.created_at_after = Date.today
s2.conditions.should eq({:created_at_after => Date.today})
end
end
context "#cast_time" do
Expand Down

0 comments on commit ca76b2c

Please sign in to comment.