encore 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 67ae9330944ea710370dd8ea1032b1a7b47267dd
4
- data.tar.gz: 5a6156f14a3d3c1d753b6d5e9ce31ca8a365b6e3
3
+ metadata.gz: 723164fd61a92235eaac47843e2ed972c2d5c57b
4
+ data.tar.gz: 0fdfba4c4088a0cdfad32b9549951138befeff20
5
5
  SHA512:
6
- metadata.gz: e4c6e0c8c5e7868808950430b841eee3a5bf6390491eadd41d96910399a01aed48d8436bec65871da3bcf10481b6440ab4d18daffdcb98071b07a045da9b2344
7
- data.tar.gz: d4a85a515aec1882439173b62004239fe8e0adcfafde0f0bde7d49b801f813839f9f469f52987c40e11cee1d87a6691074b63f443f227efb249a1b1d06a9759c
6
+ metadata.gz: a1da57f4c3ccaa478dec254bbd1c2058c722c7212d3e0fac524f4db11541a2297ad75db4e2176b50d8483fd3cdb9f0ccf5c5ad7557cee5531747d152606ddf17
7
+ data.tar.gz: e901741536ae58e5e1529e6e92e62d89cc70c6f00eb0d0663481d0b283e23f6077fde90306ac920c11437c70a8daac154d10193eff7b77d34aff87d8b2feaffd
data/.travis.yml CHANGED
@@ -6,9 +6,12 @@ rvm:
6
6
 
7
7
  gemfile:
8
8
  - gemfiles/Gemfile.activerecord-4.0
9
- - gemfiles/Gemfile.activerecord-4.1
9
+ - gemfiles/Gemfile.activerecord-4.1.3
10
+ - gemfiles/Gemfile.activerecord-4.1.4
10
11
 
11
- script: "echo 'DO IT' && bundle exec rake spec"
12
+ script:
13
+ - 'echo "Checking code style" && bundle exec phare'
14
+ - 'echo "Running specs" && RAILS_ENV=test bundle exec rake spec'
12
15
 
13
16
  notifications:
14
17
  hipchat:
data/encore.gemspec CHANGED
@@ -20,11 +20,11 @@ Gem::Specification.new do |spec|
20
20
  spec.add_dependency 'activemodel', '>= 4.0.0'
21
21
  spec.add_dependency 'activerecord', '>= 4.0.0'
22
22
  spec.add_dependency 'active_model_serializers', '~> 0.8.0'
23
- spec.add_dependency 'kaminari', '~> 0.15.1'
23
+ spec.add_dependency 'kaminari', '>= 0.15'
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.3'
26
26
  spec.add_development_dependency 'rake', '~> 10.3'
27
- spec.add_development_dependency 'rspec', '3.0.0beta2'
27
+ spec.add_development_dependency 'rspec', '~> 3.0.0'
28
28
  spec.add_development_dependency 'sqlite3', '>= 1.3.8', '< 1.4'
29
29
  spec.add_development_dependency 'rubocop', '0.22'
30
30
  spec.add_development_dependency 'phare'
@@ -2,4 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec path: '../'
4
4
 
5
- gem 'activerecord', '~> 4.1.0'
5
+ gem 'activerecord', '4.1.3'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'activerecord', '~> 4.1.4'
@@ -7,7 +7,8 @@ module Encore
7
7
  links = args.delete(:links) || []
8
8
 
9
9
  links.each do |link, value|
10
- reflection = @model.reflections[link.to_sym]
10
+ reflections = @model.try(:_reflections) || @model.reflections
11
+ reflection = reflections[link.to_sym]
11
12
  key = fetch_key(reflection)
12
13
  value = fetch_value(value, reflection)
13
14
 
@@ -10,7 +10,8 @@ module Encore
10
10
  end
11
11
 
12
12
  def links
13
- object.reflections.each_with_object({}) do |(_, reflection), memo|
13
+ reflections = object.try(:_reflections) || object.reflections
14
+ reflections.each_with_object({}) do |(_, reflection), memo|
14
15
  if object.association(reflection.name).loaded?
15
16
  fetcher = LinksReflectionIncluder::Loaded
16
17
  else
@@ -34,7 +34,9 @@ module Encore
34
34
  private
35
35
 
36
36
  def reflections
37
- @reflections ||= @collection.klass.reflections
37
+ @reflections ||= begin
38
+ @collection.klass.try(:_reflections) || @collection.klass.reflections
39
+ end
38
40
  end
39
41
 
40
42
  def serializer
@@ -14,7 +14,8 @@ module Encore
14
14
  serializer = Utils.fetch_serializer(klass)
