Permalink
Browse files

Changed 'Task' to ActiveRecord::Base in method (closes #141).

MultiConditions.new now requires the first parameter to be the ActiveRecord model class. The new method ActiveRecord::Base#multiconditions is now the preferred way to to initialize a new MultiConditions instance.

    class Task < ActiveRecord::Base; end;
    Task::MultiConditions.new(Task) # too verbose
    Task.multiconditions            # preferred way
  • Loading branch information...
1 parent 28b200d commit 6865af9744614586da7ebbb880034750611cce4a @weppos committed Feb 8, 2009
View
@@ -1,5 +1,17 @@
= Changelog
+
+== master
+
+* FIXED: Change 'Task' to ActiveRecord::Base in method (closes #141).
+
+* CHANGED: MultiConditions.new now requires the first parameter to be the ActiveRecord model class. The new method ActiveRecord::Base#multiconditions is now the preferred way to to initialize a new MultiConditions instance.
+
+ class Task < ActiveRecord::Base; end;
+ Task::MultiConditions.new(Task) # too verbose
+ Task.multiconditions # preferred way
+
+
== Release 0.1.0
- * Initial public release.
+* Initial public release.
View
@@ -9,14 +9,23 @@ This plugin doesn't replace ActiveRecord#with_scope method,
nor the basic :condition usage but extends it with the ability
of storing illimitate conditions in multiple step.
- conditions = MultiConditions.new
- # ... do some elaboration
+ class Task < ActiveRecord::Base; end
+
+ # create a new MultiConditions instance
+ conditions = Task.multicondition
+
+ # append a condition
conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo'])
- # ... other elaboration
- conditions.append_condition(['name = ?', 'aname'])
+ # conditional-append a condition
+ conditions.append_condition(['name = ?', 'aname']) if admin?
+
+ # get the final condition list ...
conditions.to_conditions
# => "active = true AND query LIKE '%foo' AND name = 'aname'"
+
+ # ... compatible with ActiveRecord finders
+ Task.find(:all, :conditions => conditions.to_conditions)
== Dependencies
@@ -29,11 +38,6 @@ If you want to run the test suite:
* sqlite3-ruby
-== Source
-
-MultiConditions source code is managed via GIT and hosted at GitHub: http://github.com/weppos/activerecord-multiconditions/.
-
-
== Download and Installation
Installing ActiveRecord MultiConditions as a GEM is probably the best and easiest way. You must have {RubyGems}[http://rubyforge.org/projects/rubygems/] installed for the following instruction to work:
@@ -51,27 +55,20 @@ If you need the latest development version you can download the source code from
First, don't forget to require the library.
- gem 'activerecord-multiconditions'
- require 'multi_conditions'
+ require 'rubygems'
+ require 'activerecord-multiconditions/multiconditions'
Now +MultiConditions+ object is automatically available as subclass of any ActiveRecord object.
class Task < ActiveRecord::Base
-
# your Task model
-
end
- multiconditions = Task::Multiconditions.new
+ multiconditions = Task.multiconditions(:status => 'active')
# => new instance
-=== Create a new instance
-As stated by the Important section above, you first need a valid ActiveRecord model
-to create a MultiConditions instance.
-Because MultiConditions helps you to deal with ActiveRecord queries,
-it's easy to understand why you *MUST* establish a valid database connection
-and have at least one table (mapped with a Model) to query.
+=== Creating a new instance
If you use ActiveRecord from Rails, this is just a matter of creating a new Model.
@@ -85,19 +82,19 @@ You can use it in whatever class method, for example:
class Task < ActiveRecord::Base
def complex_search()
- c = MultiConditions.new(:foo => 'bar')
+ c = multiconditions(:foo => 'bar')
Task.find(:all, c.to_conditions)
end
end
-But you can also create a new instance from an other library, class or model.
+But you can create a new instance from an other library, class or model as well.
Just remember to initialize MultiConditions from its own namespace.
class Foo
class << self
def my_cool_conditions
- Task::MultiConditions.new(:foo => 1).to_conditions
+ Task::multiconditions(:foo => 1).to_conditions
end
end
end
@@ -107,29 +104,26 @@ Just remember to initialize MultiConditions from its own namespace.
=== Appending conditions
-You can append new conditions calling
+You can append new conditions with the following methods,
+passing the conditions you want to append or prepend as parameters.
* #append_condition
* #prepend_condition
-and passing the conditions you want to append or prepend.
See Condition Types section to lean more about supported objects.
- conditions = MultiConditions.new
conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo']
conditions.prepend_condition(['name = ?', 'aname']
conditions.to_conditions
# => "name = 'aname' AND active = true AND query LIKE '%foo'"
-
=== Condition types
The MultiConditions object accepts any type of conditions supported by ActiveRecord,
including Strings, Arrays and Hashes, and merges them alltogether
just before sending the final :condition value to ActiveRecord search method.
- conditions = MultiConditions.new
conditions.append_conditions(:foo => 1, :bar => 2)
conditions.append_conditions('active = 1')
conditions.append_conditions(['name LIKE ?', '%foo'])
@@ -140,7 +134,6 @@ just before sending the final :condition value to ActiveRecord search method.
See ActiveRecord::Base#find documentation for more conditions examples.
-
== Important
Once loaded, this library become part of ActiveRecord package and
@@ -149,21 +142,25 @@ creates its own namespace at ActiveRecord::Base::MultiConditions.
require 'multi_conditions'
For various reason, you cannot initialize a new ActiveRecord::Base::MultiConditions
-but you *MUST* initialize a MultiConditions instance from a Model.
+but you *MUST* initialize a MultiConditions instance from a Model or
+using the ActiveRecord::Base#multiconditions method (preferred way).
# The wrong way
# raises Message: <"undefined method `abstract_class?' for Object:Class">
ActiveRecord::Base::MultiConditions.new
# The right way
class Model < ActiveRecord::Base
-
def a_method()
- c = MultiConditions.new
+ c = MultiConditions.new(Model, ['foo = ?', 'bar'])
find(:all, :conditions => c.to_conditions)
end
+ end
+ # The best way
+ class Model < ActiveRecord::Base
end
+ conditions = Model.multiconditions(['foo = ?', 'bar'])
== Author
View
@@ -0,0 +1,21 @@
+= TODO
+
+* improve tests in order to use real queries instead of simple statements
+
+* add tests for different ActiveRecord versions
+
+* ... more
+
+* configure a Redmine installation to store TODO list! :)
+
+== m02
+
+* store conditions as an array of condition
+
+* ability to identify a condition by :name ( append_condition(condition, :name), condition? :name, remove_condition :name ... )
+
+== m03
+
+* ability to tag conditions (condition group)
+
+* ability to remove, search, manage condition by tag
View
@@ -15,6 +15,27 @@
module ActiveRecord
class Base
+
+ class << self
+
+ # Initialize a new +MultiConditions+ instance with +conditions+.
+ #
+ # This is the preferred way to initialize a new +MultiConditions+ instance.
+ # If you want to initialize a MultiConditions youself you must be sure
+ # to pass current Active Record class as the first parameter.
+ #
+ # Task.multiconditions(['active = ? AND query LIKE ?', true, '%foo'])
+ # # => Task::MultiConditions
+ #
+ # Task.multiconditions(:foo => 'bar')
+ # # => Task::MultiConditions
+ #
+ def multiconditions(condition = nil, &block)
+ MultiConditions.new(self, condition, &block)
+ end
+ alias :multicondition :multiconditions
+
+ end
#
# = ActiveRecord::MultiConditions
@@ -26,52 +47,28 @@ class Base
# nor the basic :condition usage but extends it with the ability
# of storing illimitate conditions in multiple step.
#
- # conditions = MultiConditions.new
+ # conditions = MultiConditions.new(ModelClass)
# # ... do some elaboration
- # conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo']
+ # conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo'])
# # ... other elaboration
- # conditions.append_condition(['name = ?', 'aname']
+ # conditions.append_condition(['name = ?', 'aname'])
#
# conditions.to_conditions
# # => "active = true AND query LIKE '%foo' AND name = 'aname'"
#
- # The MultiConditions object accepts any type of conditions supported by ActiveRecord,
- # including Strings, Arrays and Hashes, and merges them alltogether
- # just before sending the final :condition value to ActiveRecord search method.
- #
- # conditions = MultiConditions.new
- # conditions.append_conditions(:foo => 1, :bar => 2)
- # conditions.append_conditions('active = 1')
- # conditions.append_conditions(['name LIKE ?', '%foo'])
- #
- # conditions.to_conditions
- # # => 'foo = 1 AND :bar = 2 AND active = 1 AND name LIKE '%foo'
- #
- # See ActiveRecord::Base#find documentation for more conditions examples.
- #
- #
- # == Important
- #
- # Once loaded, this library become part of ActiveRecord package and
- # creates its own namespace at ActiveRecord::Base::MultiConditions.
+ # == Example Usage
#
- # For various reason, you cannot initialize a new ActiveRecord::Base::MultiConditions
- # but you *MUST* initialize a MultiConditions instance from a Model.
- #
- # # The wrong way
- # # raises Message: <"undefined method `abstract_class?' for Object:Class">
- # ActiveRecord::Base::MultiConditions.new
- #
- # # The right way
- # class Model < ActiveRecord::Base
- #
- # def a_method()
- # c = MultiConditions.new
- # find(:all, :conditions => c.to_conditions)
- # end
- #
+ # class Task < ActiveRecord::base
+ # # your model
+ # end
+ #
+ # conditions = Task.multiconditions(['name = ?', 'aname']) do |m|
+ # m.append_condition(['active = ? AND query LIKE ?', true, '%foo'])
+ # m.append_condition(:field => false)
# end
#
+ # Task.find(:all, :conditions => conditions.to_conditions)
+ #
#
class MultiConditions
@@ -110,31 +107,27 @@ module Version #:nodoc:
# condition.append_condition(:baz => 3)
# end
#
- def initialize(condition = nil, &block) # :yields: self
+ def initialize(klass, condition = nil, &block) # :yields: self
@conditions = []
+ @klass = klass
append_condition(condition)
yield(self) if block_given?
self
end
- #
# Appends a new condition at the end of condition list.
- #
def append_condition(condition)
@conditions.push(prepare_condition(condition)) unless condition.nil?
end
- #
# Prepends a new condition at the beginning of condition list.
- #
def prepend_condition(condition)
@conditions.unshift(prepare_condition(condition)) unless condition.nil?
end
- #
# Returns a :conditions suitable representation of this object
#
- # c = MultiConditions.new do |condition|
+ # c = ModelClass.multiconditions do |condition|
# condition.append_condition('foo = 1')
# condition.append_condition('bar = 2')
# condition.append_condition(:baz => 3)
@@ -158,7 +151,7 @@ def to_conditions
# for now use active record :sanitize_sql_for_conditions
# instead of dealing with custom methods.
def prepare_condition(condition)
- Task.send(:sanitize_sql_for_conditions, condition)
+ @klass.send(:sanitize_sql_for_conditions, condition)
end
end
Oops, something went wrong.

0 comments on commit 6865af9

Please sign in to comment.