goldiloader 0.0.9 → 2.1.2

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.
data/spec/db/schema.rb DELETED
@@ -1,173 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- ActiveRecord::Schema.define(:version => 0) do
4
-
5
- create_table(:blogs, force: true) do |t|
6
- t.string :name
7
- end
8
-
9
- create_table(:posts, force: true) do |t|
10
- t.string :title
11
- t.integer :blog_id
12
- t.integer :author_id
13
- t.string :owner_type
14
- t.integer :owner_id
15
- end
16
-
17
- create_table(:users, force: true) do |t|
18
- t.string :name
19
- end
20
-
21
- create_table(:addresses, force: true) do |t|
22
- t.string :city
23
- t.integer :user_id
24
- end
25
-
26
- create_table(:groups, force: true) do |t|
27
- t.string :name
28
- end
29
-
30
- create_table(:tags, force: true) do |t|
31
- t.string :name
32
- t.integer :parent_id
33
- t.string :owner_type
34
- t.integer :owner_id
35
- end
36
-
37
- create_table(:post_tags, force: true) do |t|
38
- t.integer :post_id
39
- t.integer :tag_id
40
- end
41
- end
42
-
43
- class Tag < ActiveRecord::Base
44
- belongs_to :parent, class_name: 'Tag'
45
- has_many :children, class_name: 'Tag', foreign_key: :parent_id
46
-
47
- belongs_to :owner, polymorphic: true
48
- has_many :post_tags
49
- has_many :posts, through: :post_tags
50
-
51
- if Goldiloader::Compatibility.mass_assignment_security_enabled?
52
- attr_accessible :name
53
- end
54
- end
55
-
56
- class PostTag < ActiveRecord::Base
57
- belongs_to :post
58
- belongs_to :tag
59
- end
60
-
61
- class Blog < ActiveRecord::Base
62
- has_many :posts
63
- has_many :posts_without_auto_include, auto_include: false, class_name: 'Post'
64
- has_many :posts_fully_load, fully_load: true, class_name: 'Post'
65
-
66
- if ActiveRecord::VERSION::MAJOR >= 4
67
- has_many :read_only_posts, -> { readonly }, class_name: 'Post'
68
- has_many :limited_posts, -> { limit(2) }, class_name: 'Post'
69
- has_many :grouped_posts, -> { group(:blog_id) }, class_name: 'Post'
70
- has_many :offset_posts, -> { offset(2) }, class_name: 'Post'
71
- has_many :from_posts, -> { from('(select distinct blog_id from posts) as posts') }, class_name: 'Post'
72
- has_many :instance_dependent_posts, ->(instance) { Post.where(blog_id: instance.id) }, class_name: 'Post'
73
-
74
- has_many :posts_ordered_by_author, -> { joins(:author).order('users.name') }, class_name: 'Post'
75
-
76
- has_many :authors_with_join, -> { joins(:address).order('addresses.city') }, through: :posts, source: :author
77
- else
78
- has_many :read_only_posts, readonly: true, class_name: 'Post'
79
- has_many :limited_posts, limit: 2, class_name: 'Post'
80
- has_many :grouped_posts, group: :blog_id, class_name: 'Post'
81
- has_many :offset_posts, offset: 2, class_name: 'Post'
82
-
83
- has_many :posts_ordered_by_author, include: :author, order: 'users.name', class_name: 'Post'
84
- end
85
-
86
- if Goldiloader::Compatibility.association_finder_sql_enabled?
87
- has_many :finder_sql_posts, finder_sql: Proc.new { "select distinct blog_id from posts where blog_id = #{self.id}" },
88
- class_name: 'Post'
89
- end
90
-
91
- has_many :posts_overridden, class_name: 'Post'
92
- has_many :authors, through: :posts
93
- has_many :addresses, through: :authors
94
-
95
- if Goldiloader::Compatibility.mass_assignment_security_enabled?
96
- attr_accessible :name
97
- end
98
-
99
- def posts_overridden
100
- 'boom'
101
- end
102
- end
103
-
104
- class Post < ActiveRecord::Base
105
- belongs_to :blog
106
- belongs_to :blog_without_auto_include, auto_include: false, class_name: 'Blog', foreign_key: :blog_id
107
- belongs_to :author, class_name: 'User'
108
- has_many :post_tags
109
- has_many :tags, through: :post_tags
110
-
111
- belongs_to :owner, polymorphic: true
112
-
113
- if ActiveRecord::VERSION::MAJOR >= 4
114
- has_many :unique_tags, -> { distinct }, through: :post_tags, source: :tag, class_name: 'Tag'
115
- else
116
- has_many :unique_tags, through: :post_tags, source: :tag, uniq: true, class_name: 'Tag'
117
- end
118
-
119
- if ActiveRecord::VERSION::MAJOR < 4
120
- has_and_belongs_to_many :unique_tags_has_and_belongs, join_table: :post_tags, class_name: 'Tag', uniq: true
121
- end
122
-
123
- after_destroy :after_post_destroy
124
-
125
- if Goldiloader::Compatibility.mass_assignment_security_enabled?
126
- attr_accessible :title
127
- end
128
-
129
- def after_post_destroy
130
- # Hook for tests
131
- end
132
- end
133
-
134
- class User < ActiveRecord::Base
135
- has_many :posts, foreign_key: :author_id
136
- has_many :tags, as: :owner
137
- has_one :address
138
- has_one :address_without_auto_include, auto_include: false, class_name: 'Address'
139
-
140
- if Goldiloader::Compatibility.unscope_query_method_enabled?
141
- has_one :scoped_address_with_default_scope_remove, -> { unscope(where: :city) }, class_name: 'ScopedAddress'
142
- end
143
-
144
- if Goldiloader::Compatibility.mass_assignment_security_enabled?
145
- attr_accessible :name
146
- end
147
- end
148
-
149
- class Address < ActiveRecord::Base
150
- belongs_to :user
151
-
152
- if Goldiloader::Compatibility.mass_assignment_security_enabled?
153
- attr_accessible :city
154
- end
155
- end
156
-
157
- class ScopedAddress < ActiveRecord::Base
158
- self.table_name = 'addresses'
159
- default_scope { where(city: ['Philadelphia'])}
160
- belongs_to :user
161
-
162
- if Goldiloader::Compatibility.mass_assignment_security_enabled?
163
- attr_accessible :city
164
- end
165
- end
166
-
167
- class Group < ActiveRecord::Base
168
- has_many :tags, as: :owner
169
-
170
- if Goldiloader::Compatibility.mass_assignment_security_enabled?
171
- attr_accessible :name
172
- end
173
- end
@@ -1,164 +0,0 @@
1
- require 'spec_helper'
2
- require 'ostruct'
3
-
4
- describe Goldiloader::AutoIncludeContext do
5
- describe ".register_models" do
6
- context "when included_associations is an array of symbols" do
7
- let!(:roots) do
8
- [
9
- create_mock_model(cars: cars.take(2), fruit: fruits.first),
10
- create_mock_model(cars: cars.drop(2).take(2), fruit: fruits.last)
11
- ]
12
- end
13
- let!(:cars) { create_mock_models(4) }
14
- let!(:fruits) { create_mock_models(2) }
15
-
16
- before do
17
- Goldiloader::AutoIncludeContext.register_models(roots, [:cars, :fruit])
18
- end
19
-
20
- it "sets the AutoIncludeContext for roots" do
21
- expect(roots.map(&:auto_include_context).uniq.size).to eq 1
22
- expect(roots.first.auto_include_context.models).to match_array(roots)
23
- end
24
-
25
- it "sets the AutoIncludeContext for singluar nested associations" do
26
- expect(fruits.map(&:auto_include_context).uniq.size).to eq 1
27
- expect(fruits.first.auto_include_context.models).to match_array(fruits)
28
- end
29
-
30
- it "sets the AutoIncludeContext for collection nested associations" do
31
- expect(cars.map(&:auto_include_context).uniq.size).to eq 1
32
- expect(cars.first.auto_include_context.models).to match_array(cars)
33
- end
34
- end
35
-
36
- context "when included_associations is a hash" do
37
- let!(:roots) do
38
- [
39
- create_mock_model(car: cars.first),
40
- create_mock_model(car: cars.last)
41
- ]
42
- end
43
-
44
- let!(:cars) do
45
- [
46
- create_mock_model(wheels: wheels.take(4)),
47
- create_mock_model(wheels: wheels.drop(4).take(4))
48
- ]
49
- end
50
-
51
- let!(:wheels) { create_mock_models(8) }
52
-
53
- before do
54
- Goldiloader::AutoIncludeContext.register_models(roots, car: :wheels)
55
- end
56
-
57
- it "sets the AutoIncludeContext for roots" do
58
- expect(roots.map(&:auto_include_context).uniq.size).to eq 1
59
- expect(roots.first.auto_include_context.models).to match_array(roots)
60
- end
61
-
62
- it "sets the AutoIncludeContext for child nested associations" do
63
- expect(cars.map(&:auto_include_context).uniq.size).to eq 1
64
- expect(cars.first.auto_include_context.models).to match_array(cars)
65
- end
66
-
67
- it "sets the AutoIncludeContext for grandchild nested associations" do
68
- expect(wheels.map(&:auto_include_context).uniq.size).to eq 1
69
- expect(wheels.first.auto_include_context.models).to match_array(wheels)
70
- end
71
- end
72
-
73
- context "when included_associations is an array that mixes hashes and symbols" do
74
- let!(:roots) do
75
- [
76
- create_mock_model(car: cars.first, person: people.first),
77
- create_mock_model(car: cars.last, person: people.last)
78
- ]
79
- end
80
-
81
- let!(:people) { create_mock_models(2) }
82
-
83
- let!(:cars) do
84
- [
85
- create_mock_model(wheels: wheels.take(4)),
86
- create_mock_model(wheels: wheels.drop(4).take(4))
87
- ]
88
- end
89
-
90
- let!(:wheels) { create_mock_models(8) }
91
-
92
- before do
93
- Goldiloader::AutoIncludeContext.register_models(roots, [:person, car: :wheels])
94
- end
95
-
96
- it "sets the AutoIncludeContext for roots" do
97
- expect(roots.map(&:auto_include_context).uniq.size).to eq 1
98
- expect(roots.first.auto_include_context.models).to match_array(roots)
99
- end
100
-
101
- it "sets the AutoIncludeContext for child nested associations specified with a symbol" do
102
- expect(people.map(&:auto_include_context).uniq.size).to eq 1
103
- expect(people.first.auto_include_context.models).to match_array(people)
104
- end
105
-
106
- it "sets the AutoIncludeContext for child nested associations specified with a hash" do
107
- expect(cars.map(&:auto_include_context).uniq.size).to eq 1
108
- expect(cars.first.auto_include_context.models).to match_array(cars)
109
- end
110
-
111
- it "sets the AutoIncludeContext for grandchild nested associations" do
112
- expect(wheels.map(&:auto_include_context).uniq.size).to eq 1
113
- expect(wheels.first.auto_include_context.models).to match_array(wheels)
114
- end
115
- end
116
-
117
- context "when nested associations are nil" do
118
- let!(:roots) do
119
- [
120
- create_mock_model(car: cars.first),
121
- create_mock_model(car: nil),
122
- create_mock_model(car: cars.last)
123
- ]
124
- end
125
-
126
- let!(:cars) do
127
- [
128
- create_mock_model,
129
- create_mock_model
130
- ]
131
- end
132
-
133
- before do
134
- Goldiloader::AutoIncludeContext.register_models(roots, :car)
135
- end
136
-
137
- it "sets the AutoIncludeContext for roots" do
138
- expect(roots.map(&:auto_include_context).uniq.size).to eq 1
139
- expect(roots.first.auto_include_context.models).to match_array(roots)
140
- end
141
-
142
- it "sets the AutoIncludeContext for child nested associations" do
143
- expect(cars.map(&:auto_include_context).uniq.size).to eq 1
144
- expect(cars.first.auto_include_context.models).to match_array(cars)
145
- end
146
- end
147
-
148
- def create_mock_models(num)
149
- num.times.map { create_mock_model }
150
- end
151
-
152
- def create_mock_model(associations = {})
153
- model = AutoIncludeContextMockModel.new
154
- associations.each do |association, models|
155
- allow(model).to receive(:association).with(association) do
156
- OpenStruct.new(target: models)
157
- end
158
- end
159
- model
160
- end
161
-
162
- AutoIncludeContextMockModel = Struct.new(:auto_include_context)
163
- end
164
- end