squeel 1.0.8 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 1.0.9 (2012-08-06)
2
+
3
+ * Fix issue with duplication of order conditions in default_scope on AR 3.0.x
4
+
1
5
  ## 1.0.8 (2012-07-28)
2
6
 
3
7
  * Fix an issue with properly casting values to column type when used
@@ -16,6 +16,36 @@ module Arel
16
16
  ::Arel::Attribute.new self, name.to_sym
17
17
  end
18
18
  end
19
+
20
+ def hash
21
+ [name, engine].hash
22
+ end
23
+
24
+ def eql?(other)
25
+ self.class == other.class &&
26
+ self.name == other.name &&
27
+ self.engine == other.engine
28
+ end
29
+ alias :== :eql?
30
+ end
31
+
32
+ module Attributes
33
+
34
+ class Attribute < Attribute.superclass
35
+
36
+ def hash
37
+ [relation, name].hash
38
+ end
39
+
40
+ def eql?(other)
41
+ self.class == other.class &&
42
+ self.relation == other.relation &&
43
+ self.name == other.name
44
+ end
45
+ alias :== :eql?
46
+
47
+ end
48
+
19
49
  end
20
50
 
21
51
  module Nodes
@@ -1,3 +1,3 @@
1
1
  module Squeel
2
- VERSION = "1.0.8"
2
+ VERSION = "1.0.9"
3
3
  end
data/spec/console.rb CHANGED
@@ -3,9 +3,11 @@ require 'machinist/active_record'
3
3
  require 'sham'
4
4
  require 'faker'
5
5
 
6
- Dir[File.expand_path('../../spec/{helpers,support,blueprints}/*.rb', __FILE__)].each do |f|
6
+ Dir[File.expand_path('../helpers/*.rb', __FILE__)].each do |f|
7
7
  require f
8
8
  end
9
+ require File.expand_path('../support/schema.rb', __FILE__)
10
+ require File.expand_path('../support/models.rb', __FILE__)
9
11
 
10
12
  Sham.define do
11
13
  name { Faker::Name.name }
@@ -17,7 +19,7 @@ Sham.define do
17
19
  object_name { Faker::Lorem.words(1).first }
18
20
  end
19
21
 
20
- Schema.create
22
+ Models.make
21
23
 
22
24
  require 'squeel'
23
25
 
data/spec/spec_helper.rb CHANGED
@@ -26,9 +26,11 @@ module ActiveRecord
26
26
  ActiveSupport::Notifications.subscribe('sql.active_record', SQLCounter.new)
27
27
  end
28
28
 
29
- Dir[File.expand_path('../{helpers,support,blueprints}/*.rb', __FILE__)].each do |f|
29
+ Dir[File.expand_path('../helpers/*.rb', __FILE__)].each do |f|
30
30
  require f
31
31
  end
32
+ require File.expand_path('../support/schema.rb', __FILE__)
33
+ require File.expand_path('../support/models.rb', __FILE__)
32
34
 
33
35
  Sham.define do
34
36
  name { Faker::Name.name }
@@ -45,7 +47,7 @@ RSpec.configure do |config|
45
47
  puts '=' * 80
46
48
  puts "Running specs against ActiveRecord #{ActiveRecord::VERSION::STRING} and ARel #{Arel::VERSION}..."
47
49
  puts '=' * 80
48
- Schema.create
50
+ Models.make
49
51
  end
50
52
  config.before(:all) { Sham.reset(:before_all) }
51
53
  config.before(:each) { Sham.reset(:before_each) }
@@ -64,4 +66,4 @@ require 'squeel'
64
66
 
65
67
  Squeel.configure do |config|
66
68
  config.load_core_extensions :hash, :symbol
67
- end
69
+ end
@@ -754,7 +754,20 @@ module Squeel
754
754
 
755
755
  it "doesn't ruin everything when a group exists" do
756
756
  relation = Person.scoped.merge(Person.group{name})