15
15
 
16
16
  collection = klass.where(id: ids.to_a)
17
- available_includes = klass.reflections.map do |key, _|
17
+ reflections = klass.try(:_reflections) || klass.reflections
18
+ available_includes = reflections.map do |key, _|
18
19
  next unless included_models.include?(key.to_s)
19
20
  key
20
21
  end.compact
@@ -17,33 +17,46 @@ module Encore
17
17
 
18
18
  module NotLoaded
19
19
  def self.reflection_has_many(object, reflection)
20
- reflection_type = reflection.name.to_s.pluralize
20
+ reflection_type = plural_path(reflection.klass)
21
21
 
22
22
  {
23
- href: "/#{reflection_type}?#{object.class.name.downcase}_id=#{object.id}",
23
+ href: "/#{reflection_type}?#{object.class.name.underscore}_id=#{object.id}",
24
24
  type: reflection_type
25
25
  }
26
26
  end
27
27
 
28
28
  def self.reflection_has_one(object, reflection)
29
- reflection_type = reflection.name.to_s
29
+ root_type = plural_path(object.class)
30
+ reflection_type = singular_path(reflection.klass)
30
31
 
31
32
  {
32
- href: "/#{object.class.name.downcase.pluralize}/#{object.id}/#{reflection_type}",
33
+ href: "/#{root_type}/#{object.id}/#{reflection_type}",
33
34
  type: reflection_type.pluralize
34
35
  }
35
36
  end
36
37
 
37
38
  def self.reflection_belongs_to(object, reflection)
38
- reflection_type = reflection.name.to_s.pluralize
39
+ reflection_type = plural_path(reflection.klass)
39
40
  reflection_id = object.send(reflection.foreign_key).try(:to_s)
40
41
 
42
+ return nil if reflection_id.blank?
43
+
41
44
  {
42
45
  href: "/#{reflection_type}/#{reflection_id}",
43
46
  id: reflection_id,
44
47
  type: reflection_type
45
48
  }
46
49
  end
50
+
51
+ private
52
+
53
+ def self.plural_path(klass)
54
+ klass.active_model_serializer.root_key.to_s
55
+ end
56
+
57
+ def self.singular_path(klass)
58
+ plural_path(klass).singularize
59
+ end
47
60
  end
48
61
  end
49
62
  end
@@ -1,3 +1,3 @@
1
1
  module Encore
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -71,8 +71,8 @@ describe Encore::Serializer do
71
71
  end
72
72
 
73
73
  context 'already included resource' do
74
- it { expect(serialized[:linked][:projects][0][:links][:users]).to eq({ href: '/users?project_id=1', type: 'users' }) }
75
- it { expect(serialized[:linked][:projects][1][:links][:users]).to eq({ href: '/users?project_id=2', type: 'users' }) }
74
+ it { expect(serialized[:linked][:projects][0][:links][:users]).to eq(href: '/users?project_id=1', type: 'users') }
75
+ it { expect(serialized[:linked][:projects][1][:links][:users]).to eq(href: '/users?project_id=2', type: 'users') }
76
76
  end
77
77
 
