active_loaders 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/CHANGES.md +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +464 -0
- data/Rakefile +2 -0
- data/active_loaders.gemspec +30 -0
- data/lib/active_loaders.rb +6 -0
- data/lib/active_loaders/datasource_adapter.rb +216 -0
- data/lib/active_loaders/test.rb +100 -0
- data/lib/active_loaders/version.rb +3 -0
- data/spec/sequel_serializer_spec.rb +65 -0
- data/spec/sequel_skip_select_spec.rb +77 -0
- data/spec/serializer_spec.rb +62 -0
- data/spec/skip_select_spec.rb +74 -0
- data/spec/spec_helper.rb +41 -0
- data/spec/support/db.rb +53 -0
- data/spec/test_methods_spec.rb +57 -0
- metadata +211 -0
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module SerializerSpec
|
4
|
+
describe "Serializer", :activerecord do
|
5
|
+
class Comment < ActiveRecord::Base
|
6
|
+
belongs_to :post
|
7
|
+
end
|
8
|
+
|
9
|
+
class Post < ActiveRecord::Base
|
10
|
+
belongs_to :blog
|
11
|
+
has_many :comments
|
12
|
+
|
13
|
+
datasource_module do
|
14
|
+
query :author_name do
|
15
|
+
"posts.author_first_name || ' ' || posts.author_last_name"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Blog < ActiveRecord::Base
|
21
|
+
has_many :posts
|
22
|
+
end
|
23
|
+
|
24
|
+
class BlogSerializer < ActiveModel::Serializer
|
25
|
+
attributes :id, :title
|
26
|
+
|
27
|
+
has_many :posts
|
28
|
+
end
|
29
|
+
|
30
|
+
class PostSerializer < ActiveModel::Serializer
|
31
|
+
attributes :id, :title, :author_name
|
32
|
+
|
33
|
+
has_many :comments
|
34
|
+
end
|
35
|
+
|
36
|
+
class CommentSerializer < ActiveModel::Serializer
|
37
|
+
attributes :id, :comment
|
38
|
+
end
|
39
|
+
|
40
|
+
it "returns serialized hash" do
|
41
|
+
blog = Blog.create! title: "Blog 1"
|
42
|
+
post = blog.posts.create! title: "Post 1", author_first_name: "John", author_last_name: "Doe"
|
43
|
+
post.comments.create! comment: "Comment 1"
|
44
|
+
post = blog.posts.create! title: "Post 2", author_first_name: "Maria", author_last_name: "Doe"
|
45
|
+
post.comments.create! comment: "Comment 2"
|
46
|
+
blog = Blog.create! title: "Blog 2"
|
47
|
+
|
48
|
+
expected_result = [
|
49
|
+
{:id =>1, :title =>"Blog 1", :posts =>[
|
50
|
+
{:id =>1, :title =>"Post 1", :author_name =>"John Doe", comments: [{:id =>1, :comment =>"Comment 1"}]},
|
51
|
+
{:id =>2, :title =>"Post 2", :author_name =>"Maria Doe", comments: [{:id =>2, :comment =>"Comment 2"}]}
|
52
|
+
]},
|
53
|
+
{:id =>2, :title =>"Blog 2", :posts =>[]}
|
54
|
+
]
|
55
|
+
|
56
|
+
expect_query_count(3) do
|
57
|
+
serializer = ActiveModel::ArraySerializer.new(Blog.all)
|
58
|
+
expect(expected_result).to eq(serializer.as_json)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module SkipSelectSpec
|
4
|
+
describe "skip_select", :activerecord do
|
5
|
+
class Comment < ActiveRecord::Base
|
6
|
+
belongs_to :post
|
7
|
+
end
|
8
|
+
|
9
|
+
class Post < ActiveRecord::Base
|
10
|
+
belongs_to :blog
|
11
|
+
has_many :comments
|
12
|
+
|
13
|
+
datasource_module do
|
14
|
+
query :author_name do
|
15
|
+
"posts.author_first_name || ' ' || posts.author_last_name"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Blog < ActiveRecord::Base
|
21
|
+
has_many :posts
|
22
|
+
end
|
23
|
+
|
24
|
+
class BlogSerializer < ActiveModel::Serializer
|
25
|
+
attributes :id, :title
|
26
|
+
|
27
|
+
has_many :posts
|
28
|
+
end
|
29
|
+
|
30
|
+
class PostSerializer < ActiveModel::Serializer
|
31
|
+
attributes :id, :title, :author_name
|
32
|
+
|
33
|
+
has_many :comments
|
34
|
+
|
35
|
+
loaders do
|
36
|
+
skip_select :author_first_name, :author_last_name
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class CommentSerializer < ActiveModel::Serializer
|
41
|
+
attributes :id, :comment
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns serialized hash" do
|
45
|
+
blog = Blog.create! title: "Blog 1"
|
46
|
+
post = blog.posts.create! title: "Post 1", author_first_name: "John", author_last_name: "Doe"
|
47
|
+
post.comments.create! comment: "Comment 1"
|
48
|
+
post = blog.posts.create! title: "Post 2", author_first_name: "Maria", author_last_name: "Doe"
|
49
|
+
post.comments.create! comment: "Comment 2"
|
50
|
+
blog = Blog.create! title: "Blog 2"
|
51
|
+
|
52
|
+
expected_result = [
|
53
|
+
{:id =>1, :title =>"Blog 1", :posts =>[
|
54
|
+
{:id =>1, :title =>"Post 1", :author_name =>"John Doe", comments: [{:id =>1, :comment =>"Comment 1"}]},
|
55
|
+
{:id =>2, :title =>"Post 2", :author_name =>"Maria Doe", comments: [{:id =>2, :comment =>"Comment 2"}]},
|
56
|
+
]},
|
57
|
+
{:id =>2, :title =>"Blog 2", :posts =>[]}
|
58
|
+
]
|
59
|
+
|
60
|
+
expect_query_count(3) do |logger|
|
61
|
+
serializer = ActiveModel::ArraySerializer.new(Blog.all)
|
62
|
+
expect(expected_result).to eq(serializer.as_json)
|
63
|
+
expect(logger.string.lines[0]).to include("blogs.*")
|
64
|
+
expect(logger.string.lines[1]).to_not include("posts.*")
|
65
|
+
expect(logger.string.lines[1]).to_not include("posts.author_first_name,")
|
66
|
+
expect(logger.string.lines[1]).to_not include("posts.author_last_name,")
|
67
|
+
expect(logger.string.lines[1]).to include("posts.id")
|
68
|
+
expect(logger.string.lines[1]).to include("posts.title")
|
69
|
+
expect(logger.string.lines[1]).to include("posts.blog_id")
|
70
|
+
expect(logger.string.lines[2]).to include("comments.*")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
ENV["RAILS_ENV"] ||= 'test'
|
2
|
+
require 'rspec/core'
|
3
|
+
require 'rspec/expectations'
|
4
|
+
require 'rspec/mocks'
|
5
|
+
require 'database_cleaner'
|
6
|
+
require 'pry'
|
7
|
+
|
8
|
+
require 'active_support/all'
|
9
|
+
require 'active_record'
|
10
|
+
require 'sequel'
|
11
|
+
require 'datasource'
|
12
|
+
require 'active_loaders'
|
13
|
+
require 'active_loaders/test'
|
14
|
+
require 'active_model_serializers'
|
15
|
+
|
16
|
+
Datasource.setup do |config|
|
17
|
+
config.adapters = [:activerecord, :sequel]
|
18
|
+
config.raise_error_on_unknown_attribute_select = false
|
19
|
+
end
|
20
|
+
|
21
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
22
|
+
|
23
|
+
RSpec.configure do |config|
|
24
|
+
config.order = "random"
|
25
|
+
|
26
|
+
config.filter_run_including focus: true
|
27
|
+
config.run_all_when_everything_filtered = true
|
28
|
+
|
29
|
+
config.before(:suite) do
|
30
|
+
DatabaseCleaner.strategy = :truncation
|
31
|
+
DatabaseCleaner.clean_with(:truncation)
|
32
|
+
end
|
33
|
+
|
34
|
+
config.before :each do
|
35
|
+
DatabaseCleaner.start
|
36
|
+
end
|
37
|
+
|
38
|
+
config.after :each do
|
39
|
+
DatabaseCleaner.clean
|
40
|
+
end
|
41
|
+
end
|
data/spec/support/db.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
db_path = File.expand_path("../../db.sqlite3")
|
2
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: db_path)
|
3
|
+
Sequel::Model.db = Sequel.sqlite(db_path)
|
4
|
+
Sequel::Model.plugin :active_model
|
5
|
+
ActiveRecord::Migration.verbose = false
|
6
|
+
|
7
|
+
ActiveRecord::Schema.define(:version => 0) do
|
8
|
+
create_table :blogs, :force => true do |t|
|
9
|
+
t.string :title
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :posts, :force => true do |t|
|
13
|
+
t.integer :blog_id
|
14
|
+
t.string :title
|
15
|
+
t.string :author_first_name
|
16
|
+
t.string :author_last_name
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :comments, :force => true do |t|
|
20
|
+
t.integer :post_id
|
21
|
+
t.text :comment
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
Sequel::Model.send :include, ActiveModel::SerializerSupport
|
26
|
+
|
27
|
+
def expect_query_count(count)
|
28
|
+
logger_io = StringIO.new
|
29
|
+
logger = Logger.new(logger_io)
|
30
|
+
logger.formatter = ->(severity, datetime, progname, msg) { "#{msg}\n" }
|
31
|
+
if defined?(ActiveRecord::Base)
|
32
|
+
ar_old_logger = ActiveRecord::Base.logger
|
33
|
+
ActiveRecord::Base.logger = logger
|
34
|
+
end
|
35
|
+
if defined?(Sequel::Model)
|
36
|
+
Sequel::Model.db.loggers << logger
|
37
|
+
end
|
38
|
+
|
39
|
+
begin
|
40
|
+
yield(logger_io)
|
41
|
+
ensure
|
42
|
+
if defined?(ActiveRecord::Base)
|
43
|
+
ActiveRecord::Base.logger = ar_old_logger
|
44
|
+
end
|
45
|
+
if defined?(Sequel::Model)
|
46
|
+
Sequel::Model.db.loggers.delete(logger)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
output = logger_io.string
|
51
|
+
puts output if output.lines.count != count
|
52
|
+
expect(logger_io.string.lines.count).to eq(count)
|
53
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module TestMethodsSpec
|
4
|
+
describe "Test Methods" do
|
5
|
+
include ActiveLoaders::Test
|
6
|
+
|
7
|
+
class Post < ActiveRecord::Base
|
8
|
+
belongs_to :blog
|
9
|
+
end
|
10
|
+
class Blog < ActiveRecord::Base
|
11
|
+
has_many :posts
|
12
|
+
end
|
13
|
+
|
14
|
+
class PostSerializer < ActiveModel::Serializer
|
15
|
+
attributes :id, :title
|
16
|
+
end
|
17
|
+
|
18
|
+
class BlogSerializer < ActiveModel::Serializer
|
19
|
+
attributes :id, :title
|
20
|
+
has_many :posts
|
21
|
+
end
|
22
|
+
|
23
|
+
class BadBlogSerializer < ActiveModel::Serializer
|
24
|
+
attributes :id, :title, :stuff
|
25
|
+
|
26
|
+
def stuff
|
27
|
+
object.posts.to_a
|
28
|
+
"^^^ I was naughty ^^^"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should fail when data is not preloaded" do
|
33
|
+
blog = Blog.create! title: "The Blog"
|
34
|
+
2.times do
|
35
|
+
blog.posts.create! title: "The Post", author_first_name: "John", author_last_name: "Doe", blog_id: 10
|
36
|
+
end
|
37
|
+
|
38
|
+
expect { test_serializer_queries(BadBlogSerializer, Blog) }.to raise_error(ActiveLoaders::Test::Error)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should not fail when data is preloaded" do
|
42
|
+
blog = Blog.create! title: "The Blog"
|
43
|
+
2.times do
|
44
|
+
blog.posts.create! title: "The Post", author_first_name: "John", author_last_name: "Doe", blog_id: 10
|
45
|
+
end
|
46
|
+
|
47
|
+
expect { test_serializer_queries(BlogSerializer, Blog) }.to_not raise_error
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should fail when not all serializers were tested" do
|
51
|
+
blog = Blog.create! title: "The Blog"
|
52
|
+
|
53
|
+
test_serializer_queries(BlogSerializer, Blog)
|
54
|
+
expect { assert_all_serializers_tested(TestMethodsSpec) }.to raise_error(ActiveLoaders::Test::Error)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_loaders
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Berdajs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: active_model_serializers
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: datasource
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.3'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.2'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activerecord
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '4'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '4'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.9'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.9'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: sequel
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '4.17'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '4.17'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: database_cleaner
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '1.3'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '1.3'
|
153
|
+
description:
|
154
|
+
email:
|
155
|
+
- mrbrdo@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".gitignore"
|
161
|
+
- ".ruby-gemset"
|
162
|
+
- ".ruby-version"
|
163
|
+
- CHANGES.md
|
164
|
+
- Gemfile
|
165
|
+
- LICENSE.txt
|
166
|
+
- README.md
|
167
|
+
- Rakefile
|
168
|
+
- active_loaders.gemspec
|
169
|
+
- lib/active_loaders.rb
|
170
|
+
- lib/active_loaders/datasource_adapter.rb
|
171
|
+
- lib/active_loaders/test.rb
|
172
|
+
- lib/active_loaders/version.rb
|
173
|
+
- spec/sequel_serializer_spec.rb
|
174
|
+
- spec/sequel_skip_select_spec.rb
|
175
|
+
- spec/serializer_spec.rb
|
176
|
+
- spec/skip_select_spec.rb
|
177
|
+
- spec/spec_helper.rb
|
178
|
+
- spec/support/db.rb
|
179
|
+
- spec/test_methods_spec.rb
|
180
|
+
homepage: https://github.com/kundi/active_loaders
|
181
|
+
licenses:
|
182
|
+
- MIT
|
183
|
+
metadata: {}
|
184
|
+
post_install_message:
|
185
|
+
rdoc_options: []
|
186
|
+
require_paths:
|
187
|
+
- lib
|
188
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
189
|
+
requirements:
|
190
|
+
- - ">="
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
194
|
+
requirements:
|
195
|
+
- - ">="
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
version: '0'
|
198
|
+
requirements: []
|
199
|
+
rubyforge_project:
|
200
|
+
rubygems_version: 2.4.5
|
201
|
+
signing_key:
|
202
|
+
specification_version: 4
|
203
|
+
summary: Ruby library to automatically preload data for your Active Model Serializers
|
204
|
+
test_files:
|
205
|
+
- spec/sequel_serializer_spec.rb
|
206
|
+
- spec/sequel_skip_select_spec.rb
|
207
|
+
- spec/serializer_spec.rb
|
208
|
+
- spec/skip_select_spec.rb
|
209
|
+
- spec/spec_helper.rb
|
210
|
+
- spec/support/db.rb
|
211
|
+
- spec/test_methods_spec.rb
|