Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 204 lines (123 sloc) 5.963 kB
1bb464c @weppos Update README.rdoc
authored
1 = Discontinued
1bcb17e @weppos * prepared library for GEM package
authored
2
1bb464c @weppos Update README.rdoc
authored
3 As of April 2009, I decided to discontinue the development of this library to concentrate my efforts on other projects.
1bcb17e @weppos * prepared library for GEM package
authored
4
1bb464c @weppos Update README.rdoc
authored
5 If you are looking for a good alternative, I encourage you to have a look at Ben Johnson's SearchLogic library.
6 SearchLogic provides all ActiveRecord::Multiconditions features along with many other cool stuff.
1bcb17e @weppos * prepared library for GEM package
authored
7
1bb464c @weppos Update README.rdoc
authored
8 Now back to your original README content...
603d59c @weppos *DISCONTINUED PROJECT*: as of April 2009, I decided to discontinue th…
authored
9
10
1bb464c @weppos Update README.rdoc
authored
11 = ActiveRecord::MultiConditions
12
13 MultiConditions is a simple ActiveRecord plugin for storing ActiveRecord find conditions and make complex queries painless.
603d59c @weppos *DISCONTINUED PROJECT*: as of April 2009, I decided to discontinue th…
authored
14
1bcb17e @weppos * prepared library for GEM package
authored
15 == Overview
16
17 This plugin doesn't replace ActiveRecord#with_scope method,
18 nor the basic :condition usage but extends it with the ability
19 of storing illimitate conditions in multiple step.
20
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
21 class Task < ActiveRecord::Base; end
22
23 # create a new MultiConditions instance
24 conditions = Task.multicondition
25
26 # append a condition
ca41d2b @weppos * fixed typo
authored
27 conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo'])
1bcb17e @weppos * prepared library for GEM package
authored
28
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
29 # conditional-append a condition
30 conditions.append_condition(['name = ?', 'aname']) if admin?
31
32 # get the final condition list ...
1bcb17e @weppos * prepared library for GEM package
authored
33 conditions.to_conditions
34 # => "active = true AND query LIKE '%foo' AND name = 'aname'"
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
35
36 # ... compatible with ActiveRecord finders
37 Task.find(:all, :conditions => conditions.to_conditions)
1bcb17e @weppos * prepared library for GEM package
authored
38
39
40 == Dependencies
41
42 * Ruby 1.8.6
603d59c @weppos *DISCONTINUED PROJECT*: as of April 2009, I decided to discontinue th…
authored
43 * ActiveRecord >= 2.0 (tested up to AR 2.3)
1bcb17e @weppos * prepared library for GEM package
authored
44
45 If you want to run the test suite:
46
47 * sqlite3-ruby
48
49
50 == Download and Installation
51
a99efb2 @weppos * added download instructions
authored
52 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:
1bcb17e @weppos * prepared library for GEM package
authored
53
a99efb2 @weppos * added download instructions
authored
54 $ sudo gem install activerecord-multiconditions
55
56 To install the library manually grab the source code from the website, navigate to the root library directory and enter:
57
58 $ sudo ruby setup.rb
59
60 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.
1bcb17e @weppos * prepared library for GEM package
authored
61
62
63 == Usage
64
80bf8b1 @weppos * added require details in README file
authored
65 First, don't forget to require the library.
66
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
67 require 'rubygems'
456c6ab @weppos Added activerecord-multiconditions.rb file to enable RubyGems single-…
authored
68 require 'activerecord-multiconditions'
80bf8b1 @weppos * added require details in README file
authored
69
a99efb2 @weppos * added download instructions
authored
70 Now +MultiConditions+ object is automatically available as subclass of any ActiveRecord object.
71
8c0995b @weppos Updated README and CHANGELOG files.
authored
72 class Task < ActiveRecord::Base
73 # your Task model
74 end
a99efb2 @weppos * added download instructions
authored
75
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
76 multiconditions = Task.multiconditions(:status => 'active')
a99efb2 @weppos * added download instructions
authored
77 # => new instance
80bf8b1 @weppos * added require details in README file
authored
78
1bcb17e @weppos * prepared library for GEM package
authored
79
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
80 === Creating a new instance
1bcb17e @weppos * prepared library for GEM package
authored
81
82 If you use ActiveRecord from Rails, this is just a matter of creating a new Model.
83
84 # create the Task model
85 class Task < ActiveRecord::Base
86 end
87
88 Now MultiConditions is automatically available within your Task namespace.
89 You can use it in whatever class method, for example:
90
91 class Task < ActiveRecord::Base
92
93 def complex_search()
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
94 c = multiconditions(:foo => 'bar')
1bcb17e @weppos * prepared library for GEM package
authored
95 Task.find(:all, c.to_conditions)
96 end
97
98 end
99
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
100 But you can create a new instance from an other library, class or model as well.
1bcb17e @weppos * prepared library for GEM package
authored
101 Just remember to initialize MultiConditions from its own namespace.
102
103 class Foo
104 class << self
105 def my_cool_conditions
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
106 Task::multiconditions(:foo => 1).to_conditions
1bcb17e @weppos * prepared library for GEM package
authored
107 end
108 end
109 end
110
111 Foo.my_cool_conditions
112 # => 'foo = 1'
113
114 === Appending conditions
115
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
116 You can append new conditions with the following methods,
117 passing the conditions you want to append or prepend as parameters.
1bcb17e @weppos * prepared library for GEM package
authored
118
119 * #append_condition
120 * #prepend_condition
121
122 See Condition Types section to lean more about supported objects.
123
124 conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo']
125 conditions.prepend_condition(['name = ?', 'aname']
126
127 conditions.to_conditions
128 # => "name = 'aname' AND active = true AND query LIKE '%foo'"
129
130 === Condition types
131
132 The MultiConditions object accepts any type of conditions supported by ActiveRecord,
133 including Strings, Arrays and Hashes, and merges them alltogether
134 just before sending the final :condition value to ActiveRecord search method.
135
136 conditions.append_conditions(:foo => 1, :bar => 2)
137 conditions.append_conditions('active = 1')
138 conditions.append_conditions(['name LIKE ?', '%foo'])
139
140 conditions.to_conditions
141 # => 'foo = 1 AND :bar = 2 AND active = 1 AND name LIKE '%foo'
142
143 See ActiveRecord::Base#find documentation for more conditions examples.
144
145
146 == Important
147
148 Once loaded, this library become part of ActiveRecord package and
149 creates its own namespace at ActiveRecord::Base::MultiConditions.
150
80bf8b1 @weppos * added require details in README file
authored
151 require 'multi_conditions'
152
1bcb17e @weppos * prepared library for GEM package
authored
153 For various reason, you cannot initialize a new ActiveRecord::Base::MultiConditions
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
154 but you *MUST* initialize a MultiConditions instance from a Model or
155 using the ActiveRecord::Base#multiconditions method (preferred way).
1bcb17e @weppos * prepared library for GEM package
authored
156
157 # The wrong way
158 # raises Message: <"undefined method `abstract_class?' for Object:Class">
159 ActiveRecord::Base::MultiConditions.new
160
161 # The right way
162 class Model < ActiveRecord::Base
163 def a_method()
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
164 c = MultiConditions.new(Model, ['foo = ?', 'bar'])
1bcb17e @weppos * prepared library for GEM package
authored
165 find(:all, :conditions => c.to_conditions)
166 end
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
167 end
1bcb17e @weppos * prepared library for GEM package
authored
168
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
169 # The best way
170 class Model < ActiveRecord::Base
1bcb17e @weppos * prepared library for GEM package
authored
171 end
6865af9 @weppos Changed 'Task' to ActiveRecord::Base in method (closes #141).
authored
172 conditions = Model.multiconditions(['foo = ?', 'bar'])
1bcb17e @weppos * prepared library for GEM package
authored
173
174
175 == Author
176
177 * {Simone Carletti}[http://www.simonecarletti.com/] <weppos@weppos.net>
178
179
8c0995b @weppos Updated README and CHANGELOG files.
authored
180 == Resources
1bcb17e @weppos * prepared library for GEM package
authored
181
8c0995b @weppos Updated README and CHANGELOG files.
authored
182 * {Homepage}[http://code.simonecarletti.com/multiconditions]
183 * {API}[http://multiconditions.rubyforge.org/]
184 * {GitHub}[http://github.com/weppos/activerecord-multiconditions/]
185 * {RubyForge}[http://rubyforge.org/projects/multiconditions/]
1bcb17e @weppos * prepared library for GEM package
authored
186
187
188 == FeedBack and Bug reports
189
8c0995b @weppos Updated README and CHANGELOG files.
authored
190 Feel free to email {Simone Carletti}[mailto:weppos@weppos.net] with any questions or feedback.
1bcb17e @weppos * prepared library for GEM package
authored
191
8c0995b @weppos Updated README and CHANGELOG files.
authored
192 Please use the {Ticket System}[http://code.simonecarletti.com/projects/show/multiconditions] to submit bug reports or feature request.
1bcb17e @weppos * prepared library for GEM package
authored
193
194
195 == Changelog
196
8c0995b @weppos Updated README and CHANGELOG files.
authored
197 See the CHANGELOG.rdoc file for details.
198
199
200 == License
201
202 Copyright (c) 2008-2009 Simone Carletti, ActiveRecord::MultiConditions is released under the MIT license.
203
Something went wrong with that request. Please try again.