Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: binarylogic/searchlogic
base: master
...
head fork: murbanski/searchlogic
compare: master
  • 3 commits
  • 3 files changed
  • 1 commit comment
  • 1 contributor
8 lib/searchlogic/named_scopes/association_conditions.rb
View
@@ -31,13 +31,13 @@ def association_condition_details(name, last_condition = nil)
poly_type = nil
condition = nil
- if name_with_condition.to_s =~ /^(#{non_poly_assocs.collect(&:name).join("|")})_(\w+)$/
- association_name = $1
- condition = $2
- elsif name_with_condition.to_s =~ /^(#{poly_assocs.collect(&:name).join("|")})_(\w+)_type_(\w+)$/
+ if name_with_condition.to_s =~ /^(#{poly_assocs.collect(&:name).join("|")})_(\w+?)_type_(\w+)$/
association_name = $1
poly_type = $2
condition = $3
+ elsif name_with_condition.to_s =~ /^(#{non_poly_assocs.collect(&:name).join("|")})_(\w+)$/
+ association_name = $1
+ condition = $2
end
if association_name && condition
14 spec/named_scopes/association_conditions_spec.rb
View
@@ -171,6 +171,17 @@
:joins => "INNER JOIN \"users\" ON \"users\".id = \"audits\".auditable_id AND \"audits\".auditable_type = 'User'"
}
end
+
+ it "should delegate to polymorphic relationships first and plain relationships next" do
+ MagazineDoc.magazine_item_book_type_title_like("Design Patterns").proxy_options.should == {
+ :conditions => ["books.title LIKE ?", "%Design Patterns%"],
+ :joins => "INNER JOIN \"books\" ON \"books\".id = \"magazine_docs\".magazine_item_id AND \"magazine_docs\".magazine_item_type = 'Book'"
+ }
+ MagazineDoc.magazine_name_like("Main Warehouse").proxy_options.should == {
+ :conditions=>["magazines.name LIKE ?", "%Main Warehouse%"],
+ :joins=> :magazine
+ }
+ end
it "should deep delegate to polymorphic relationships" do
Audit.auditable_user_type_company_name_like("company").proxy_options.should == {
@@ -178,4 +189,5 @@
:joins => ["INNER JOIN \"users\" ON \"users\".id = \"audits\".auditable_id AND \"audits\".auditable_type = 'User'", " INNER JOIN \"companies\" ON \"companies\".id = \"users\".company_id "]
}
end
-end
+
+end
46 spec/spec_helper.rb
View
@@ -72,6 +72,27 @@
t.integer :order_id
t.float :price
end
+
+ create_table :magazines do |t|
+ t.string :name
+ end
+
+ create_table :magazine_docs do |t|
+ t.integer :doc_type
+ t.integer :magazine_id
+ t.integer :magazine_item_id
+ t.string :magazine_item_type
+ end
+
+ create_table :books do |t|
+ t.string :author
+ t.string :title
+ end
+
+ create_table :computers do |t|
+ t.string :vendor
+ t.string :cpu
+ end
end
$LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -112,11 +133,32 @@ class Fee < ActiveRecord::Base
class LineItem < ActiveRecord::Base
belongs_to :order
end
+
+ class Magazine < ActiveRecord::Base
+ has_many :magazine_docs
+ end
+
+ class MagazineDoc < ActiveRecord::Base
+ belongs_to :magazine
+ belongs_to :magazine_item, :polymorphic => true
+ end
+
+ class Book < ActiveRecord::Base
+ has_many :magazine_docs, :as => :magazine_item
+ end
+
+ class Computer < ActiveRecord::Base
+ has_many :magazine_docs, :as => :magazine_item
+ end
Company.destroy_all
User.destroy_all
Order.destroy_all
LineItem.destroy_all
+ MagazineDoc.destroy_all
+ Magazine.destroy_all
+ Book.destroy_all
+ Computer.destroy_all
end
config.after(:each) do
@@ -124,5 +166,9 @@ class LineItem < ActiveRecord::Base
Object.send(:remove_const, :User)
Object.send(:remove_const, :Order)
Object.send(:remove_const, :LineItem)
+ Object.send(:remove_const, :MagazineDoc)
+ Object.send(:remove_const, :Magazine)
+ Object.send(:remove_const, :Book)
+ Object.send(:remove_const, :Computer)
end
end

Showing you all comments on commits in this comparison.

Michael Deering

Funny I ran into the exact same issue http://github.com/mdeering/searchlogic. Guess I should have looked at the other forks ;) Anyway I put in the pull request but needed the fix in gem form ASAP for a project. Hosted under lazy-searchlogic gem over on github if you are interested http://gemcutter.org/gems/lazy-searchlogic .

Cheers,
Mike D.

Something went wrong with that request. Please try again.