brainstem 0.2.6 → 0.2.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/Gemfile.lock +18 -18
- data/README.md +0 -3
- data/lib/brainstem/presenter.rb +2 -1
- data/lib/brainstem/presenter_collection.rb +14 -19
- data/lib/brainstem/version.rb +1 -1
- data/pkg/brainstem-0.2.6.gem +0 -0
- data/spec/brainstem/presenter_collection_spec.rb +1 -17
- data/spec/brainstem/presenter_spec.rb +1 -1
- data/spec/spec_helpers/db.rb +4 -4
- metadata +20 -19
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZjgxZDgwYTRkN2Y0NGU0Y2NhMmFiYjMxZmJjNWE0ZmNjOWY3NWQ4Yw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZDY4MmYyNjBlZTNlMDJmMmU3Yjk1MDZhYjJhNTY0MDQ3NmY3MjRmMg==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NzZkOGU5Y2FlN2I0OTY0ZTkwMzkxYzkyYzMyOTc2MDU5MTA4ODk2NTI0ODEy
|
10
|
+
NzhmYzViODBmN2M2NmFkODk4NTdiM2U4YzBhNTZjODQ1NTllZmUxOWUzZWM3
|
11
|
+
MzFiMmEzMDViNGJkNjkwMmM0NzE1MTg0N2JhYjJkMzVmYzM1NTk=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
Mzk5ZmNlMmQ4ZGMxODI0OTY0ZjU1Y2FlYjQ1MDAwOWJiZjk0MDNlMzJhNjVi
|
14
|
+
N2RmMjkwZTUzZjJmM2UyOWE3OWQ1MTMwMzBhMWEyMjZjNjY1MDgwNGY5MTJj
|
15
|
+
YmI2NDBjZTA1N2EzM2Q3ZjFkZWRkNzk3NGYyZDBjZjdjNzY5MGM=
|
data/Gemfile.lock
CHANGED
@@ -1,39 +1,39 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
brainstem (0.2.6)
|
4
|
+
brainstem (0.2.6.1)
|
5
5
|
activerecord (>= 3.2)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (4.
|
11
|
-
activesupport (= 4.
|
10
|
+
activemodel (4.2.0)
|
11
|
+
activesupport (= 4.2.0)
|
12
12
|
builder (~> 3.1)
|
13
|
-
activerecord (4.
|
14
|
-
activemodel (= 4.
|
15
|
-
activesupport (= 4.
|
16
|
-
arel (~>
|
17
|
-
activesupport (4.
|
18
|
-
i18n (~> 0.
|
13
|
+
activerecord (4.2.0)
|
14
|
+
activemodel (= 4.2.0)
|
15
|
+
activesupport (= 4.2.0)
|
16
|
+
arel (~> 6.0)
|
17
|
+
activesupport (4.2.0)
|
18
|
+
i18n (~> 0.7)
|
19
19
|
json (~> 1.7, >= 1.7.7)
|
20
20
|
minitest (~> 5.1)
|
21
|
-
thread_safe (~> 0.
|
21
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
22
22
|
tzinfo (~> 1.1)
|
23
|
-
arel (
|
23
|
+
arel (6.0.0)
|
24
24
|
builder (3.2.2)
|
25
25
|
diff-lcs (1.2.5)
|
26
|
-
i18n (0.
|
26
|
+
i18n (0.7.0)
|
27
27
|
json (1.8.1)
|
28
|
-
minitest (5.
|
29
|
-
rake (10.
|
30
|
-
redcarpet (3.
|
28
|
+
minitest (5.5.0)
|
29
|
+
rake (10.4.2)
|
30
|
+
redcarpet (3.2.2)
|
31
31
|
rr (1.1.2)
|
32
32
|
rspec (3.1.0)
|
33
33
|
rspec-core (~> 3.1.0)
|
34
34
|
rspec-expectations (~> 3.1.0)
|
35
35
|
rspec-mocks (~> 3.1.0)
|
36
|
-
rspec-core (3.1.
|
36
|
+
rspec-core (3.1.7)
|
37
37
|
rspec-support (~> 3.1.0)
|
38
38
|
rspec-expectations (3.1.2)
|
39
39
|
diff-lcs (>= 1.2.0, < 2.0)
|
@@ -41,11 +41,11 @@ GEM
|
|
41
41
|
rspec-mocks (3.1.3)
|
42
42
|
rspec-support (~> 3.1.0)
|
43
43
|
rspec-support (3.1.2)
|
44
|
-
sqlite3 (1.3.
|
44
|
+
sqlite3 (1.3.10)
|
45
45
|
thread_safe (0.3.4)
|
46
46
|
tzinfo (1.2.2)
|
47
47
|
thread_safe (~> 0.1)
|
48
|
-
yard (0.8.7.
|
48
|
+
yard (0.8.7.6)
|
49
49
|
|
50
50
|
PLATFORMS
|
51
51
|
ruby
|
data/README.md
CHANGED
@@ -86,9 +86,6 @@ The scope passed to `present` could contain any starting conditions that you'd l
|
|
86
86
|
|
87
87
|
GET /api/widgets.json?include=features&order=popularity:desc&location_name=san+francisco
|
88
88
|
|
89
|
-
Additionally, requests can have a 'pretty' parameter. When set to true, responses will be pretty-printed.
|
90
|
-
Do this by adding `&pretty=true` to the above example.
|
91
|
-
|
92
89
|
Responses will look like the following:
|
93
90
|
|
94
91
|
{
|
data/lib/brainstem/presenter.rb
CHANGED
@@ -149,13 +149,14 @@ module Brainstem
|
|
149
149
|
# @api private
|
150
150
|
# Makes sure that associations are loaded and converted into ids.
|
151
151
|
def load_associations!(model, struct, associations)
|
152
|
+
reflections = Brainstem::PresenterCollection.reflections(model.class)
|
152
153
|
struct.to_a.each do |key, value|
|
153
154
|
if value.is_a?(AssociationField)
|
154
155
|
struct.delete key
|
155
156
|
id_attr = value.method_name ? "#{value.method_name}_id" : nil
|
156
157
|
|
157
158
|
if id_attr && model.class.columns_hash.has_key?(id_attr)
|
158
|
-
reflection = value.method_name &&
|
159
|
+
reflection = value.method_name && reflections[value.method_name.to_s]
|
159
160
|
if reflection && reflection.options[:polymorphic] && !value.ignore_type
|
160
161
|
struct["#{key.to_s.singularize}_ref".to_sym] = begin
|
161
162
|
if (id_attr = model.send(id_attr)).present?
|
@@ -108,9 +108,6 @@ module Brainstem
|
|
108
108
|
|
109
109
|
rewrite_keys_as_objects!(struct)
|
110
110
|
|
111
|
-
# Make struct pretty-print when rendered as json, if required.
|
112
|
-
make_pretty_printable!(struct) if options[:params][:pretty] == "true"
|
113
|
-
|
114
111
|
struct
|
115
112
|
end
|
116
113
|
|
@@ -175,6 +172,7 @@ module Brainstem
|
|
175
172
|
def calculate_allowed_includes(presenter, presented_class, is_only_query)
|
176
173
|
allowed_includes = {}
|
177
174
|
model = presented_class.new
|
175
|
+
reflections = Brainstem::PresenterCollection.reflections(model.class)
|
178
176
|
presenter.present(model).each do |k, v|
|
179
177
|
next unless v.is_a?(AssociationField)
|
180
178
|
next if v.restrict_to_only && !is_only_query
|
@@ -182,8 +180,8 @@ module Brainstem
|
|
182
180
|
if v.json_name
|
183
181
|
v.json_name = v.json_name.tableize.to_sym
|
184
182
|
else
|
185
|
-
association =
|
186
|
-
if !association.options[:polymorphic]
|
183
|
+
association = reflections[v.method_name.to_s]
|
184
|
+
if association && !association.options[:polymorphic]
|
187
185
|
v.json_name = association && association.table_name.to_sym
|
188
186
|
if v.json_name.nil?
|
189
187
|
raise ":json_name is a required option for method-based associations (#{presented_class}##{v.method_name})"
|
@@ -329,11 +327,11 @@ module Brainstem
|
|
329
327
|
records.tap do |models|
|
330
328
|
association_names_to_preload = includes_hash.values.map {|i| i.method_name }
|
331
329
|
if models.first
|
332
|
-
reflections = models.first.class
|
333
|
-
association_names_to_preload.reject! { |association| !reflections.has_key?(association) }
|
330
|
+
reflections = Brainstem::PresenterCollection.reflections(models.first.class)
|
331
|
+
association_names_to_preload.reject! { |association| !reflections.has_key?(association.to_s) }
|
334
332
|
end
|
335
333
|
if association_names_to_preload.any?
|
336
|
-
preload(models, association_names_to_preload)
|
334
|
+
Brainstem::PresenterCollection.preload(models, association_names_to_preload)
|
337
335
|
Brainstem.logger.info "Eager loaded #{association_names_to_preload.join(", ")}."
|
338
336
|
end
|
339
337
|
end
|
@@ -375,22 +373,19 @@ module Brainstem
|
|
375
373
|
end
|
376
374
|
end
|
377
375
|
|
378
|
-
|
376
|
+
# Class Methods
|
377
|
+
|
378
|
+
# In Rails 4.2, ActiveRecord::Base#reflections started being keyed by strings instead of symbols.
|
379
|
+
def self.reflections(klass)
|
380
|
+
klass.reflections.each_with_object({}) { |(key, value), memo| memo[key.to_s] = value }
|
381
|
+
end
|
382
|
+
|
383
|
+
def self.preload(models, association_names)
|
379
384
|
if Gem.loaded_specs['activerecord'].version >= Gem::Version.create('4.1')
|
380
385
|
ActiveRecord::Associations::Preloader.new.preload(models, association_names)
|
381
386
|
else
|
382
387
|
ActiveRecord::Associations::Preloader.new(models, association_names).run
|
383
388
|
end
|
384
389
|
end
|
385
|
-
|
386
|
-
# Modify a hash so that it is pretty-printed when rendered as JSON. Works
|
387
|
-
# by adding a `to_json` singleton method which is called during rendering.
|
388
|
-
# Uses a copy of the hash to avoid recursion issues.
|
389
|
-
def make_pretty_printable!(struct)
|
390
|
-
struct.define_singleton_method(:to_json) do |options = nil|
|
391
|
-
copy = self.deep_dup
|
392
|
-
JSON.pretty_generate(copy)
|
393
|
-
end
|
394
|
-
end
|
395
390
|
end
|
396
391
|
end
|
data/lib/brainstem/version.rb
CHANGED
Binary file
|
@@ -259,7 +259,7 @@ describe Brainstem::PresenterCollection do
|
|
259
259
|
|
260
260
|
it "preloads associations when they are full model-level associations" do
|
261
261
|
# Here, primary_maven is a method on Workspace, not a true association.
|
262
|
-
mock(
|
262
|
+
mock(Brainstem::PresenterCollection).preload(anything, [:tasks])
|
263
263
|
result = @presenter_collection.presenting("workspaces", :params => { :include => "tasks" }) { Workspace.order('id desc') }
|
264
264
|
expect(result[:tasks].length).to be > 0
|
265
265
|
end
|
@@ -825,22 +825,6 @@ describe Brainstem::PresenterCollection do
|
|
825
825
|
expect(result[:count]).to eq(Workspace.owned_by(bob.to_param).count)
|
826
826
|
end
|
827
827
|
end
|
828
|
-
|
829
|
-
describe "pretty printing" do
|
830
|
-
before do
|
831
|
-
@user_result = { :count => 1, :users => { 1 => { :username => "bob", :id => "1" } }, :results => [{ :key => "users", :id => "1" }] }
|
832
|
-
end
|
833
|
-
|
834
|
-
it "should return normal json if the pretty parameter is not true" do
|
835
|
-
result = @presenter_collection.presenting("users") { User.where(:id => 1) }
|
836
|
-
expect(result.to_json).to eq(JSON.generate(@user_result))
|
837
|
-
end
|
838
|
-
|
839
|
-
it "should return pretty printed json if the pretty parameter is true" do
|
840
|
-
result = @presenter_collection.presenting("users", :params => { :pretty => "true" }) { User.where(:id => 1) }
|
841
|
-
expect(result.to_json).to eq(JSON.pretty_generate(@user_result))
|
842
|
-
end
|
843
|
-
end
|
844
828
|
end
|
845
829
|
|
846
830
|
describe "collection methods" do
|
@@ -136,7 +136,7 @@ describe Brainstem::Presenter do
|
|
136
136
|
iso8601_time = /\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}[-+]\d{2}:\d{2}/
|
137
137
|
iso8601_date = /\d{4}-\d{2}-\d{2}/
|
138
138
|
|
139
|
-
struct = TimePresenter.new.present_and_post_process(
|
139
|
+
struct = TimePresenter.new.present_and_post_process(Workspace.first)
|
140
140
|
expect(struct[:time]).to match(iso8601_time)
|
141
141
|
expect(struct[:date]).to match(iso8601_date)
|
142
142
|
expect(struct[:recursion][:time]).to match(iso8601_time)
|
data/spec/spec_helpers/db.rb
CHANGED
@@ -4,28 +4,28 @@ ActiveRecord::Schema.define do
|
|
4
4
|
|
5
5
|
create_table :users, :force => true do |t|
|
6
6
|
t.string :username
|
7
|
-
t.timestamps
|
7
|
+
t.timestamps null: true
|
8
8
|
end
|
9
9
|
|
10
10
|
create_table :workspaces, :force => true do |t|
|
11
11
|
t.string :title
|
12
12
|
t.string :description
|
13
13
|
t.belongs_to :user
|
14
|
-
t.timestamps
|
14
|
+
t.timestamps null: true
|
15
15
|
end
|
16
16
|
|
17
17
|
create_table :tasks, :force => true do |t|
|
18
18
|
t.string :name
|
19
19
|
t.integer :parent_id
|
20
20
|
t.belongs_to :workspace
|
21
|
-
t.timestamps
|
21
|
+
t.timestamps null: true
|
22
22
|
end
|
23
23
|
|
24
24
|
create_table :posts, :force => true do |t|
|
25
25
|
t.string :body
|
26
26
|
t.integer :subject_id
|
27
27
|
t.string :subject_type
|
28
|
-
t.timestamps
|
28
|
+
t.timestamps null: true
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
metadata
CHANGED
@@ -1,111 +1,111 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brainstem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.6
|
4
|
+
version: 0.2.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mavenlink
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ! '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ! '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ! '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ! '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: redcarpet
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ! '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rr
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ! '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ! '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ! '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: sqlite3
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ! '>='
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ! '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: yard
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - ! '>='
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - ! '>='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
description: Brainstem allows you to create rich API presenters that know how to filter,
|
@@ -135,6 +135,7 @@ files:
|
|
135
135
|
- lib/brainstem/time_classes.rb
|
136
136
|
- lib/brainstem/version.rb
|
137
137
|
- pkg/brainstem-0.2.5.gem
|
138
|
+
- pkg/brainstem-0.2.6.gem
|
138
139
|
- spec/brainstem/controller_methods_spec.rb
|
139
140
|
- spec/brainstem/presenter_collection_spec.rb
|
140
141
|
- spec/brainstem/presenter_spec.rb
|
@@ -153,17 +154,17 @@ require_paths:
|
|
153
154
|
- lib
|
154
155
|
required_ruby_version: !ruby/object:Gem::Requirement
|
155
156
|
requirements:
|
156
|
-
- -
|
157
|
+
- - ! '>='
|
157
158
|
- !ruby/object:Gem::Version
|
158
159
|
version: '0'
|
159
160
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
161
|
requirements:
|
161
|
-
- -
|
162
|
+
- - ! '>='
|
162
163
|
- !ruby/object:Gem::Version
|
163
164
|
version: '0'
|
164
165
|
requirements: []
|
165
166
|
rubyforge_project:
|
166
|
-
rubygems_version: 2.
|
167
|
+
rubygems_version: 2.2.2
|
167
168
|
signing_key:
|
168
169
|
specification_version: 4
|
169
170
|
summary: ActiveRecord presenters with a rich request API
|