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
@@ -1,11 +0,0 @@
1
- require 'encore'
2
- require 'rails'
3
- require 'action_controller'
4
-
5
- module Encore
6
- class Railtie < Rails::Railtie
7
- initializer 'encore.action_controller' do |app|
8
- ActiveSupport.on_load :action_controller, {}, &Encore::Helpers::ControllerHelper.inject_into_action_controller
9
- end
10
- end
11
- end
@@ -1,48 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Encore::Entity::Input::Associations do
4
- describe :ClassMethods do
5
- describe :has_one do
6
- before do
7
- spawn_model(:User) { belongs_to :organization }
8
- spawn_model(:Organization) { has_many :users }
9
-
10
- spawn_entity :UserEntity do
11
- expose :name
12
- expose_one :organization
13
- end
14
-
15
- run_migration do
16
- create_table(:users, force: true) do |t|
17
- t.string :name
18
- t.integer :organization_id
19
- t.timestamps
20
- end
21
-
22
- create_table(:organizations, force: true)
23
- end
24
- end
25
-
26
- let(:organization) { Organization.create }
27
- let(:other_organization) { Organization.create }
28
- let(:user) { organization.users.create(name: 'Art Vandelay') }
29
- let(:entity) { UserEntity.new(user) }
30
-
31
- context 'when assigning attributes' do
32
- before { entity.assign_attributes organization: other_organization.id }
33
- it { expect(entity.object.organization_id).to eql organization.id }
34
- it { expect(entity.encore_tmp_organization_id).to eql other_organization.id }
35
- end
36
-
37
- context 'when saving the entity' do
38
- before do
39
- entity.assign_attributes organization: other_organization.id
40
- entity.save
41
- end
42
-
43
- it { expect(entity.object.organization_id).to eql other_organization.id }
44
- it { expect(entity.encore_tmp_organization_id).to be_nil }
45
- end
46
- end
47
- end
48
- end
@@ -1,46 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Encore::Entity::Input::ExposedAttributes do
4
- shared_examples 'exposability' do
5
- before do
6
- spawn_object_class!
7
- end
8
-
9
- context 'with attribute name' do
10
- it { expect{ entity.assign_attributes(name: 'Art Vandelay') }.to raise_error(Encore::Entity::Input::InvalidAttributeError) }
11
- end
12
-
13
- context 'with column name' do
14
- before { entity.assign_attributes fullname: 'Art Vandelay' }
15
- it { expect(entity.object.name).to eql 'Art Vandelay' }
16
- end
17
- end
18
-
19
- let(:entity) { UserEntity.new }
20
-
21
- before do
22
- spawn_entity :UserEntity do
23
- expose :name, as: :fullname
24
- end
25
- end
26
-
27
- context 'with ActiveRecord model' do
28
- let(:spawn_object_class!) do
29
- spawn_model :User
30
-
31
- run_migration do
32
- create_table(:users, force: true) do |t|
33
- t.string :name
34
- t.timestamps
35
- end
36
- end
37
- end
38
-
39
- it_has_behavior 'exposability'
40
- end
41
-
42
- context 'with OpenStruct structure' do
43
- let(:spawn_object_class!) { spawn_struct :User }
44
- it_has_behavior 'exposability'
45
- end
46
- end
@@ -1,104 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Encore::Entity::Input do
4
- describe :InstanceMethods do
5
- describe :assign_attributes do
6
- before do
7
- spawn_model :User
8
-
9
- spawn_entity :UserEntity do
10
- expose :name
11
- expose :email
12
- expose :created_at, readonly: true
13
- expose :updated_at, readonly: true
14
- end
15
-
16
- run_migration do
17
- create_table(:users, force: true) do |t|
18
- t.string :email
19
- t.string :name
20
- t.timestamps
21
- end
22
- end
23
- end
24
-
25
- let(:assign_attributes!) { entity.assign_attributes attributes, context: context }
26
-
27
- context 'with :create context' do
28
- let(:entity) { UserEntity.new }
29
- let(:attributes) { { email: 'art@vandelay.com' } }
30
- let(:context) { :create }
31
-
32
- before { assign_attributes! }
33
-
34
- it { expect(entity.object.email).to eql 'art@vandelay.com' }
35
- it { expect(entity.object.name).to be_blank }
36
- it { expect(entity.object.created_at).to be_blank }
37
- it { expect(entity.object.updated_at).to be_blank }
38
- end
39
-
40
- context 'with :update context' do
41
- let(:entity) { UserEntity.new(User.first) }
42
- let(:context) { :update }
43
- let(:create_user!) { User.create(email: 'art@vandelay.com', name: 'Art Vandelay') }
44
-
45
- before { create_user! }
46
-
47
- context 'when updating valid attributes' do
48
- let(:attributes) { { email: 'art+test@vandelay.com' } }
49
- before { assign_attributes! }
50
-
51
- it { expect(entity.object.email).to eql 'art+test@vandelay.com' }
52
- it { expect(entity.object.name).to be_blank }
53
- it { expect(entity.object.created_at).to_not be_blank }
54
- it { expect(entity.object.updated_at).to_not be_blank }
55
- end
56
-
57
- context 'when updating a protected attribute' do
58
- let(:attributes) { { email: 'art+test@vandelay.com', created_at: '2013-08-29 21:22:44' } }
59
-
60
- it { expect { assign_attributes! }.to raise_error(Encore::Entity::Input::InvalidAttributeError) }
61
- end
62
- end
63
-
64
- context 'with :partial_update context' do
65
- let(:entity) { UserEntity.new(User.first) }
66
- let(:attributes) { { email: 'art+test@vandelay.com' } }
67
- let(:context) { :partial_update }
68
- let(:create_user!) { User.create(email: 'art@vandelay.com', name: 'Art Vandelay') }
69
-
70
- before do
71
- create_user!
72
- assign_attributes!
73
- end
74
-
75
- it { expect(entity.object.email).to eql 'art+test@vandelay.com' }
76
- it { expect(entity.object.name).to eql 'Art Vandelay' }
77
- it { expect(entity.object.created_at).to_not be_blank }
78
- it { expect(entity.object.updated_at).to_not be_blank }
79
- end
80
-
81
- context 'with overriden setter method' do
82
- before do
83
- class UserEntity
84
- def name=(name)
85
- object.name = name.upcase
86
- end
87
- end
88
- end
89
-
90
- let(:entity) { UserEntity.new(User.first) }
91
- let(:attributes) { { name: 'Art Vandelay' } }
92
- let(:context) { :partial_update }
93
- let(:create_user!) { User.create }
94
-
95
- before do
96
- create_user!
97
- assign_attributes!
98
- end
99
-
100
- it { expect(entity.object.name).to eql 'ART VANDELAY' }
101
- end
102
- end
103
- end
104
- end
@@ -1,76 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Encore::Entity::Output::JSON do
4
- describe :InstanceMethods do
5
- describe :as_json do
6
- context 'for simple entity' do
7
- before do
8
- # Create a model
9
- spawn_model :User
10
-
11
- # Create an entity
12
- spawn_entity :UserEntity do
13
- expose :name
14
- end
15
-
16
- run_migration do
17
- create_table(:users, force: true) do |t|
18
- t.string :name
19
- end
20
- end
21
- end
22
-
23
- let(:user) { User.create(name: 'Art Vandelay') }
24
- let(:entity) { UserEntity.new(user) }
25
- let(:json) { entity.as_json }
26
-
27
- it { expect(json.keys).to include :id, :name }
28
- it { expect(json[:id]).to eql user.id }
29
- it { expect(json[:name]).to eql 'Art Vandelay' }
30
- end
31
-
32
- context 'for entity with associations' do
33
- before do
34
- # Create a model
35
- spawn_model :User do
36
- has_many :comments
37
- end
38
-
39
- # Create a model
40
- spawn_model :Comment
41
-
42
- # Create an entity
43
- spawn_entity :UserEntity do
44
- expose :name
45
- expose_many :comments
46
- end
47
-
48
- run_migration do
49
- create_table(:users, force: true) do |t|
50
- t.string :name
51
- end
52
-
53
- create_table(:comments, force: true) do |t|
54
- t.text :body
55
- t.integer :user_id
56
- end
57
- end
58
- end
59
-
60
- let(:user) do
61
- User.create(name: 'Art Vandelay').tap do |user|
62
- user.comments << Comment.create(body: 'Test 1')
63
- user.comments << Comment.create(body: 'Test 2')
64
- end
65
- end
66
-
67
- let(:entity) { UserEntity.new(user) }
68
- let(:json) { entity.as_json }
69
-
70
- it { expect(json.keys).to include :id, :name, :comments }
71
- it { expect(json[:comments]).to be_instance_of Array }
72
- it { expect(json[:comments]).to include *Comment.pluck(:id) }
73
- end
74
- end
75
- end
76
- end
@@ -1,5 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Encore::Entity::Output do
4
- # Nothing to test here
5
- end
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Encore::Entity do
4
- describe :InstanceMethods do
5
- describe :method_missing do
6
- before do
7
- spawn_model :User
8
- spawn_entity(:UserEntity) { expose :name }
9
-
10
- run_migration do
11
- create_table(:users, force: true) do |t|
12
- t.string :name
13
- end
14
- end
15
- end
16
-
17
- let(:user) { User.create }
18
- let(:entity) { UserEntity.new(user) }
19
-
20
- context 'for delegated method' do
21
- before { expect(user).to receive(:save) }
22
- specify { entity.save }
23
- end
24
-
25
- context 'for setter method' do
26
- before { expect(user).to receive(:name=).with('Art Vandelay') }
27
- specify { entity.name = "Art Vandelay" }
28
- end
29
- end
30
- end
31
- end
@@ -1,59 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Encore::Helpers::ControllerHelper do
4
- describe :process! do
5
- before do
6
- # Create a model
7
- spawn_model :User
8
-
9
- # Create a controller
10
- spawn_controller :UsersController do
11
- before_filter :fetch_user, only: [:update]
12
-
13
- define_method :create do
14
- @entity = UserEntity.new
15
- @entity.assign_attributes(params[:user], context: :create)
16
- process! @entity
17
- end
18
-
19
- define_method :update do
20
- @entity = UserEntity.new(@user)
21
- @entity.assign_attributes(params[:user], context: :update0)
22
- process! @entity
23
- end
24
-
25
- define_method :fetch_user do
26
- @user = User.find(params[:id])
27
- end
28
- end
29
-
30
- # Create an entity
31
- spawn_entity :UserEntity do
32
- expose :name
33
- end
34
-
35
- run_migration do
36
- create_table(:users, force: true) do |t|
37
- t.string :name
38
- end
39
- end
40
- end
41
-
42
- let(:request) { ActionDispatch::TestRequest.new }
43
- let(:env) { request.env.tap { |e| e['action_dispatch.request.parameters'] = params } }
44
-
45
- context 'for create context' do
46
- let(:params) { { user: { name: 'H.E. Pennypacker' } } }
47
- let(:response) { UsersController.action(:create).call(env) }
48
- it { expect(response.first).to eql 201 }
49
- end
50
-
51
- context 'for update context' do
52
- before { User.create(name: 'Art Vandelay') }
53
-
54
- let(:params) { { id: 1, user: { name: 'H.E. Pennypacker' } } }
55
- let(:response) { UsersController.action(:update).call(env) }
56
- it { expect(response.first).to eql 200 }
57
- end
58
- end
59
- end