757
- count_hash = relation.count
757
+ count_hash = {}
758
+ expect { count_hash = relation.count }.should_not raise_error
759
+ count_hash.size.should eq Person.scoped.size
760
+ count_hash.values.all? {|v| v == 1}.should be_true
761
+ count_hash.keys.should =~ Person.select{name}.map(&:name)
762
+ end
763
+
764
+ it "doesn't merge the default scope more than once" do
765
+ relation = PersonNamedBill.scoped.highly_compensated.ending_with_ill
766
+ sql = relation.to_sql
767
+ sql.scan(/"people"."name" = 'Bill'/).should have(1).item
768
+ sql.scan(/"people"."name" LIKE '%ill'/).should have(1).item
769
+ sql.scan(/"people"."salary" > 200000/).should have(1).item
770
+ sql.scan(/"people"."id"/).should have(1).item
758
771
  end
759
772
 
760
773
  end
@@ -0,0 +1,99 @@
1
+ class Person < ActiveRecord::Base
2
+ belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id
3
+ has_many :children, :class_name => 'Person', :foreign_key => :parent_id
4
+ has_many :articles
5
+ has_many :articles_with_condition, :class_name => 'Article', :conditions => {:title => 'Condition'}
6
+ has_many :comments
7
+ has_many :condition_article_comments, :through => :articles_with_condition, :source => :comments
8
+ has_many :authored_article_comments, :through => :articles,
9
+ :source => :comments
10
+ has_many :notes, :as => :notable
11
+ has_many :unidentified_objects
12
+
13
+ has_many :outgoing_messages, :class_name => 'Message', :foreign_key => :author_id
14
+ has_many :incoming_messages, :class_name => 'Message', :foreign_key => :recipient_id
15
+
16
+ scope :nil_scope, lambda { nil }
17
+
18
+ sifter :name_starts_or_ends_with do |value|
19
+ (name =~ "#{value}%") | (name =~ "%#{value}")
20
+ end
21
+ end
22
+
23
+ class PersonWithNamePrimaryKey < ActiveRecord::Base
24
+ set_primary_key 'name'
25
+ # Set this second, because I'm lazy and don't want to populate another table,
26
+ # and also don't want to clobber the AR connection's primary_key cache.
27
+ set_table_name 'people'
28
+ end
29
+
30
+ class PersonNamedBill < ActiveRecord::Base
31
+ self.table_name = 'people'
32
+ belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id
33
+ default_scope where{name == 'Bill'}.order{id}
34
+ scope :highly_compensated, where{salary > 200000}
35
+ scope :ending_with_ill, where{name =~ '%ill'}
36
+ end
37
+
38
+ class Message < ActiveRecord::Base
39
+ belongs_to :author, :class_name => 'Person'
40
+ belongs_to :recipient, :class_name => 'Person'
41
+ end
42
+
43
+ class UnidentifiedObject < ActiveRecord::Base
44
+ belongs_to :person
45
+ end
46
+
47
+ class Article < ActiveRecord::Base
48
+ belongs_to :person
49
+ has_many :comments
50
+ has_and_belongs_to_many :tags
51
+ has_many :notes, :as => :notable
52
+ has_many :commenters, :through => :comments, :source => :person
53
+ has_many :uniq_commenters, :through => :comments, :source => :person, :uniq => true
54
+ end
55
+
56
+ class Comment < ActiveRecord::Base
57
+ belongs_to :article
58
+ belongs_to :person
59
+ end
60
+
61
+ class Tag < ActiveRecord::Base
62
+ has_and_belongs_to_many :articles
63
+ end
64
+
65
+ class Note < ActiveRecord::Base
66
+ belongs_to :notable, :polymorphic => true
67
+ end
68
+
69
+ Dir[File.expand_path('../../blueprints/*.rb', __FILE__)].each do |f|
70
+ require f
71
+ end
72
+
73
+ class Models
74
+ def self.make
75
+ 10.times do
76
+ person = Person.make
77
+ 2.times do
78
+ UnidentifiedObject.create(:person => person, :name => Sham.object_name)
79
+ end
80
+ Note.make(:notable => person)
81
+ 3.times do
82
+ article = Article.make(:person => person)
83
+ 3.times do
84
+ article.tags = [Tag.make, Tag.make, Tag.make]
85
+ end
86
+ Note.make(:notable => article)
87
+ 10.times do
88
+ Comment.make(:article => article)
89
+ end
90
+ end
91
+ 2.times do
92
+ Comment.make(:person => person)
93
+ end
94
+ end
95
+
96
+ Comment.make(:body => 'First post!', :article => Article.make(:title => 'Hello, world!'))
97
+ Comment.make(:body => 'Last post!', :article => Article.first, :person => Article.first.commenters.first)
98
+ end
99
+ end
@@ -6,147 +6,53 @@ ActiveRecord::Base.establish_connection(
6
6
  :database => ':memory:'
7
7
  )