78
78
  context 'not included resource' do
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Serializer do
4
+ let(:serializer) { Encore::Serializer::Instance }
5
+ let(:objects) { User.all }
6
+ let(:serialized) { serializer.new(objects, include: include).as_json }
7
+
8
+ before do
9
+ run_migrations!
10
+ spawn_objects!
11
+ create_records!
12
+ end
13
+
14
+ let(:run_migrations!) do
15
+ run_migration do
16
+ create_table(:users, force: true) do |t|
17
+ t.string :name, default: nil
18
+ t.integer :project_id
19
+ t.integer :event_result_id
20
+ end
21
+ create_table(:projects, force: true) do |t|
22
+ t.string :name, default: nil
23
+ t.integer :user_id
24
+ end
25
+ create_table(:event_results, force: true) do |t|
26
+ t.string :score, default: nil
27
+ t.integer :user_id
28
+ end
29
+ end
30
+ end
31
+
32
+ let(:spawn_objects!) do
33
+ spawn_model('User') do
34
+ belongs_to :project
35
+ end
36
+ spawn_serializer('UserSerializer') do
37
+ attributes :name, :links
38
+
39
+ can_include :project
40
+ end
41
+ spawn_model('Project') do
42
+ has_many :users
43
+ end
44
+ spawn_serializer('ProjectSerializer') do
45
+ attributes :name
46
+ end
47
+
48
+ end
49
+ let(:create_records!) do
50
+ User.create name: 'Allan', project_id: 1
51
+ User.create name: 'Doe', project_id: 2
52
+ User.create name: 'Bar', project_id: nil
53
+ end
54
+
55
+ context 'not included' do
56
+ let(:include) { '' }
57
+ let(:expected_project) do
58
+ {
59
+ href: '/projects/1',
60
+ id: '1',
61
+ type: 'projects'
62
+ }
63
+ end
64
+
65
+ it { expect(serialized[:users][0][:links][:project]).to eq(expected_project) }
66
+ end
67
+
68
+ context 'empty association' do
69
+ let(:include) { '' }
70
+ let(:expected_project) do
71
+ nil
72
+ end
73
+
74
+ it { expect(serialized[:users][2][:links][:project]).to eq(expected_project) }
75
+ end
76
+
77
+ context 'included' do
78
+ let(:include) { 'project' }
79
+ let(:expected_project) { '1' }
80
+
81
+ it { expect(serialized[:users][0][:links][:project]).to eq(expected_project) }
82
+ end
83
+ end
@@ -0,0 +1,147 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Serializer do
4
+ let(:serializer) { Encore::Serializer::Instance }
5
+ let(:objects) { User.all }
6
+ let(:serialized) { serializer.new(objects, include: include).as_json }
7
+
8
+ before do
9
+ run_migrations!
10
+ spawn_objects!
11
+ create_records!
12
+ end
13
+
14
+ let(:run_migrations!) do
15
+ run_migration do
16
+ create_table(:users, force: true) do |t|
17
+ t.string :name, default: nil
18
+ t.integer :project_id
19
+ t.integer :event_result_id
20
+ end
21
+ create_table(:projects, force: true) do |t|
22
+ t.string :name, default: nil
23
+ t.integer :user_id
24
+ end
25
+ create_table(:event_results, force: true) do |t|
26
+ t.string :score, default: nil
27
+ t.integer :user_id
28
+ end
29
+ end
30
+ end
31
+
32
+ context 'default' do
33
+ let(:spawn_objects!) do
34
+ spawn_model('User') do
35
+ belongs_to :project
36
+ belongs_to :irrelevant_reflection
37
+ end
38
+ spawn_serializer('UserSerializer') do
39
+ attributes :name, :links
40
+
41
+ can_include :project
42
+ end
43
+ spawn_model('Project') do
44
+ has_many :users
45
+ end
46
+ spawn_serializer('ProjectSerializer') do
47
+ attributes :name
48
+ end
49
+
50
+ end
51
+ let(:create_records!) do
52
+ User.create name: 'Allan', project_id: 1
53
+ User.create name: 'Doe', project_id: 2
54
+ end
55
+ let(:include) { '' }
56
+
57
+ it { expect(serialized[:users][0][:links][:irrelevant_reflection]).to eq(nil) }
58
+ end
59
+
60
+ context 'not loaded with access' do
61
+ let(:spawn_objects!) do
62
+ spawn_model('User') do
63
+ belongs_to :project
64
+ belongs_to :irrelevant_reflection
65
+ end
66
+ spawn_serializer('UserSerializer') do
67
+ attributes :name, :links
68
+
69
+ can_include :project
70
+ end
71
+ spawn_model('Project') do
72
+ has_many :users
73
+ end
74
+ spawn_serializer('ProjectSerializer') do
75
+ attributes :name
76
+ end
77
+
78
+ end
79
+ let(:create_records!) do
80
+ User.create name: 'Allan', project_id: 1
81
+ User.create name: 'Doe', project_id: 2
82
+ end
83
+ let(:include) { '' }
84
+
85
+ it { expect(serialized[:users][0][:links][:project].present?).to eq(true) }
86
+ end
87
+
88
+ context 'not loaded without access' do
89
+ let(:spawn_objects!) do
90
+ spawn_model('User') do
91
+ belongs_to :project
92
+ belongs_to :irrelevant_reflection
93
+ end
94
+ spawn_serializer('UserSerializer') do
95
+ attributes :name, :links
96
+
97
+ can_include :project
98
+
99
+ can_access []
100
+ end
101
+ spawn_model('Project') do
102
+ has_many :users
103
+ end
104
+ spawn_serializer('ProjectSerializer') do
105
+ attributes :name
106
+ end
107
+
108
+ end
109
+ let(:create_records!) do
110
+ User.create name: 'Allan', project_id: 1
111
+ User.create name: 'Doe', project_id: 2
112
+ end
113
+ let(:include) { '' }
114
+
115
+ it { expect(serialized[:users][0][:links][:project].present?).to eq(false) }
116
+ end
117
+
118
+ context 'loaded without access' do
119
+ let(:spawn_objects!) do
120
+ spawn_model('User') do
121
+ belongs_to :project
122
+ belongs_to :irrelevant_reflection
123
+ end
124
+ spawn_serializer('UserSerializer') do
125
+ attributes :name, :links
126
+
127
+ can_include :project
128
+
129
+ can_access []
130
+ end
131
+ spawn_model('Project') do
132
+ has_many :users
133
+ end
134
+ spawn_serializer('ProjectSerializer') do
135
+ attributes :name
136
+ end
137
+
138
+ end
139
+ let(:create_records!) do
140
+ User.create name: 'Allan', project_id: 1
141
+ User.create name: 'Doe', project_id: 2
142
+ end
143
+ let(:include) { 'project' }
144
+
145
+ it { expect(serialized[:users][0][:links][:project].present?).to eq(true) }
146
+ end
147
+ end
@@ -0,0 +1,151 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Serializer do
4
+ let(:serializer) { Encore::Serializer::Instance }
5
+ let(:serialized) { serializer.new(objects).as_json }
6
+
7
+ before do
8
+ run_migrations!
9
+ spawn_models!
10
+ spawn_serializers!
11
+ create_records!
12
+ end
13
+
14
+ let(:run_migrations!) do
15
+ run_migration do
16
+ create_table(:users, force: true) do |t|
17
+ t.string :name, default: nil
18
+ t.integer :organization_id
19
+ end
20
+ create_table(:projects, force: true) do |t|
21
+ t.string :name, default: nil
22
+ t.integer :creator_id
23
+ t.integer :user_id
24
+ end
25
+ create_table(:organizations, force: true)
26
+ end
27
+ end
28
+
29
+ let(:spawn_models!) do
30
+ spawn_model('Organization') do
31
+ has_many :artists, class_name: 'User'
32
+ end
33
+ spawn_model('User') do
34
+ has_one :awesome_project, class_name: 'Project'
35
+ end
36
+ spawn_model('Project') do
37
+ belongs_to :creator, class_name: 'User'
38
+ end
39
+ end
40
+
41
+ let(:create_records!) do
42
+ Organization.create
43
+ p1 = Project.create name: 'p1', creator_id: 1
44
+ p2 = Project.create name: 'p2', creator_id: 2
45
+ User.create name: 'Allan', awesome_project: p1, organization_id: 1
46
+ User.create name: 'Doe', awesome_project: p2, organization_id: 1
47
+ end
48
+
49
+ context 'default root key' do
50
+ let(:spawn_serializers!) do
51
+ spawn_serializer('OrganizationSerializer') do
52
+ can_include :artists
53
+ end
54
+ spawn_serializer('UserSerializer') do
55
+ can_include :awesome_project
56
+ end
57
+ spawn_serializer('ProjectSerializer') do
58
+ can_include :creator
59
+ end
60
+ end
61
+ context 'has_one' do
62
+ let(:objects) { User.all }
63
+ let(:expected_project) do
64
+ {
65
+ href: '/users/1/project',
66
+ type: 'projects'
67
+ }
68
+ end
69
+
70
+ it { expect(serialized[:users][0][:links][:awesome_project]).to eq(expected_project) }
71
+ end
72
+
73
+ context 'has_many' do
74
+ let(:objects) { Organization.all }
75
+ let(:expected_artists) do
76
+ {
77
+ href: '/users?organization_id=1',
78
+ type: 'users'
79
+ }
80
+ end
81
+
82
+ it { expect(serialized[:organizations][0][:links][:artists]).to eq(expected_artists) }
83
+ end
84
+
85
+ context 'belongs_to' do
86
+ let(:objects) { Project.all }
87
+ let(:expected_creator) do
88
+ {
89
+ href: '/users/1',
90
+ id: '1',
91
+ type: 'users'
92
+ }
93
+ end
94
+
95
+ it { expect(serialized[:projects][0][:links][:creator]).to eq(expected_creator) }
96
+ end
97
+ end
98
+
99
+ context 'custom root key' do
100
+ let(:spawn_serializers!) do
101
+ spawn_serializer('OrganizationSerializer') do
102
+ can_include :artists
103
+ root_key :super_organizations
104
+ end
105
+ spawn_serializer('UserSerializer') do
106
+ can_include :awesome_project
107
+ root_key :creators
108
+ end
109
+ spawn_serializer('ProjectSerializer') do
110
+ can_include :creator
111
+ root_key :awesome_projects
112
+ end
113
+ end
114
+ context 'has_one' do
115
+ let(:objects) { User.all }
116
+ let(:expected_project) do
117
+ {
118
+ href: '/creators/1/awesome_project',
119
+ type: 'awesome_projects'
120
+ }
121
+ end
122
+
123
+ it { expect(serialized[:creators][0][:links][:awesome_project]).to eq(expected_project) }
124
+ end
125
+
126
+ context 'has_many' do
127
+ let(:objects) { Organization.all }
128
+ let(:expected_artists) do
129
+ {
130
+ href: '/creators?organization_id=1',
131
+ type: 'creators'
132
+ }
133
+ end
134
+
135
+ it { expect(serialized[:super_organizations][0][:links][:artists]).to eq(expected_artists) }
136
+ end
137
+
138
+ context 'belongs_to' do
139
+ let(:objects) { Project.all }
140
+ let(:expected_creator) do
141
+ {
142
+ href: '/creators/1',
143
+ id: '1',
144
+ type: 'creators'
145
+ }
146
+ end
147
+
148
+ it { expect(serialized[:awesome_projects][0][:links][:creator]).to eq(expected_creator) }
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Serializer do
4
+ let(:serializer) { Encore::Serializer::Instance }
5
+ let(:objects) { User.all }
6
+ let(:serialized) { serializer.new(objects, include: include).as_json }
7
+
8
+ before do
9
+ run_migrations!
10
+ spawn_objects!
11
+ create_records!
12
+ end
13
+
14
+ let(:run_migrations!) do
15
+ run_migration do
16
+ create_table(:users, force: true) do |t|
17
+ t.string :name, default: nil
18
+ t.integer :project_id
19
+ t.integer :event_result_id
20
+ end
21
+ create_table(:projects, force: true) do |t|
22
+ t.string :name, default: nil
23
+ t.integer :user_id
24
+ end
25
+ create_table(:event_results, force: true) do |t|
26
+ t.string :score, default: nil
27
+ t.integer :user_id
28
+ end
29
+ end
30
+ end
31
+
32
+ let(:spawn_objects!) do
33
+ spawn_model('User') do
34
+ has_many :projects
35
+ end
36
+ spawn_serializer('UserSerializer') do
37
+ attributes :name, :links
38
+
39
+ can_include :projects
40
+ end
41
+ spawn_model('Project') do
42
+ belongs_to :user
43
+ end
44
+ spawn_serializer('ProjectSerializer') do
45
+ attributes :name
46
+ end
47
+ end
48
+
49
+ let(:create_records!) do
50
+ User.create name: 'Allan'
51
+ User.create name: 'Doe'
52
+ Project.create name: 'p1', user_id: 1
53
+ Project.create name: 'p2', user_id: 1
54
+ end
55
+
56
+ context 'not included' do
57
+ let(:include) { '' }
58
+ let(:expected_project) do
59
+ {
60
+ href: '/projects?user_id=1',
61
+ type: 'projects'
62
+ }
63
+ end
64
+
65
+ it { expect(serialized[:users][0][:links][:projects]).to eq(expected_project) }
66
+ end
67
+
68
+ context 'included' do
69
+ let(:include) { 'projects' }
70
+ let(:expected_project) { %w(1 2) }
71
+
72
+ it { expect(serialized[:users][0][:links][:projects]).to eq(expected_project) }
73
+ end
74
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Serializer do
4
+ let(:serializer) { Encore::Serializer::Instance }
5
+ let(:objects) { User.all }
6
+ let(:serialized) { serializer.new(objects, include: include).as_json }
7
+
8
+ before do
9
+ run_migrations!
10
+ spawn_objects!
11
+ create_records!
12
+ end
13
+
14
+ let(:run_migrations!) do
15
+ run_migration do
16
+ create_table(:users, force: true) do |t|
17
+ t.string :name, default: nil
18
+ t.integer :project_id
19
+ t.integer :event_result_id
20
+ end
21
+ create_table(:projects, force: true) do |t|
22
+ t.string :name, default: nil
23
+ t.integer :user_id
24
+ end
25
+ create_table(:event_results, force: true) do |t|
26
+ t.string :score, default: nil
27
+ t.integer :user_id
28
+ end
29
+ end
30
+ end
31
+
32
+ let(:spawn_objects!) do
33
+ spawn_model('User') do
34
+ has_one :project
35
+ end
36
+ spawn_serializer('UserSerializer') do
37
+ attributes :name, :links
38
+
39
+ can_include :project
40
+ end
41
+ spawn_model('Project')
42
+ spawn_serializer('ProjectSerializer') do
43
+ attributes :name
44
+ end
45
+ end
46
+
47
+ let(:create_records!) do
48
+ User.create name: 'Allan'
49
+ User.create name: 'Doe'
50
+ Project.create name: 'p1', user_id: 1
51
+ Project.create name: 'p2', user_id: 2
52
+ end
53
+
54
+ context 'not included' do
55
+ let(:include) { '' }
56
+ let(:expected_project) do
57
+ {
58
+ href: '/users/1/project',
59
+ type: 'projects'
60
+ }
61
+ end
62
+
63
+ it { expect(serialized[:users][0][:links][:project]).to eq(expected_project) }
64
+ end
65
+
66
+ context 'included' do
67
+ let(:include) { 'project' }
68
+ let(:expected_project) { '1' }
69
+
70
+ it { expect(serialized[:users][0][:links][:project]).to eq(expected_project) }
71
+ end
72
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Serializer do
4
+ let(:serializer) { Encore::Serializer::Instance }
5
+ let(:objects) { User.all }
6
+ let(:serialized) { serializer.new(objects, include: include).as_json }
7
+
8
+ before do
9
+ run_migrations!
10
+ spawn_objects!
11
+ create_records!
12
+ end
13
+
14
+ let(:run_migrations!) do
15
+ run_migration do
16
+ create_table(:users, force: true) do |t|
17
+ t.string :name, default: nil
18
+ t.integer :project_id
19
+ t.integer :event_result_id
20
+ end
21
+ create_table(:projects, force: true) do |t|
22
+ t.string :name, default: nil
23
+ t.integer :user_id
24
+ end
25
+ create_table(:event_results, force: true) do |t|
26
+ t.string :score, default: nil
27
+ t.integer :user_id
28
+ end
29
+ end
30
+ end
31
+
32
+ context 'underscore route name' do
33
+ let(:spawn_objects!) do
34
+ spawn_model('User') do
35
+ has_many :event_results
36
+ end
37
+ spawn_serializer('UserSerializer') do
38
+ attributes :name, :links
39
+
40
+ def can_include
41
+ %i(event_results)
42
+ end
43
+ end
44
+ spawn_model('EventResult') do
45
+ belongs_to :user
46
+ end
47
+ spawn_serializer('EventResultSerializer') do
48
+ attributes :score
49
+ end
50
+ end
51
+
52
+ let(:create_records!) do
53
+ User.create name: 'Allan'
54
+ User.create name: 'Doe'
55
+ EventResult.create score: 'p1', user_id: 1
56
+ EventResult.create score: 'p2', user_id: 1
57
+ end
58
+
59
+ let(:include) { '' }
60
+ let(:expected_event_results) do
61
+ {
62
+ href: '/event_results?user_id=1',
63
+ type: 'event_results'
64
+ }
65
+ end
66
+
67
+ it { expect(serialized[:users][0][:links][:event_results]).to eq(expected_event_results) }
68
+ end
69
+
70
+ context 'underscore id' do
71
+ let(:spawn_objects!) do
72
+ spawn_model('User')
73
+ spawn_serializer('UserSerializer') do
74
+ attributes :name, :links
75
+ end
76
+ spawn_model('EventResult') do
77
+ has_many :users
78
+ end
79
+ spawn_serializer('EventResultSerializer') do
80
+ attributes :score
81
+
82
+ def can_include
83
+ %i(users)
84
+ end
85
+ end
86
+ end
87
+
88
+ let(:create_records!) do
89
+ EventResult.create score: 'p1'
90
+ EventResult.create score: 'p2'
91
+ User.create name: 'Allan', event_result_id: 1
92
+ User.create name: 'Doe', event_result_id: 1
93
+ end
94
+
95
+ let(:objects) { EventResult.all }
96
+ let(:include) { '' }
97
+ let(:expected_users) do
98
+ {
99
+ href: '/users?event_result_id=1',
100
+ type: 'users'
101
+ }
102
+ end
103
+
104
+ it { expect(serialized[:event_results][0][:links][:users]).to eq(expected_users) }
105
+ end
106
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-09 00:00:00.000000000 Z
11
+ date: 2014-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: kaminari
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 0.15.1
61
+ version: '0.15'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 0.15.1
68
+ version: '0.15'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -98,16 +98,16 @@ dependencies:
98
98
  name: rspec
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '='
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 3.0.0beta2
103
+ version: 3.0.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
- version: 3.0.0beta2
110
+ version: 3.0.0
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: sqlite3
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -174,7 +174,8 @@ files:
174
174
  - Rakefile
175
175
  - encore.gemspec
176
176
  - gemfiles/Gemfile.activerecord-4.0
177
- - gemfiles/Gemfile.activerecord-4.1
177
+ - gemfiles/Gemfile.activerecord-4.1.3
178
+ - gemfiles/Gemfile.activerecord-4.1.4
178
179
  - lib/encore.rb
179
180
  - lib/encore/config.rb
180
181
  - lib/encore/persister/errors_parser.rb
@@ -209,7 +210,12 @@ files:
209
210
  - spec/encore/serializer/linked/linked_resources_spec.rb
210
211
  - spec/encore/serializer/linked/links_includes/links_includes_has_many_spec.rb
211
212
  - spec/encore/serializer/linked/links_includes/links_includes_has_one_spec.rb
212
- - spec/encore/serializer/links_resource_spec.rb
213
+ - spec/encore/serializer/links_resources/belongs_to_spec.rb
214
+ - spec/encore/serializer/links_resources/can_access_spec.rb
215
+ - spec/encore/serializer/links_resources/custom_association_names_spec.rb
216
+ - spec/encore/serializer/links_resources/has_many_spec.rb
217
+ - spec/encore/serializer/links_resources/has_one_spec.rb
218
+ - spec/encore/serializer/links_resources/underscore_names_spec.rb
213
219
  - spec/encore/serializer/main_meta_spec.rb
