Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
.gitignore | Sun Feb 08 06:30:36 -0800 2009 | |
| |
CHANGELOG.rdoc | Sun Feb 08 08:01:37 -0800 2009 | |
| |
LICENSE.rdoc | Sun Feb 08 06:23:59 -0800 2009 | |
| |
README.rdoc | Sat Apr 18 05:03:25 -0700 2009 | |
| |
Rakefile | Sun Feb 08 08:01:37 -0800 2009 | |
| |
TODO.rdoc | Sun Feb 08 07:43:36 -0800 2009 | |
| |
lib/ | Sun Feb 08 08:01:37 -0800 2009 | |
| |
setup.rb | Wed May 21 14:52:43 -0700 2008 | |
| |
test/ | Sun Feb 08 08:01:37 -0800 2009 |
ActiveRecord::MultiConditions
MultiConditions is a simple ActiveRecord plugin for storing ActiveRecord find conditions and make complex queries painless.
*DISCONTINUED PROJECT*: as of April 2009, I decided to discontinue the development of this library to concentrate my efforts on other projects.
If you are looking for a good alternative, I encourage you to have a look at Ben Johnson’s SearchLogic library. SearchLogic provides all ActiveRecord::Multiconditions features along with many other cool stuff.
Overview
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.
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']) # 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
- Ruby 1.8.6
- ActiveRecord >= 2.0 (tested up to AR 2.3)
If you want to run the test suite:
- sqlite3-ruby
Download and Installation
Installing ActiveRecord MultiConditions as a GEM is probably the best and easiest way. You must have RubyGems installed for the following instruction to work:
$ sudo gem install activerecord-multiconditions
To install the library manually grab the source code from the website, navigate to the root library directory and enter:
$ sudo ruby setup.rb
If you need the latest development version you can download the source code from the GIT repositories listed above. Beware that the code might not as stable as the official release.
Usage
First, don’t forget to require the library.
require 'rubygems'
require 'activerecord-multiconditions'
Now MultiConditions object is automatically available as subclass of any ActiveRecord object.
class Task < ActiveRecord::Base
# your Task model
end
multiconditions = Task.multiconditions(:status => 'active')
# => new instance
Creating a new instance
If you use ActiveRecord from Rails, this is just a matter of creating a new Model.
# create the Task model
class Task < ActiveRecord::Base
end
Now MultiConditions is automatically available within your Task namespace. You can use it in whatever class method, for example:
class Task < ActiveRecord::Base
def complex_search()
c = multiconditions(:foo => 'bar')
Task.find(:all, c.to_conditions)
end
end
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(:foo => 1).to_conditions
end
end
end
Foo.my_cool_conditions
# => 'foo = 1'
Appending conditions
You can append new conditions with the following methods, passing the conditions you want to append or prepend as parameters.
- #append_condition
- #prepend_condition
See Condition Types section to lean more about supported objects.
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.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.
require 'multi_conditions'
For various reason, you cannot initialize a new ActiveRecord::Base::MultiConditions 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(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
- Simone Carletti <weppos@weppos.net>
Resources
FeedBack and Bug reports
Feel free to email Simone Carletti with any questions or feedback.
Please use the Ticket System to submit bug reports or feature request.
Changelog
See the CHANGELOG.rdoc file for details.
License
Copyright © 2008-2009 Simone Carletti, ActiveRecord::MultiConditions is released under the MIT license.








