record_filter 0.9.13 → 0.9.14

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 9
4
- :patch: 13
4
+ :patch: 14
@@ -50,7 +50,7 @@ module RecordFilter
50
50
 
51
51
  def add_restriction(column_name, operator, value, options={})
52
52
  check_column_exists!(column_name)
53
- restriction = RestrictionFactory.build(operator, "#{@table_name}.#{column_name}", value, options)
53
+ restriction = RestrictionFactory.build(operator, "#{@table_name}.#{column_name}", value, @table, options)
54
54
  self << restriction
55
55
  end
56
56
 
@@ -50,6 +50,7 @@ module RecordFilter
50
50
  dsl_restriction.operator,
51
51
  "#{@right_table.table_alias}.#{dsl_restriction.column}",
52
52
  dsl_restriction.value,
53
+ @right_table,
53
54
  :negated => dsl_restriction.negated)
54
55
  @right_table.model_class.merge_conditions(restriction.to_conditions)
55
56
  end
@@ -13,8 +13,8 @@ module RecordFilter
13
13
  :like => Restrictions::Like
14
14
  }
15
15
 
16
- def self.build(operator, column_name, value, options)
17
- OPERATOR_HASH[operator].new(column_name, value, options)
16
+ def self.build(operator, column_name, value, table, options)
17
+ OPERATOR_HASH[operator].new(column_name, value, table, options)
18
18
  end
19
19
  end
20
20
  end
@@ -1,13 +1,19 @@
1
1
  module RecordFilter
2
2
  module Restrictions # :nodoc: all
3
3
  class Base
4
- def initialize(column_name, value, options={})
5
- @column_name, @value, @negated = column_name, value, !!options.delete(:negated)
4
+ include ColumnParser
5
+
6
+ def initialize(column_name, value, table, options={})
7
+ @column_name, @value, @table, @negated = column_name, value, table, !!options.delete(:negated)
6
8
  @value = @value.id if @value.kind_of?(ActiveRecord::Base)
7
9
  end
8
10
 
9
11
  def to_conditions
10
- @value.nil? ? [to_sql] : [to_sql, @value]
12
+ if @value.nil? || @value.is_a?(Hash)
13
+ [to_sql]
14
+ else
15
+ [to_sql, @value]
16
+ end
11
17
  end
12
18
 
13
19
  def to_sql
@@ -17,15 +23,26 @@ module RecordFilter
17
23
  def to_negative_sql
18
24
  "NOT (#{to_positive_sql})"
19
25
  end
26
+
27
+ def value_hash_as_column_or_question_mark
28
+ if @value.is_a?(Hash)
29
+ column, table = parse_column_in_table(@value, @table)
30
+ if (table.has_column(column))
31
+ "#{table.table_alias}.#{column}"
32
+ end
33
+ else
34
+ '?'
35
+ end
36
+ end
20
37
  end
21
38
 
22
39
  class EqualTo < Base
23
40
  def to_positive_sql
24
- "#{@column_name} = ?"
41
+ "#{@column_name} = #{value_hash_as_column_or_question_mark}"
25
42
  end
26
43
 
27
44
  def to_negative_sql
28
- "#{@column_name} <> ?"
45
+ "#{@column_name} <> #{value_hash_as_column_or_question_mark}"
29
46
  end
30
47
  end
31
48
 
@@ -41,35 +58,35 @@ module RecordFilter
41
58
 
42
59
  class LessThan < Base
43
60
  def to_positive_sql
44
- "#{@column_name} < #{@value.nil? ? 'NULL' : '?'}"
61
+ "#{@column_name} < #{@value.nil? ? 'NULL' : value_hash_as_column_or_question_mark}"
45
62
  end
46
63
  end
47
64
 
48
65
  class LessThanOrEqualTo < Base
49
66
  def to_positive_sql
50
- "#{@column_name} <= #{@value.nil? ? 'NULL' : '?'}"
67
+ "#{@column_name} <= #{@value.nil? ? 'NULL' : value_hash_as_column_or_question_mark}"
51
68
  end
52
69
  end
53
70
 
54
71
  class GreaterThan < Base
55
72
  def to_positive_sql
56
- "#{@column_name} > #{@value.nil? ? 'NULL' : '?'}"
73
+ "#{@column_name} > #{@value.nil? ? 'NULL' : value_hash_as_column_or_question_mark}"
57
74
  end
58
75
  end
59
76
 
60
77
  class GreaterThanOrEqualTo < Base
61
78
  def to_positive_sql
62
- "#{@column_name} >= #{@value.nil? ? 'NULL' : '?'}"
79
+ "#{@column_name} >= #{@value.nil? ? 'NULL' : value_hash_as_column_or_question_mark}"
63
80
  end
64
81
  end
65
82
 
66
83
  class In < Base
67
84
  def to_positive_sql
68
- "#{@column_name} IN (?)"
85
+ "#{@column_name} IN (#{value_hash_as_column_or_question_mark})"
69
86
  end
70
87
 
71
88
  def to_negative_sql
72
- "#{@column_name} NOT IN (?)"
89
+ "#{@column_name} NOT IN (#{value_hash_as_column_or_question_mark})"
73
90
  end
74
91
 
75
92
  def to_conditions
@@ -86,11 +103,11 @@ module RecordFilter
86
103
 
87
104
  class Like < Base
88
105
  def to_positive_sql
89
- "#{@column_name} LIKE ?"
106
+ "#{@column_name} LIKE #{value_hash_as_column_or_question_mark}"
90
107
  end
91
108
 
92
109
  def to_negative_sql
93
- "#{@column_name} NOT LIKE ?"
110
+ "#{@column_name} NOT LIKE #{value_hash_as_column_or_question_mark}"
94
111
  end
95
112
  end
96
113
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{record_filter}
8
- s.version = "0.9.13"
8
+ s.version = "0.9.14"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Aubrey Holland", "Mat Brown"]
12
- s.date = %q{2009-11-10}
12
+ s.date = %q{2009-12-21}
13
13
  s.description = %q{RecordFilter is a Pure-ruby criteria API for building complex queries in ActiveRecord. It supports queries that are built on the fly as well as named filters that can be added to objects and chained to create complex queries. It also gets rid of the nasty hard-coded SQL that shows up in most ActiveRecord code with a clean API that makes queries simple and intuitive to build.}
14
14
  s.email = %q{aubreyholland@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -248,4 +248,20 @@ describe 'RecordFilter restrictions' do
248
248
  end.inspect
249
249
  Comment.last_find.should == { :readonly => false, :conditions => "(\"comments\".offensive = 't') AND (\"comments\".post_id = #{Post.first.id})" }
250
250
  end
251
+
252
+ it 'should allow a hash as the value for the restriction and use that as the name of a table' do
253
+ Blog.filter do
254
+ having(:comments)
255
+ with(:created_at).gt(:comments => :created_at)
256
+ end.inspect
257
+ Blog.last_find[:conditions].should == [%q(("blogs".created_at > blogs__posts__comments.created_at))]
258
+ end
259
+
260
+ it 'should allow a hash as the value for the restriction and use that as the name of a table alias' do
261
+ Blog.filter do
262
+ having(:comments, :alias => 'cmts')
263
+ with(:created_at).gt('cmts' => :created_at)
264
+ end.inspect
265
+ Blog.last_find[:conditions].should == [%q(("blogs".created_at > cmts.created_at))]
266
+ end
251
267
  end
data/spec/test.db CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: record_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.13
4
+ version: 0.9.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aubrey Holland
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-11-10 00:00:00 -05:00
13
+ date: 2009-12-21 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency