encore 0.0.3 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -16
  3. data/.rubocop.yml +53 -0
  4. data/.travis.yml +10 -2
  5. data/LICENSE.md +1 -1
  6. data/README.md +105 -153
  7. data/Rakefile +10 -0
  8. data/encore.gemspec +16 -13
  9. data/gemfiles/{Gemfile.activerecord-3.2.x → Gemfile.activerecord-4.1} +1 -1
  10. data/lib/encore/config.rb +2 -0
  11. data/lib/encore/persister/errors_parser.rb +17 -0
  12. data/lib/encore/persister/instance.rb +85 -0
  13. data/lib/encore/persister/key_mapping.rb +15 -0
  14. data/lib/encore/persister/links_parser.rb +57 -0
  15. data/lib/encore/persister/param_injection.rb +15 -0
  16. data/lib/encore/serializer/base.rb +48 -0
  17. data/lib/encore/serializer/eager_loading_manager.rb +11 -0
  18. data/lib/encore/serializer/instance.rb +56 -0
  19. data/lib/encore/serializer/linked_resource_manager.rb +19 -0
  20. data/lib/encore/serializer/links_reflection_includer.rb +50 -0
  21. data/lib/encore/serializer/main_resource_links_manager/reflection_belongs_to.rb +13 -0
  22. data/lib/encore/serializer/main_resource_links_manager/reflection_has_many.rb +13 -0
  23. data/lib/encore/serializer/main_resource_links_manager/reflection_has_one.rb +13 -0
  24. data/lib/encore/serializer/main_resource_links_manager.rb +45 -0
  25. data/lib/encore/serializer/main_resource_manager.rb +13 -0
  26. data/lib/encore/serializer/meta_manager.rb +29 -0
  27. data/lib/encore/serializer/options_parser.rb +51 -0
  28. data/lib/encore/serializer/utils.rb +12 -0
  29. data/lib/encore/version.rb +1 -1
  30. data/lib/encore.rb +16 -8
  31. data/spec/encore/persister/belongs_to_links_spec.rb +46 -0
  32. data/spec/encore/persister/create_resources_spec.rb +55 -0
  33. data/spec/encore/persister/errors_resources_spec.rb +55 -0
  34. data/spec/encore/persister/has_many_links_spec.rb +49 -0
  35. data/spec/encore/persister/has_one_links_spec.rb +49 -0
  36. data/spec/encore/persister/key_mappings_spec.rb +66 -0
  37. data/spec/encore/persister/param_injection_spec.rb +46 -0
  38. data/spec/encore/persister/update_resources_spec.rb +37 -0
  39. data/spec/encore/serializer/linked/always_include_resources_spec.rb +85 -0
  40. data/spec/encore/serializer/linked/can_include_resources_spec.rb +69 -0
  41. data/spec/encore/serializer/linked/linked_resources_spec.rb +103 -0
  42. data/spec/encore/serializer/links_resource_spec.rb +294 -0
  43. data/spec/encore/serializer/main_meta_spec.rb +66 -0
  44. data/spec/encore/serializer/main_resource_spec.rb +37 -0
  45. data/spec/encore/serializer/no_paging_spec.rb +40 -0
  46. data/spec/encore_spec.rb +4 -0
  47. data/spec/spec_helper.rb +8 -21
  48. data/spec/support/macros/database/database_adapter.rb +9 -0
  49. data/spec/support/macros/database/sqlite3_adapter.rb +14 -0
  50. data/spec/support/macros/database_macros.rb +5 -16
  51. data/spec/support/macros/model_macros.rb +7 -22
  52. metadata +143 -72
  53. data/lib/encore/association/has_many_association.rb +0 -12
  54. data/lib/encore/association/has_one_association.rb +0 -12
  55. data/lib/encore/association.rb +0 -39
  56. data/lib/encore/attribute.rb +0 -42
  57. data/lib/encore/entity/input/associations.rb +0 -27
  58. data/lib/encore/entity/input/errors.rb +0 -8
  59. data/lib/encore/entity/input/exposed_attributes.rb +0 -37
  60. data/lib/encore/entity/input.rb +0 -73
  61. data/lib/encore/entity/output/json.rb +0 -14
  62. data/lib/encore/entity/output.rb +0 -13
  63. data/lib/encore/entity.rb +0 -44
  64. data/lib/encore/helpers/controller_helper.rb +0 -37
  65. data/lib/encore/helpers.rb +0 -1
  66. data/lib/encore/railtie.rb +0 -11
  67. data/spec/encore/entity/input/associations_spec.rb +0 -48
  68. data/spec/encore/entity/input/exposed_attributes_spec.rb +0 -46
  69. data/spec/encore/entity/input_spec.rb +0 -104
  70. data/spec/encore/entity/output/json_spec.rb +0 -76
  71. data/spec/encore/entity/output_spec.rb +0 -5
  72. data/spec/encore/entity_spec.rb +0 -31
  73. data/spec/encore/helpers/controller_helper_spec.rb +0 -59
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Persister do
4
+ let(:persister) { Encore::Persister::Instance }
5
+ let(:persist!) { persister.new(model, params).persist! }
6
+
7
+ before do
8
+ run_migrations!
9
+ spawn_models!
10
+ end
11
+
12
+ let(:run_migrations!) do
13
+ run_migration do
14
+ create_table(:users, force: true) do |t|
15
+ t.string :name, default: nil
16
+ end
17
+ create_table(:groups, force: true) do |t|
18
+ t.string :name, default: nil
19
+ t.integer :user_id, default: nil
20
+ end
21
+ end
22
+ end
23
+
24
+ let(:spawn_models!) do
25
+ spawn_model('User') do
26
+ has_many :groups
27
+ end
28
+ spawn_model('Group') do
29
+ belongs_to :user
30
+ end
31
+ end
32
+
33
+ let(:model) { User }
34
+ let(:group1) { Group.create name: 'a' }
35
+ let(:group2) { Group.create name: 'b' }
36
+
37
+ let(:params) do
38
+ [{
39
+ name: 'Allan',
40
+ links: {
41
+ groups: [group1.id.to_s, group2.id.to_s]
42
+ }
43
+ }]
44
+ end
45
+
46
+ it { expect { persist! }.to change { model.count }.by(1) }
47
+ it { expect { persist! }.to change { model.first.try(:name) }.to('Allan') }
48
+ it { expect { persist! }.to change { model.first.try(:groups).try(:count) }.to(2) }
49
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Persister do
4
+ let(:persister) { Encore::Persister::Instance }
5
+ let(:persist!) { persister.new(model, params).persist! }
6
+
7
+ before do
8
+ run_migrations!
9
+ spawn_models!
10
+ end
11
+
12
+ let(:run_migrations!) do
13
+ run_migration do
14
+ create_table(:users, force: true) do |t|
15
+ t.string :name, default: nil
16
+ end
17
+ create_table(:groups, force: true) do |t|
18
+ t.string :name, default: nil
19
+ t.integer :user_id, default: nil
20
+ end
21
+ end
22
+ end
23
+
24
+ let(:spawn_models!) do
25
+ spawn_model('User') do
26
+ has_one :group
27
+ end
28
+ spawn_model('Group') do
29
+ belongs_to :user
30
+ end
31
+ end
32
+
33
+ let(:model) { User }
34
+ let(:group1) { Group.create name: 'a' }
35
+
36
+ let(:params) do
37
+ [{
38
+ name: 'Allan',
39
+ links: {
40
+ group: group1.id.to_s
41
+ }
42
+ }]
43
+ end
44
+
45
+ it { expect { persist! }.to change { model.count }.by(1) }
46
+ it { expect { persist! }.to change { model.first.try(:name) }.to('Allan') }
47
+ it { expect { persist! }.to change { model.first.try(:group).try(:id) }.to(group1.id) }
48
+ it { expect { persist! }.to change { model.first.try(:group).try(:user).try(:name) }.to('Allan') }
49
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Persister do
4
+ let(:persister) { Encore::Persister::Instance }
5
+ let(:persist!) { persister.new(model, params).persist! }
6
+
7
+ let(:run_migrations!) do
8
+ run_migration do
9
+ create_table(:users, force: true) do |t|
10
+ t.string :name, default: nil
11
+ t.string :phone, default: nil
12
+ end
13
+ end
14
+ end
15
+
16
+ let(:spawn_objects!) do
17
+ spawn_model('User')
18
+ spawn_serializer('UserSerializer') do
19
+ attributes :snake_case_key
20
+
21
+ def snake_case_key
22
+ :name
23
+ end
24
+
25
+ def key_mappings
26
+ { snake_case_key: :name }
27
+ end
28
+ end
29
+ end
30
+
31
+ let(:model) { User }
32
+
33
+ before do
34
+ run_migrations!
35
+ spawn_objects!
36
+ end
37
+
38
+ context 'single create' do
39
+ let(:params) do
40
+ [{
41
+ snake_case_key: 'Allan',
42
+ phone: '555-2525'
43
+ }]
44
+ end
45
+
46
+ it { expect { persist! }.to change { model.count }.by(1) }
47
+ it { expect { persist! }.to change { model.first.try(:name) }.to('Allan') }
48
+ it { expect { persist! }.to change { model.first.try(:phone) }.to('555-2525') }
49
+ end
50
+
51
+ context 'many create' do
52
+ let(:params) do
53
+ [{
54
+ snake_case_key: 'Allan',
55
+ phone: '555-2525'
56
+ }, {
57
+ snake_case_key: 'Bob'
58
+ }]
59
+ end
60
+
61
+ it { expect { persist! }.to change { model.count }.by(2) }
62
+ it { expect { persist! }.to change { model.first.try(:name) }.to('Allan') }
63
+ it { expect { persist! }.to change { model.first.try(:phone) }.to('555-2525') }
64
+ it { expect { persist! }.to change { model.last.try(:name) }.to('Bob') }
65
+ end
66
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Persister do
4
+ let(:persister) { Encore::Persister::Instance }
5
+ let(:persist!) { persister.new(model, params, options).persist! }
6
+
7
+ let(:run_migrations!) do
8
+ run_migration do
9
+ create_table(:users, force: true) do |t|
10
+ t.string :name, default: nil
11
+ t.integer :creator_id
12
+ end
13
+ end
14
+ end
15
+
16
+ let(:spawn_objects!) do
17
+ spawn_model('User')
18
+ spawn_serializer('UserSerializer')
19
+ end
20
+
21
+ let(:model) { User }
22
+
23
+ before do
24
+ run_migrations!
25
+ spawn_objects!
26
+ end
27
+
28
+ context 'single inject' do
29
+ let(:options) do
30
+ {
31
+ inject_params: {
32
+ creator_id: '1'
33
+ }
34
+ }
35
+ end
36
+ let(:params) do
37
+ [{
38
+ name: 'Allan'
39
+ }]
40
+ end
41
+
42
+ it { expect { persist! }.to change { model.count }.by(1) }
43
+ it { expect { persist! }.to change { model.first.try(:name) }.to('Allan') }
44
+ it { expect { persist! }.to change { model.first.try(:creator_id) }.to(1) }
45
+ end
46
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Persister do
4
+ let(:persister) { Encore::Persister::Instance }
5
+ let(:persist!) { persister.new(model, params).persist! }
6
+
7
+ let(:run_migrations!) do
8
+ run_migration do
9
+ create_table(:users, force: true) do |t|
10
+ t.string :name, default: nil
11
+ t.string :phone, default: nil
12
+ end
13
+ end
14
+ end
15
+
16
+ let(:spawn_models!) do
17
+ spawn_model('User')
18
+ end
19
+
20
+ let(:model) { User.create name: 'Bob' }
21
+ let(:params) do
22
+ [{
23
+ id: model.id,
24
+ name: 'Allan',
25
+ phone: '555-2525'
26
+ }]
27
+ end
28
+
29
+ before do
30
+ run_migrations!
31
+ spawn_models!
32
+ end
33
+
34
+ it { expect { persist! }.to_not change { model.class.count } }
35
+ it { expect { persist! }.to change { model.class.first.name }.to('Allan') }
36
+ it { expect { persist! }.to change { model.class.first.phone }.to('555-2525') }
37
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Serializer do
4
+ let(:serializer) { Encore::Serializer::Instance }
5
+ let(:serialized) { serializer.new(objects, page: page, include: include).as_json }
6
+
7
+ let(:run_migrations!) do
8
+ run_migration do
9
+ create_table(:users, force: true) do |t|
10
+ t.string :name, default: nil
11
+ t.string :discarded_attribute, default: nil
12
+ t.integer :project_id
13
+ end
14
+
15
+ create_table(:projects, force: true) do |t|
16
+ t.string :name, default: nil
17
+ t.string :discarded_attribute, default: nil
18
+ end
19
+ end
20
+ end
21
+
22
+ let(:project1) { Project.create name: 'Awesome project' }
23
+ let(:project2) { Project.create name: 'Medium awesome project' }
24
+ let(:page) { 1 }
25
+ let(:spawn_objects!) do
26
+ spawn_model('User') { belongs_to :project }
27
+ spawn_model('Project')
28
+ end
29
+ let(:create_records!) do
30
+ User.create name: 'Allan', project_id: project1.id
31
+ User.create name: 'Doe', project_id: project2.id
32
+ User.create name: 'Ding', project_id: project1.id
33
+ User.create name: 'Bob', project_id: project2.id
34
+ end
35
+ let(:objects) { User.all }
36
+
37
+ before do
38
+ run_migrations!
39
+ spawn_objects!
40
+ spawn_serializer!
41
+ create_records!
42
+ end
43
+
44
+ context 'without' do
45
+ let(:include) { 'project' }
46
+ let(:spawn_serializer!) do
47
+ spawn_serializer('ProjectSerializer') { attributes :name }
48
+ spawn_serializer('UserSerializer') { attributes :name }
49
+ end
50
+
51
+ it { expect(serialized[:linked][:projects]).to eq(nil) }
52
+ end
53
+
54
+ context 'with and include' do
55
+ let(:include) { 'project' }
56
+ let(:spawn_serializer!) do
57
+ spawn_serializer('ProjectSerializer') { attributes :name }
58
+ spawn_serializer('UserSerializer') do
59
+ attributes :name
60
+
61
+ def always_include
62
+ %i(project)
63
+ end
64
+ end
65
+ end
66
+
67
+ it { expect(serialized[:linked][:projects]).to eq([{ name: project1.name }, { name: project2.name }]) }
68
+ end
69
+
70
+ context 'with and no include' do
71
+ let(:include) { '' }
72
+ let(:spawn_serializer!) do
73
+ spawn_serializer('ProjectSerializer') { attributes :name }
74
+ spawn_serializer('UserSerializer') do
75
+ attributes :name
76
+
77
+ def always_include
78
+ %i(project)
79
+ end
80
+ end
81
+ end
82
+
83
+ it { expect(serialized[:linked][:projects]).to eq([{ name: project1.name }, { name: project2.name }]) }
84
+ end
85
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Serializer do
4
+ let(:serializer) { Encore::Serializer::Instance }
5
+ let(:serialized) { serializer.new(objects, page: page, include: include).as_json }
6
+
7
+ let(:run_migrations!) do
8
+ run_migration do
9
+ create_table(:users, force: true) do |t|
10
+ t.string :name, default: nil
11
+ t.string :discarded_attribute, default: nil
12
+ t.integer :project_id
13
+ end
14
+
15
+ create_table(:projects, force: true) do |t|
16
+ t.string :name, default: nil
17
+ t.string :discarded_attribute, default: nil
18
+ end
19
+ end
20
+ end
21
+
22
+ let(:project1) { Project.create name: 'Awesome project' }
23
+ let(:project2) { Project.create name: 'Medium awesome project' }
24
+ let(:page) { 1 }
25
+ let(:spawn_objects!) do
26
+ spawn_model('User') { belongs_to :project }
27
+ spawn_model('Project')
28
+ end
29
+ let(:create_records!) do
30
+ User.create name: 'Allan', project_id: project1.id
31
+ User.create name: 'Doe', project_id: project2.id
32
+ User.create name: 'Ding', project_id: project1.id
33
+ User.create name: 'Bob', project_id: project2.id
34
+ end
35
+ let(:objects) { User.all }
36
+
37
+ before do
38
+ run_migrations!
39
+ spawn_objects!
40
+ spawn_serializer!
41
+ create_records!
42
+ end
43
+
44
+ context 'without' do
45
+ let(:include) { 'project' }
46
+ let(:spawn_serializer!) do
47
+ spawn_serializer('ProjectSerializer') { attributes :name }
48
+ spawn_serializer('UserSerializer') { attributes :name }
49
+ end
50
+
51
+ it { expect(serialized[:linked][:projects]).to eq(nil) }
52
+ end
53
+
54
+ context 'with' do
55
+ let(:include) { 'project' }
56
+ let(:spawn_serializer!) do
57
+ spawn_serializer('ProjectSerializer') { attributes :name }
58
+ spawn_serializer('UserSerializer') do
59
+ attributes :name
60
+
61
+ def can_include
62
+ %i(project)
63
+ end
64
+ end
65
+ end
66
+
67
+ it { expect(serialized[:linked][:projects]).to eq([{ name: project1.name }, { name: project2.name }]) }
68
+ end
69
+ end
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encore::Serializer do
4
+ let(:serializer) { Encore::Serializer::Instance }
5
+ let(:serialized) { serializer.new(objects, page: page, include: include).as_json }
6
+
7
+ let(:run_migrations!) do
8
+ run_migration do
9
+ create_table(:users, force: true) do |t|
10
+ t.string :name, default: nil
11
+ t.string :discarded_attribute, default: nil
12
+ t.integer :project_id
13
+ end
14
+
15
+ create_table(:projects, force: true) do |t|
16
+ t.string :name, default: nil
17
+ t.string :discarded_attribute, default: nil
18
+ end
19
+ end
20
+ end
21
+
22
+ let(:project1) { Project.create name: 'Awesome project' }
23
+ let(:project2) { Project.create name: 'Medium awesome project' }
24
+ let(:page) { 1 }
25
+ let(:spawn_serializer!) do
26
+ spawn_serializer('ProjectSerializer') do
27
+ attributes :name
28
+
29
+ def can_include
30
+ %i(user users)
31
+ end
32
+ end
33
+ spawn_serializer('UserSerializer') do
34
+ attributes :name
35
+
36
+ def can_include
37
+ %i(project)
38
+ end
39
+ end
40
+ end
41
+
42
+ before do
43
+ run_migrations!
44
+ spawn_objects!
45
+ spawn_serializer!
46
+ create_records!
47
+ end
48
+
49
+ context 'belongs_to include' do
50
+ let(:objects) { User.all }
51
+ let(:spawn_objects!) do
52
+ spawn_model('User') { belongs_to :project }
53
+ spawn_model('Project')
54
+ end
55
+
56
+ let(:create_records!) do
57
+ User.create name: 'Allan', project_id: project1.id
58
+ User.create name: 'Doe', project_id: project2.id
59
+ User.create name: 'Ding', project_id: project1.id
60
+ User.create name: 'Bob', project_id: project2.id
61
+ end
62
+
63
+ let(:include) { 'project' }
64
+
65
+ it { expect(serialized[:linked][:projects]).to eq([{ name: project1.name }, { name: project2.name }]) }
66
+ end
67
+
68
+ context 'has_many include' do
69
+ let(:objects) { Project.all }
70
+ let(:spawn_objects!) do
71
+ spawn_model('User')
72
+ spawn_model('Project') { has_many :users }
73
+ end
74
+
75
+ let(:create_records!) do
76
+ User.create name: 'Allan', project_id: project1.id
77
+ User.create name: 'Doe', project_id: project2.id
78
+ User.create name: 'Ding', project_id: project1.id
79
+ User.create name: 'Bob', project_id: project2.id
80
+ end
81
+
82
+ let(:include) { 'users' }
83
+
84
+ it { expect(serialized[:linked][:users]).to eq([{ name: 'Allan' }, { name: 'Doe' }, { name: 'Ding' }, { name: 'Bob' }]) }
85
+ end
86
+
87
+ context 'has_one include' do
88
+ let(:objects) { Project.all }
89
+ let(:spawn_objects!) do
90
+ spawn_model('User')
91
+ spawn_model('Project') { has_one :user }
92
+ end
93
+
94
+ let(:create_records!) do
95
+ User.create name: 'Allan', project_id: project1.id
96
+ User.create name: 'Doe', project_id: project2.id
97
+ end
98
+
99
+ let(:include) { 'user' }
100
+
101
+ it { expect(serialized[:linked][:users]).to eq([{ name: 'Allan' }, { name: 'Doe' }]) }
102
+ end
103
+ end