214
220
  - spec/encore/serializer/main_resource_spec.rb
215
221
  - spec/encore/serializer/no_paging_spec.rb
@@ -1,276 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Encore::Serializer do
4
- let(:serializer) { Encore::Serializer::Instance }
5
- let(:objects) { User.all }
6
- let(:serialized) { serializer.new(objects, include: include).as_json }
7
-
8
- let(:run_migrations!) do
9
- run_migration do
10
- create_table(:users, force: true) do |t|
11
- t.string :name, default: nil
12
- t.integer :project_id
13
- end
14
- create_table(:projects, force: true) do |t|
15
- t.string :name, default: nil
16
- t.integer :user_id
17
- end
18
- end
19
- end
20
-
21
- before do
22
- run_migrations!
23
- spawn_objects!
24
- create_records!
25
- end
26
-
27
- context 'can access' do
28
- context 'default' do
29
- let(:spawn_objects!) do
30
- spawn_model('User') do
31
- belongs_to :project
32
- belongs_to :irrelevant_reflection
33
- end
34
- spawn_serializer('UserSerializer') do
35
- attributes :name, :links
36
-
37
- can_include :project
38
- end
39
- spawn_model('Project') do
40
- has_many :users
41
- end
42
- spawn_serializer('ProjectSerializer') do
43
- attributes :name
44
- end
45
-
46
- end
47
- let(:create_records!) do
48
- User.create name: 'Allan', project_id: 1
49
- User.create name: 'Doe', project_id: 2
50
- end
51
- let(:include) { '' }
52
-
53
- it { expect(serialized[:users][0][:links][:irrelevant_reflection]).to eq(nil) }
54
- end
55
-
56
- context 'not loaded with access' do
57
- let(:spawn_objects!) do
58
- spawn_model('User') do
59
- belongs_to :project
60
- belongs_to :irrelevant_reflection
61
- end
62
- spawn_serializer('UserSerializer') do
63
- attributes :name, :links
64
-
65
- can_include :project
66
- end
67
- spawn_model('Project') do
68
- has_many :users
69
- end
70
- spawn_serializer('ProjectSerializer') do
71
- attributes :name
72
- end
73
-
74
- end
75
- let(:create_records!) do
76
- User.create name: 'Allan', project_id: 1
77
- User.create name: 'Doe', project_id: 2
78
- end
79
- let(:include) { '' }
80
-
81
- it { expect(serialized[:users][0][:links][:project].present?).to eq(true) }
82
- end
83
-
84
- context 'not loaded without access' do
85
- let(:spawn_objects!) do
86
- spawn_model('User') do
87
- belongs_to :project
88
- belongs_to :irrelevant_reflection
89
- end
90
- spawn_serializer('UserSerializer') do
91
- attributes :name, :links
92
-
93
- can_include :project
94
-
95
- can_access []
96
- end
97
- spawn_model('Project') do
98
- has_many :users
99
- end
100
- spawn_serializer('ProjectSerializer') do
101
- attributes :name
102
- end
103
-
104
- end
105
- let(:create_records!) do
106
- User.create name: 'Allan', project_id: 1
107
- User.create name: 'Doe', project_id: 2
108
- end
109
- let(:include) { '' }
110
-
111
- it { expect(serialized[:users][0][:links][:project].present?).to eq(false) }
112
- end
113
-
114
- context 'loaded without access' do
115
- let(:spawn_objects!) do
116
- spawn_model('User') do
117
- belongs_to :project
118
- belongs_to :irrelevant_reflection
119
- end
120
- spawn_serializer('UserSerializer') do
121
- attributes :name, :links
122
-
123
- can_include :project
124
-
125
- can_access []
126
- end
127
- spawn_model('Project') do
128
- has_many :users
129
- end
130
- spawn_serializer('ProjectSerializer') do
131
- attributes :name
132
- end
133
-
134
- end
135
- let(:create_records!) do
136
- User.create name: 'Allan', project_id: 1
137
- User.create name: 'Doe', project_id: 2
138
- end
139
- let(:include) { 'project' }
140
-
141
- it { expect(serialized[:users][0][:links][:project].present?).to eq(true) }
142
- end
143
- end
144
-
145
- context 'belongs_to' do
146
- let(:spawn_objects!) do
147
- spawn_model('User') do
148
- belongs_to :project
149
- end
150
- spawn_serializer('UserSerializer') do
151
- attributes :name, :links
152
-
153
- can_include :project
154
- end
155
- spawn_model('Project') do
156
- has_many :users
157
- end
158
- spawn_serializer('ProjectSerializer') do
159
- attributes :name
160
- end
161
-
162
- end
163
- let(:create_records!) do
164
- User.create name: 'Allan', project_id: 1
165
- User.create name: 'Doe', project_id: 2
166
- end
167
-
168
- context 'not included' do
169
- let(:include) { '' }
170
- let(:expected_project) do
171
- {
172
- href: '/projects/1',
173
- id: '1',
174
- type: 'projects'
175
- }
176
- end
177
-
178
- it { expect(serialized[:users][0][:links][:project]).to eq(expected_project) }
179
- end
180
-
181
- context 'included' do
182
- let(:include) { 'project' }
183
- let(:expected_project) { '1' }
184
-
185
- it { expect(serialized[:users][0][:links][:project]).to eq(expected_project) }
186
- end
187
- end
188
-
189
- context 'has_many' do
190
- let(:spawn_objects!) do
191
- spawn_model('User') do
192
- has_many :projects
193
- end
194
- spawn_serializer('UserSerializer') do
195
- attributes :name, :links
196
-
197
- can_include :projects
198
- end
199
- spawn_model('Project') do
200
- belongs_to :user
201
- end
202
- spawn_serializer('ProjectSerializer') do
203
- attributes :name
204
- end
205
- end
206
-
207
- let(:create_records!) do
208
- User.create name: 'Allan'
209
- User.create name: 'Doe'
210
- Project.create name: 'p1', user_id: 1
211
- Project.create name: 'p2', user_id: 1
212
- end
213
-
214
- context 'not included' do
215
- let(:include) { '' }
216
- let(:expected_project) do
217
- {
218
- href: '/projects?user_id=1',
219
- type: 'projects'
220
- }
221
- end
222
-
223
- it { expect(serialized[:users][0][:links][:projects]).to eq(expected_project) }
224
- end
225
-
226
- context 'included' do
227
- let(:include) { 'projects' }
228
- let(:expected_project) { %w(1 2) }
229
-
230
- it { expect(serialized[:users][0][:links][:projects]).to eq(expected_project) }
231
- end
232
- end
233
-
234
- context 'has_one' do
235
- let(:spawn_objects!) do
236
- spawn_model('User') do
237
- has_one :project
238
- end
239
- spawn_serializer('UserSerializer') do
240
- attributes :name, :links
241
-
242
- can_include :project
243
- end
244
- spawn_model('Project')
245
- spawn_serializer('ProjectSerializer') do
246
- attributes :name
247
- end
248
- end
249
-
250
- let(:create_records!) do
251
- User.create name: 'Allan'
252
- User.create name: 'Doe'
253
- Project.create name: 'p1', user_id: 1
254
- Project.create name: 'p2', user_id: 2
255
- end
256
-
257
- context 'not included' do
258
- let(:include) { '' }
259
- let(:expected_project) do
260
- {
261
- href: '/users/1/project',
262
- type: 'projects'
263
- }
264
- end
265
-
266
- it { expect(serialized[:users][0][:links][:project]).to eq(expected_project) }
267
- end
268
-
269
- context 'included' do
270
- let(:include) { 'project' }
271
- let(:expected_project) { '1' }
272
-
273
- it { expect(serialized[:users][0][:links][:project]).to eq(expected_project) }
274
- end
275
- end
276
- end