8
8
 
9
- class Person < ActiveRecord::Base
10
- belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id
11
- has_many :children, :class_name => 'Person', :foreign_key => :parent_id
12
- has_many :articles
13
- has_many :articles_with_condition, :class_name => 'Article', :conditions => {:title => 'Condition'}
14
- has_many :comments
15
- has_many :condition_article_comments, :through => :articles_with_condition, :source => :comments
16
- has_many :authored_article_comments, :through => :articles,
17
- :source => :comments
18
- has_many :notes, :as => :notable
19
- has_many :unidentified_objects
20
-
21
- has_many :outgoing_messages, :class_name => 'Message', :foreign_key => :author_id
22
- has_many :incoming_messages, :class_name => 'Message', :foreign_key => :recipient_id
23
-
24
- scope :nil_scope, lambda { nil }
25
-
26
- sifter :name_starts_or_ends_with do |value|
27
- (name =~ "#{value}%") | (name =~ "%#{value}")
28
- end
29
- end
30
-
31
- class PersonWithNamePrimaryKey < ActiveRecord::Base
32
- set_primary_key 'name'
33
- # Set this second, because I'm lazy and don't want to populate another table,
34
- # and also don't want to clobber the AR connection's primary_key cache.
35
- set_table_name 'people'
36
- end
37
-
38
- class PersonNamedBill < ActiveRecord::Base
39
- self.table_name = 'people'
40
- belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id
41
- default_scope where{name == 'Bill'}
42
- end
43
-
44
- class Message < ActiveRecord::Base
45
- belongs_to :author, :class_name => 'Person'
46
- belongs_to :recipient, :class_name => 'Person'
47
- end
48
-
49
- class UnidentifiedObject < ActiveRecord::Base
50
- belongs_to :person
51
- end
52
-
53
- class Article < ActiveRecord::Base
54
- belongs_to :person
55
- has_many :comments
56
- has_and_belongs_to_many :tags
57
- has_many :notes, :as => :notable
58
- has_many :commenters, :through => :comments, :source => :person
59
- has_many :uniq_commenters, :through => :comments, :source => :person, :uniq => true
60
- end
61
-
62
- class Comment < ActiveRecord::Base
63
- belongs_to :article
64
- belongs_to :person
65
- end
66
-
67
- class Tag < ActiveRecord::Base
68
- has_and_belongs_to_many :articles
69
- end
70
-
71
- class Note < ActiveRecord::Base
72
- belongs_to :notable, :polymorphic => true
73
- end
74
-
75
- module Schema
76
- def self.create
77
- ActiveRecord::Base.silence do
78
- ActiveRecord::Migration.verbose = false
79
-
80
- ActiveRecord::Schema.define do
81
- create_table :people, :force => true do |t|
82
- t.integer :parent_id
83
- t.string :name
84
- t.integer :salary
85
- end
86
-
87
- create_table :messages, :force => true do |t|
88
- t.integer :author_id
89
- t.integer :recipient_id
90
- end
91
-
92
- create_table :unidentified_objects, :id => false, :force => true do |t|
93
- t.integer :person_id
94
- t.string :name
95
- end
96
-
97
- create_table :articles, :force => true do |t|
98
- t.integer :person_id
99
- t.string :title
100
- t.text :body
101
- end
9
+ ActiveRecord::Base.silence do
10
+ ActiveRecord::Migration.verbose = false
11
+
12
+ ActiveRecord::Schema.define do
13
+ create_table :people, :force => true do |t|
14
+ t.integer :parent_id
15
+ t.string :name
16
+ t.integer :salary
17
+ end
102
18
 
