Permalink
Browse files

Reorganized library structure. This package now declares a MultiCondi…

…tions module that acts as a mixin for the ActiveRecord::Base class. Having MultiCondition class declared within a MultiConditions module is not really a perfect solution but - for now - I don't want to break BC renaming the GEM or the internal class.
  • Loading branch information...
1 parent 456c6ab commit 8063cd316e6ac73d99d8b8206ca73ab28f715958 @weppos committed Feb 8, 2009
Showing with 87 additions and 77 deletions.
  1. +2 −0 CHANGELOG.rdoc
  2. +2 −2 Rakefile
  3. +83 −69 lib/multi_conditions.rb
  4. +0 −6 test/test_helper.rb
View
@@ -15,6 +15,8 @@
Task::MultiConditions.new(Task) # too verbose
Task.multiconditions # preferred way
+* CHANGED: Reorganized library structure. This package now declares a MultiConditions module that acts as a mixin for the ActiveRecord::Base class. Having MultiCondition class declared within a MultiConditions module is not really a perfect solution but - for now - I don't want to break BC renaming the GEM or the internal class.
+
== Release 0.1.0
View
@@ -7,8 +7,8 @@ require 'multi_conditions'
# Common package properties
-PKG_NAME = ENV['PKG_NAME'] || ActiveRecord::Base::MultiConditions::GEM
-PKG_VERSION = ENV['PKG_VERSION'] || ActiveRecord::Base::MultiConditions::VERSION
+PKG_NAME = ENV['PKG_NAME'] || MultiConditions::GEM
+PKG_VERSION = ENV['PKG_VERSION'] || MultiConditions::VERSION
PKG_SUMMARY = "An ActiveRecord plugin for dealing with complex search :conditions."
PKG_FILES = FileList.new("{lib,test}/**/*.rb") do |fl|
fl.include %w(README.rdoc CHANGELOG.rdoc LICENSE.rdoc)
View
@@ -13,80 +13,91 @@
#++
-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
+require 'rubygems'
+require 'active_record'
+
+
+#
+# = ActiveRecord::MultiConditions
+#
+# MultiConditions is a simple ActiveRecord plugin for storing ActiveRecord
+# query and make complex queries painless.
+#
+# 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(ModelClass)
+# # ... do some elaboration
+# conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo'])
+# # ... other elaboration
+# conditions.append_condition(['name = ?', 'aname'])
+#
+# conditions.to_conditions
+# # => "active = true AND query LIKE '%foo' AND name = 'aname'"
+#
+# == Example Usage
+#
+# 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)
+#
+#
+module MultiConditions
+
+ module Version #:nodoc:
+ MAJOR = 0
+ MINOR = 1
+ TINY = 0
+ STRING = [MAJOR, MINOR, TINY].join('.')
+ end
+
+ NAME = 'ActiveRecord::MultiConditions'
+ GEM = 'activerecord-multiconditions'
+ AUTHOR = 'Simone Carletti <weppos@weppos.net>'
+
+ VERSION = Version::STRING
+ STATUS = 'alpha'
+ BUILD = ''.match(/(\d+)/).to_a.first
+
+
+ def self.included(base)
+ base.extend ClassMethods
+ base.send :include, InstanceMethods
+ end
+
+ module ClassMethods
+
+ # Initialize a new +MultiConditions+ instance with +conditions+.
#
- # = ActiveRecord::MultiConditions
- #
- # MultiConditions is a simple ActiveRecord plugin for storing ActiveRecord
- # query and make complex queries painless.
- #
- # 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(ModelClass)
- # # ... do some elaboration
- # conditions.append_condition(['active = ? AND query LIKE ?', true, '%foo'])
- # # ... other elaboration
- # conditions.append_condition(['name = ?', 'aname'])
- #
- # conditions.to_conditions
- # # => "active = true AND query LIKE '%foo' AND name = 'aname'"
- #
- # == Example Usage
- #
- # class Task < ActiveRecord::base
- # # your model
- # end
+ # 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.
#
- # 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)
+ # Task.multiconditions(['active = ? AND query LIKE ?', true, '%foo'])
+ # # => Task::MultiConditions
#
+ # Task.multiconditions(:foo => 'bar')
+ # # => Task::MultiConditions
#
- class MultiConditions
-
- module Version #:nodoc:
- MAJOR = 0
- MINOR = 1
- TINY = 0
-
- STRING = [MAJOR, MINOR, TINY].join('.')
- end
-
- NAME = 'ActiveRecord::MultiConditions'
- GEM = 'activerecord-multiconditions'
- AUTHOR = 'Simone Carletti <weppos@weppos.net>'
- VERSION = defined?(Version) ? Version::STRING : nil
- STATUS = 'alpha'
- BUILD = ''.match(/(\d+)/).to_a.first
+ def multiconditions(condition = nil, &block)
+ InstanceMethods::MultiConditions.new(self, condition, &block)
+ end
+ alias :multicondition :multiconditions
+
+ end
+
+ module InstanceMethods
+ class MultiConditions
# Conditions collection
attr_reader :conditions
@@ -155,6 +166,9 @@ def prepare_condition(condition)
end
end
-
end
+end
+
+class ActiveRecord::Base
+ include MultiConditions
end
View
@@ -16,12 +16,6 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'rubygems'
-begin
- gem 'activerecord', '>= 2.0'
-rescue Gem::LoadError => e
- abort e.message
-end
-
require 'active_record'
require 'test/unit'
require 'multi_conditions'

0 comments on commit 8063cd3

Please sign in to comment.