activerecord-multiconditions 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,31 @@
1
+ #
2
+ # = ActiveRecord::MultiConditions
3
+ #
4
+ # An ActiveRecord plugin for dealing with complex search :conditions.
5
+ #
6
+ #
7
+ # Category:: ActiveRecord
8
+ # Package:: ActiveRecord::MultiConditions
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ #
11
+ #--
12
+ # SVN: $Id$
13
+ #++
14
+
15
+
16
+ # prepend lib folder
17
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
18
+
19
+ # load active_record
20
+ require 'rubygems'
21
+ begin
22
+ gem 'activerecord', '>= 2.0'
23
+ rescue Gem::LoadError => e
24
+ abort e.message
25
+ end
26
+
27
+ require 'active_record'
28
+ require 'test/unit'
29
+ require 'multi_conditions'
30
+
31
+ require File.dirname(__FILE__) + '/helper_database'
@@ -0,0 +1,47 @@
1
+ #
2
+ # = ActiveRecord::MultiConditions
3
+ #
4
+ # An ActiveRecord plugin for dealing with complex search :conditions.
5
+ #
6
+ #
7
+ # Category:: ActiveRecord
8
+ # Package:: ActiveRecord::MultiConditions
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ #
11
+ #--
12
+ # SVN: $Id$
13
+ #++
14
+
15
+
16
+ # FIXME: move the following execution within the unit test setup method
17
+
18
+
19
+ # reset database and prepare for tests
20
+ database = File.dirname(__FILE__) + '/db/test.sqlite3'
21
+ File.delete(database) if File.file? database
22
+ FileUtils.mkpath(File.dirname(database)) unless File.directory? File.dirname(database)
23
+
24
+ ActiveRecord::Base.establish_connection(
25
+ :adapter => 'sqlite3',
26
+ :database => database
27
+ )
28
+
29
+ # define a migration
30
+ class CreateTasks < ActiveRecord::Migration
31
+ def self.up
32
+ create_table :tasks do |t|
33
+ t.string :title
34
+ end
35
+ end
36
+
37
+ def self.down
38
+ drop_table :title
39
+ end
40
+ end
41
+
42
+ # run the migration
43
+ CreateTasks.migrate(:up)
44
+
45
+ # define a simple model
46
+ class Task < ActiveRecord::Base
47
+ end
data/test/test_all.rb ADDED
@@ -0,0 +1,16 @@
1
+ #
2
+ # = ActiveRecord::MultiConditions
3
+ #
4
+ # An ActiveRecord plugin for dealing with complex search :conditions.
5
+ #
6
+ #
7
+ # Category:: ActiveRecord
8
+ # Package:: ActiveRecord::MultiConditions
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ #
11
+ #--
12
+ # SVN: $Id$
13
+ #++
14
+
15
+
16
+ Dir.glob(File.dirname(__FILE__) + '/unit/**/*_test.rb').sort.each { |unit| require unit }
@@ -0,0 +1,114 @@
1
+ #
2
+ # = ActiveRecord::MultiConditions
3
+ #
4
+ # An ActiveRecord plugin for dealing with complex search :conditions.
5
+ #
6
+ #
7
+ # Category:: ActiveRecord
8
+ # Package:: ActiveRecord::MultiConditions
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ #
11
+ #--
12
+ # SVN: $Id$
13
+ #++
14
+
15
+
16
+ require File.dirname(__FILE__) + '/../helper'
17
+
18
+
19
+ class MultiConditionsTest < Test::Unit::TestCase
20
+
21
+ def setup
22
+ end
23
+
24
+ # =========================================================================
25
+ # Constructor behavior
26
+ # =========================================================================
27
+
28
+ def test_initialize
29
+ assert_nothing_raised() do
30
+ assert_kind_of(Task::MultiConditions, ActiveRecord::Base::MultiConditions.new)
31
+ end
32
+ end
33
+
34
+ def test_initialize_with_condition
35
+ assert_nothing_raised() do
36
+ Task::MultiConditions.new(:foo => 1)
37
+ Task::MultiConditions.new('foo = 1')
38
+ Task::MultiConditions.new(['foo = ?', 1])
39
+ end
40
+ end
41
+
42
+ def test_initialize_with_block
43
+ assert_nothing_raised() do
44
+ obj = Task::MultiConditions.new do |c|
45
+ c.append_condition(:foo => 1)
46
+ assert_equal(1, c.conditions.length)
47
+ end
48
+ # ensure initialize always returns self
49
+ assert_kind_of(ActiveRecord::Base::MultiConditions, obj)
50
+ end
51
+ end
52
+
53
+
54
+ # =========================================================================
55
+ # Condition string
56
+ # =========================================================================
57
+ #
58
+ # TODO: Consider to move check from a simple string to a real SQL query.
59
+ # Using a real query statement is more efficient and ensures
60
+ # finder methods works as expected.
61
+ #
62
+ # TODO: add test for with_scope and named_scope(s).
63
+ #
64
+
65
+ def test_single_condition_string
66
+ [{:foo => 'bar'}, "foo = 'bar'", ["foo = ?", 'bar'], ["foo = :foo", {:foo => 'bar'}]].each do |condition|
67
+ mc = Task::MultiConditions.new(condition)
68
+ assert_match(/"?foo"? = 'bar'$/, mc.to_conditions)
69
+ end
70
+ end
71
+
72
+ def test_single_condition_fixnum
73
+ [{:foo => 1}, "foo = 1", ["foo = ?", 1], ["foo = :foo", {:foo => 1}]].each do |condition|
74
+ mc = Task::MultiConditions.new(condition)
75
+ assert_match(/"?foo"? = 1$/, mc.to_conditions)
76
+ end
77
+ end
78
+
79
+ def test_multiple_conditions
80
+ first = [{:foo => 'bar'}, "foo = 'bar'", ["foo = ?", 'bar'], ["foo = :foo", {:foo => 'bar'}]]
81
+ second = [{:baz => 3}, "baz = 3", ["baz = ?", 3], ["baz = :baz", {:baz => 3}]]
82
+ first.size.times do |index|
83
+ mc = Task::MultiConditions.new
84
+ mc.append_condition(first[index])
85
+ mc.append_condition(second[index])
86
+ assert_match(/"?foo"? = 'bar'/, mc.to_conditions)
87
+ assert_match(/"?baz"? = 3/, mc.to_conditions)
88
+ end
89
+ end
90
+
91
+ def test_mixed_conditions
92
+ first = [{:foo => 'bar'}, "foo = 'bar'", ["foo = ?", 'bar'], ["foo = :foo", {:foo => 'bar'}]]
93
+ second = ["baz = 3", ["baz = ?", 3], ["baz = :baz", {:baz => 3}], {:baz => 3}]
94
+ first.size.times do |index|
95
+ mc = Task::MultiConditions.new
96
+ mc.append_condition(first[index])
97
+ mc.append_condition(second[index])
98
+ assert_match(/"?foo"? = 'bar'/, mc.to_conditions)
99
+ assert_match(/"?baz"? = 3/, mc.to_conditions)
100
+ end
101
+ end
102
+
103
+
104
+ protected
105
+
106
+ def instance(condition = nil)
107
+ Task::MultiConditions.new(condition)
108
+ end
109
+
110
+ def table_name
111
+ Task.table_name
112
+ end
113
+
114
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-multiconditions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Simone Carletti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-10 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ version:
24
+ description: MultiConditions is a simple ActiveRecord plugin for storing ActiveRecord query conditions and make complex queries painless.
25
+ email: weppos@weppos.net
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - CHANGELOG
32
+ - lib/multi_conditions.rb
33
+ - MIT-LICENSE
34
+ - README
35
+ files:
36
+ - CHANGELOG
37
+ - lib/multi_conditions.rb
38
+ - MIT-LICENSE
39
+ - Rakefile
40
+ - README
41
+ - setup.rb
42
+ - test/db/test.sqlite3
43
+ - test/helper.rb
44
+ - test/helper_database.rb
45
+ - test/test_all.rb
46
+ - test/unit/multi_conditions_test.rb
47
+ - TODO
48
+ - Manifest
49
+ - activerecord-multiconditions.gemspec
50
+ has_rdoc: true
51
+ homepage: http://code.simonecarletti.com/activerecord-multiconditions
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --line-numbers
55
+ - --inline-source
56
+ - --title
57
+ - Activerecord-multiconditions
58
+ - --main
59
+ - README
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project: activerecord-multiconditions
77
+ rubygems_version: 1.1.1
78
+ signing_key:
79
+ specification_version: 2
80
+ summary: An ActiveRecord plugin for dealing with complex search :conditions.
81
+ test_files:
82
+ - test/test_all.rb