Permalink
Browse files

made applicable class level

  • Loading branch information...
1 parent 5c23e13 commit 3df1bf18eb9fdc02ee0ce43387505d5efc8beaed James Vanneman committed Feb 6, 2013
View
@@ -1,2 +1,2 @@
source :rubygems
-gemspec
+gemspec
View
@@ -2,8 +2,8 @@ PATH
remote: .
specs:
searchlogic (3.0.0)
- activerecord (~> 3.2.11)
- activesupport (~> 3.2.11)
+ activerecord (= 3.2.11)
+ activesupport (= 3.2.11)
GEM
remote: http://rubygems.org/
@@ -24,7 +24,15 @@ GEM
rake
arel (3.0.2)
builder (3.0.4)
+ columnize (0.3.6)
database_cleaner (0.9.1)
+ debugger (1.3.0)
+ columnize (>= 0.3.1)
+ debugger-linecache (~> 1.1.1)
+ debugger-ruby_core_source (~> 1.1.7)
+ debugger-linecache (1.1.2)
+ debugger-ruby_core_source (>= 1.1.1)
+ debugger-ruby_core_source (1.1.7)
diff-lcs (1.1.3)
i18n (0.6.1)
multi_json (1.5.0)
@@ -46,6 +54,7 @@ PLATFORMS
DEPENDENCIES
appraisal (= 0.5.1)
database_cleaner (= 0.9.1)
- rspec (~> 2.12.0)
+ debugger
+ rspec (= 2.12.0)
searchlogic!
sqlite3
View
@@ -9,4 +9,4 @@ def self.included(klass)
end
end
-ActiveRecord::Base.send(:include, Searchlogic)
+ActiveRecord::Base.send(:include, Searchlogic)
@@ -1,6 +1,10 @@
Dir[File.dirname(__FILE__) + '/conditions/*.rb'].each { |f| require(f) }
module Searchlogic
module Conditions
+ def respond_to?(*args)
+ name = args.first
+ applicable?(name) || super
+ end
private
def method_missing(method, *args, &block)
generate_scope(method, args, &block) || super
@@ -11,6 +15,10 @@ def generate_scope(method, args, &block)
klass.generate_scope(self, method, args, &block) if klass
end
+ def applicable?(method)
+ !(%r{^(#{column_names.join("|")})_(#{condition_klasses.map{ |k| k.to_s.split("::").last.downcase}.join("|")})} =~ method).nil? unless method.match(/connection/)
+ end
+
def condition_klasses
[
Equals,
@@ -1,19 +1,20 @@
module Searchlogic
module Conditions
class Equals < Condition
-
def scope
+ return nil if !applicable?
if values.kind_of?(Array)
- returned_objects = values.map {|value| klass.where("#{table_name}.#{column_name} = ?", value)}.flatten if applicable?
+ values.map {|value| klass.where("#{table_name}.#{column_name} = ?", value)}.flatten
else
- klass.where("#{table_name}.#{column_name} = ?", values) if applicable?
+ klass.where("#{table_name}.#{column_name} = ?", values)
end
end
private
def values
args.first
end
+
def applicable?
!(/^(#{klass.column_names.join("|")})_equals$/ =~ method_name).nil? if klass
end
View
@@ -10,9 +10,10 @@ Gem::Specification.new do |s|
s.summary = %q{Searchlogic makes using ActiveRecord named scopes easier and less repetitive.}
s.description = %q{Searchlogic makes using ActiveRecord named scopes easier and less repetitive.}
- s.add_dependency 'activerecord', '~> 3.2.11'
- s.add_dependency 'activesupport', '~> 3.2.11'
- s.add_development_dependency 'rspec', '~> 2.12.0'
+ s.add_dependency 'activerecord', '3.2.11'
+ s.add_dependency 'activesupport', '3.2.11'
+ s.add_development_dependency 'debugger'
+ s.add_development_dependency 'rspec', '2.12.0'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'appraisal', '0.5.1'
s.add_development_dependency "database_cleaner", "0.9.1"
@@ -24,11 +24,9 @@
it "can be chained with other scopes" do
james = User.create(:name=>"James", :age=>26)
- p User.age_equals("James").class
users = User.name_equals("James").age_equals(28)
users.count.should eq(1)
users.first.name.should eq("James")
users.first.age.should eq(28)
end
-
end
View
@@ -1,5 +1,6 @@
require(File.dirname(__FILE__) + '/../lib/searchlogic.rb')
require 'database_cleaner'
+require 'debugger'
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Base.configurations = true
ActiveRecord::Schema.verbose = false

0 comments on commit 3df1bf1

Please sign in to comment.