103
- create_table :comments, :force => true do |t|
104
- t.integer :article_id
105
- t.integer :person_id
106
- t.text :body
107
- end
19
+ create_table :messages, :force => true do |t|
20
+ t.integer :author_id
21
+ t.integer :recipient_id
22
+ end
108
23
 
109
- create_table :tags, :force => true do |t|
110
- t.string :name
111
- end
24
+ create_table :unidentified_objects, :id => false, :force => true do |t|
25
+ t.integer :person_id
26
+ t.string :name
27
+ end
112
28
 
113
- create_table :articles_tags, :force => true, :id => false do |t|
114
- t.integer :article_id
115
- t.integer :tag_id
116
- end
29
+ create_table :articles, :force => true do |t|
30
+ t.integer :person_id
31
+ t.string :title
32
+ t.text :body
33
+ end
117
34
 
118
- create_table :notes, :force => true do |t|
119
- t.integer :notable_id
120
- t.string :notable_type
121
- t.string :note
122
- end
35
+ create_table :comments, :force => true do |t|
36
+ t.integer :article_id
37
+ t.integer :person_id
38
+ t.text :body
39
+ end
123
40
 
124
- end
41
+ create_table :tags, :force => true do |t|
42
+ t.string :name
125
43
  end
126
44
 
127
- 10.times do
128
- person = Person.make
129
- 2.times do
130
- UnidentifiedObject.create(:person => person, :name => Sham.object_name)
131
- end
132
- Note.make(:notable => person)
133
- 3.times do
134
- article = Article.make(:person => person)
135
- 3.times do
136
- article.tags = [Tag.make, Tag.make, Tag.make]
137
- end
138
- Note.make(:notable => article)
139
- 10.times do
140
- Comment.make(:article => article)
141
- end
142
- end
143
- 2.times do
144
- Comment.make(:person => person)
145
- end
45
+ create_table :articles_tags, :force => true, :id => false do |t|
46
+ t.integer :article_id
47
+ t.integer :tag_id
146
48
  end
147
49
 
148
- Comment.make(:body => 'First post!', :article => Article.make(:title => 'Hello, world!'))
149
- Comment.make(:body => 'Last post!', :article => Article.first, :person => Article.first.commenters.first)
50
+ create_table :notes, :force => true do |t|
51
+ t.integer :notable_id
52
+ t.string :notable_type
53
+ t.string :note
54
+ end
150
55
 
151
56
  end
152
57
  end
58
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squeel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-29 00:00:00.000000000 Z
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -224,6 +224,7 @@ files:
224
224
  - spec/squeel/visitors/attribute_visitor_spec.rb
225
225
  - spec/squeel/visitors/predicate_visitor_spec.rb
226
226
  - spec/squeel/visitors/symbol_visitor_spec.rb
227
+ - spec/support/models.rb
227
228
  - spec/support/schema.rb
228
229
  - squeel.gemspec
229
230
  homepage: http://erniemiller.org/projects/squeel
@@ -240,7 +241,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
240
241
  version: '0'
241
242
  segments:
242
243
  - 0
243
- hash: 2726279138342504508
244
+ hash: -377980682331781514
244
245
  required_rubygems_version: !ruby/object:Gem::Requirement
245
246
  none: false
246
247
  requirements:
@@ -249,7 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
250
  version: '0'
250
251
  segments:
251
252
  - 0
252
- hash: 2726279138342504508
253
+ hash: -377980682331781514
253
254
  requirements: []
254
255
  rubyforge_project: squeel
255
256
  rubygems_version: 1.8.24
@@ -287,4 +288,5 @@ test_files:
287
288
  - spec/squeel/visitors/attribute_visitor_spec.rb
288
289
  - spec/squeel/visitors/predicate_visitor_spec.rb
289
290
  - spec/squeel/visitors/symbol_visitor_spec.rb
291
+ - spec/support/models.rb
290
292
  - spec/support/schema.rb