Permalink
Browse files

* Add alias_scope feature, lets your create "named scopes" that repre…

…sent a procedure of named scopes, while at the same time telling Searchlogic it is safe to use in the Search class.
  • Loading branch information...
1 parent 3e72244 commit 8906208490deea506ea8b74eca87ee04bb455b34 @binarylogic committed Jun 21, 2009
View
@@ -1,6 +1,7 @@
== 2.0.2
* Added a delete method to the Search class to allow the deleting of conditions off of the object.
+* Add alias_scope feature, lets your create "named scopes" that represent a procedure of named scopes, while at the same time telling Searchlogic it is safe to use in the Search class.
== 2.0.1 released 2009-06-20
View
@@ -3,13 +3,15 @@
require "searchlogic/named_scopes/conditions"
require "searchlogic/named_scopes/ordering"
require "searchlogic/named_scopes/associations"
+require "searchlogic/named_scopes/alias_scope"
require "searchlogic/search"
Proc.send(:include, Searchlogic::CoreExt::Proc)
Object.send(:include, Searchlogic::CoreExt::Object)
ActiveRecord::Base.extend(Searchlogic::NamedScopes::Conditions)
ActiveRecord::Base.extend(Searchlogic::NamedScopes::Ordering)
ActiveRecord::Base.extend(Searchlogic::NamedScopes::Associations)
+ActiveRecord::Base.extend(Searchlogic::NamedScopes::AliasScope)
ActiveRecord::Base.extend(Searchlogic::Search::Implementation)
if defined?(ActionController)
@@ -0,0 +1,63 @@
+module Searchlogic
+ module NamedScopes
+ # Adds the ability to create alias scopes that allow you to alias a named
+ # scope or create a named scope procedure, while at the same time letting
+ # Searchlogic know that this is a safe method.
+ module AliasScope
+ # The searchlogic Search class takes a hash and chains the values together as named scopes.
+ # For security reasons the only hash keys that are allowed must be mapped to named scopes.
+ # You can not pass the name of a class method and expect that to be called. In some instances
+ # you might create a class method that essentially aliases a named scope or represents a
+ # named scope procedure. Ex:
+ #
+ # User.named_scope :teenager, :conditions => ["age >= ? AND age <= ?", 13, 19]
+ #
+ # This is obviously a very basic example, but there is logic that is duplicated here. For
+ # more complicated named scopes this might make more sense, but to make my point you could
+ # do something like this instead
+ #
+ # class User
+ # def teenager
+ # age_gte(13).age_lte(19)
+ # end
+ # end
+ #
+ # As I stated above, you could not use this method with the Searchlogic::Search class because
+ # there is no way to tell that this is actually a named scope. Instead, Searchlogic lets you
+ # do something like this:
+ #
+ # User.alias_scope :teenager, lambda { age_gte(13).age_lte(19) }
+ #
+ # It fits in better, at the same time Searchlogic will know this is an acceptable named scope.
+ def alias_scope(name, options = nil)
+ alias_scopes[name.to_sym] = options
+ (class << self; self end).instance_eval do
+ define_method name do |*args|
+ case options
+ when Symbol
+ send(options)
+ else
+ options.call(*args)
+ end
+ end
+ end
+ end
+
+ def alias_scopes # :nodoc:
+ @alias_scopes ||= {}
+ end
+
+ def alias_scope?(name) # :nodoc:
+ alias_scopes.key?(name.to_sym)
+ end
+
+ def condition?(name) # :nodoc:
+ super || alias_scope?(name)
+ end
+
+ def named_scope_options(name) # :nodoc:
+ super || alias_scopes[name.to_sym]
+ end
+ end
+ end
+end
@@ -0,0 +1,15 @@
+require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
+
+describe "AliasScope" do
+ it "should allow alias scopes" do
+ User.create(:username => "bjohnson")
+ User.create(:username => "thunt")
+ User.username_has("bjohnson").all.should == User.find_all_by_username("bjohnson")
+ end
+
+ it "should allow alias scopes from the search object" do
+ search = User.search
+ search.username_has = "bjohnson"
+ search.username_has.should == "bjohnson"
+ end
+end
View
@@ -51,6 +51,7 @@ class Company < ActiveRecord::Base
class User < ActiveRecord::Base
belongs_to :company
has_many :orders, :dependent => :destroy
+ alias_scope :username_has, lambda { |value| username_like(value) }
end
class Order < ActiveRecord::Base

1 comment on commit 8906208

@laserlemon
Contributor

This is a great feature. Let's get it out to the gem!

Please sign in to comment.