Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 6865af9744614586da7ebbb880034750611cce4a 1 parent 28b200d
Simone Carletti authored
14 CHANGELOG.rdoc
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.
61 README.rdoc
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,21 +104,19 @@ 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
@@ -129,7 +124,6 @@ The MultiConditions object accepts any type of conditions supported by ActiveRec
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,7 +142,8 @@ 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">
@@ -157,13 +151,16 @@ but you *MUST* initialize a MultiConditions instance from a Model.
# 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
21 TODO.rdoc
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
83 lib/multi_conditions.rb
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
30 test/unit/multi_conditions_test.rb → test/multi_conditions_test.rb
View
@@ -13,7 +13,7 @@
#++
-require File.dirname(__FILE__) + '/../helper'
+require 'test_helper'
class MultiConditionsTest < Test::Unit::TestCase
@@ -26,27 +26,23 @@ def setup
# =========================================================================
def test_initialize
- assert_nothing_raised() do
- assert_kind_of(Task::MultiConditions, ActiveRecord::Base::MultiConditions.new)
- end
+ assert_nothing_raised { Task.multiconditions }
end
def test_initialize_with_condition
- assert_nothing_raised() do
- Task::MultiConditions.new(:foo => 1)
- Task::MultiConditions.new('foo = 1')
- Task::MultiConditions.new(['foo = ?', 1])
+ assert_nothing_raised do
+ Task.multiconditions(:foo => 1)
+ Task.multiconditions('foo = 1')
+ Task.multiconditions(['foo = ?', 1])
end
end
def test_initialize_with_block
- assert_nothing_raised() do
- obj = Task::MultiConditions.new do |c|
+ assert_nothing_raised do
+ Task.multiconditions do |c|
c.append_condition(:foo => 1)
assert_equal(1, c.conditions.length)
end
- # ensure initialize always returns self
- assert_kind_of(ActiveRecord::Base::MultiConditions, obj)
end
end
@@ -64,14 +60,14 @@ def test_initialize_with_block
def test_single_condition_string
[{:foo => 'bar'}, "foo = 'bar'", ["foo = ?", 'bar'], ["foo = :foo", {:foo => 'bar'}]].each do |condition|
- mc = Task::MultiConditions.new(condition)
+ mc = Task.multiconditions(condition)
assert_match(/"?foo"? = 'bar'$/, mc.to_conditions)
end
end
def test_single_condition_fixnum
[{:foo => 1}, "foo = 1", ["foo = ?", 1], ["foo = :foo", {:foo => 1}]].each do |condition|
- mc = Task::MultiConditions.new(condition)
+ mc = Task.multiconditions(condition)
assert_match(/"?foo"? = 1$/, mc.to_conditions)
end
end
@@ -80,7 +76,7 @@ def test_multiple_conditions
first = [{:foo => 'bar'}, "foo = 'bar'", ["foo = ?", 'bar'], ["foo = :foo", {:foo => 'bar'}]]
second = [{:baz => 3}, "baz = 3", ["baz = ?", 3], ["baz = :baz", {:baz => 3}]]
first.size.times do |index|
- mc = Task::MultiConditions.new
+ mc = Task.multiconditions
mc.append_condition(first[index])
mc.append_condition(second[index])
assert_match(/"?foo"? = 'bar'/, mc.to_conditions)
@@ -92,7 +88,7 @@ def test_mixed_conditions
first = [{:foo => 'bar'}, "foo = 'bar'", ["foo = ?", 'bar'], ["foo = :foo", {:foo => 'bar'}]]
second = ["baz = 3", ["baz = ?", 3], ["baz = :baz", {:baz => 3}], {:baz => 3}]
first.size.times do |index|
- mc = Task::MultiConditions.new
+ mc = Task.multiconditions
mc.append_condition(first[index])
mc.append_condition(second[index])
assert_match(/"?foo"? = 'bar'/, mc.to_conditions)
@@ -104,7 +100,7 @@ def test_mixed_conditions
protected
def instance(condition = nil)
- Task::MultiConditions.new(condition)
+ Task.multiconditions(condition)
end
def table_name
4 test/test_all.rb
View
@@ -13,4 +13,6 @@
#++
-Dir.glob(File.dirname(__FILE__) + '/unit/**/*_test.rb').sort.each { |unit| require unit }
+require 'test_helper'
+
+Dir.glob(File.dirname(__FILE__) + '/**/*_test.rb').sort.each { |unit| require unit }
0  test/helper_database.rb → test/test_database_helper.rb
View
File renamed without changes
4 test/helper.rb → test/test_helper.rb
View
@@ -13,10 +13,8 @@
#++
-# prepend lib folder
$:.unshift(File.dirname(__FILE__) + '/../lib')
-# load active_record
require 'rubygems'
begin
gem 'activerecord', '>= 2.0'
@@ -28,4 +26,4 @@
require 'test/unit'
require 'multi_conditions'
-require File.dirname(__FILE__) + '/helper_database'
+require File.dirname(__FILE__) + '/test_database_helper'
Please sign in to comment.
Something went wrong with that request. Please try again.