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
metadata CHANGED
@@ -1,140 +1,220 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: '0.1'
5
5
  platform: ruby
6
6
  authors:
7
- - Rémi Prévost
7
+ - Simon Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-04 00:00:00.000000000 Z
11
+ date: 2014-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: active_model_serializers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: kaminari
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.15.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.15.1
13
69
  - !ruby/object:Gem::Dependency
14
70
  name: bundler
15
71
  requirement: !ruby/object:Gem::Requirement
16
72
  requirements:
17
- - - '>='
73
+ - - "~>"
18
74
  - !ruby/object:Gem::Version
19
- version: '0'
75
+ version: '1.3'
20
76
  type: :development
21
77
  prerelease: false
22
78
  version_requirements: !ruby/object:Gem::Requirement
23
79
  requirements:
24
- - - '>='
80
+ - - "~>"
25
81
  - !ruby/object:Gem::Version
26
- version: '0'
82
+ version: '1.3'
27
83
  - !ruby/object:Gem::Dependency
28
84
  name: rake
29
85
  requirement: !ruby/object:Gem::Requirement
30
86
  requirements:
31
- - - '>='
87
+ - - "~>"
32
88
  - !ruby/object:Gem::Version
33
- version: '0'
89
+ version: '10.3'
34
90
  type: :development
35
91
  prerelease: false
36
92
  version_requirements: !ruby/object:Gem::Requirement
37
93
  requirements:
38
- - - '>='
94
+ - - "~>"
39
95
  - !ruby/object:Gem::Version
40
- version: '0'
96
+ version: '10.3'
41
97
  - !ruby/object:Gem::Dependency
42
98
  name: rspec
43
99
  requirement: !ruby/object:Gem::Requirement
44
100
  requirements:
45
- - - '>='
101
+ - - '='
46
102
  - !ruby/object:Gem::Version
47
- version: '0'
103
+ version: 3.0.0beta2
48
104
  type: :development
49
105
  prerelease: false
50
106
  version_requirements: !ruby/object:Gem::Requirement
51
107
  requirements:
52
- - - '>='
108
+ - - '='
53
109
  - !ruby/object:Gem::Version
54
- version: '0'
110
+ version: 3.0.0beta2
55
111
  - !ruby/object:Gem::Dependency
56
112
  name: sqlite3
57
113
  requirement: !ruby/object:Gem::Requirement
58
114
  requirements:
59
- - - '>='
115
+ - - ">="
60
116
  - !ruby/object:Gem::Version
61
- version: '0'
117
+ version: 1.3.8
118
+ - - "<"
119
+ - !ruby/object:Gem::Version
120
+ version: '1.4'
62
121
  type: :development
63
122
  prerelease: false
64
123
  version_requirements: !ruby/object:Gem::Requirement
65
124
  requirements:
66
- - - '>='
125
+ - - ">="
67
126
  - !ruby/object:Gem::Version
68
- version: '0'
127
+ version: 1.3.8
128
+ - - "<"
129
+ - !ruby/object:Gem::Version
130
+ version: '1.4'
69
131
  - !ruby/object:Gem::Dependency
70
- name: rails
132
+ name: rubocop
71
133
  requirement: !ruby/object:Gem::Requirement
72
134
  requirements:
73
- - - '>='
135
+ - - '='
74
136
  - !ruby/object:Gem::Version
75
- version: 3.0.0
137
+ version: '0.22'
76
138
  type: :development
77
139
  prerelease: false
78
140
  version_requirements: !ruby/object:Gem::Requirement
79
141
  requirements:
80
- - - '>='
142
+ - - '='
81
143
  - !ruby/object:Gem::Version
82
- version: 3.0.0
144
+ version: '0.22'
83
145
  - !ruby/object:Gem::Dependency
84
- name: activesupport
146
+ name: phare
85
147
  requirement: !ruby/object:Gem::Requirement
86
148
  requirements:
87
- - - '>='
149
+ - - ">="
88
150
  - !ruby/object:Gem::Version
89
- version: 3.0.0
90
- type: :runtime
151
+ version: '0'
152
+ type: :development
91
153
  prerelease: false
92
154
  version_requirements: !ruby/object:Gem::Requirement
93
155
  requirements:
94
- - - '>='
156
+ - - ">="
95
157
  - !ruby/object:Gem::Version
96
- version: 3.0.0
97
- description: Encore is a Ruby framework to help dealing with entities.
158
+ version: '0'
159
+ description: Encore provides serializers and persisters to build JSON API-compliant
160
+ Web services with Ruby on Rails.
98
161
  email:
99
- - rprevost@mirego.com
162
+ - sprevost@mirego.com
100
163
  executables: []
101
164
  extensions: []
102
165
  extra_rdoc_files: []
103
166
  files:
104
- - .gitignore
105
- - .rspec
106
- - .travis.yml
167
+ - ".gitignore"
168
+ - ".rspec"
169
+ - ".rubocop.yml"
170
+ - ".travis.yml"
107
171
  - Gemfile
108
172
  - LICENSE.md
109
173
  - README.md
110
174
  - Rakefile
111
175
  - encore.gemspec
112
- - gemfiles/Gemfile.activerecord-3.2.x
113
176
  - gemfiles/Gemfile.activerecord-4.0
177
+ - gemfiles/Gemfile.activerecord-4.1
114
178
  - lib/encore.rb
115
- - lib/encore/association.rb
116
- - lib/encore/association/has_many_association.rb
117
- - lib/encore/association/has_one_association.rb
118
- - lib/encore/attribute.rb
119
- - lib/encore/entity.rb
120
- - lib/encore/entity/input.rb
121
- - lib/encore/entity/input/associations.rb
122
- - lib/encore/entity/input/errors.rb
123
- - lib/encore/entity/input/exposed_attributes.rb
124
- - lib/encore/entity/output.rb
125
- - lib/encore/entity/output/json.rb
126
- - lib/encore/helpers.rb
127
- - lib/encore/helpers/controller_helper.rb
128
- - lib/encore/railtie.rb
179
+ - lib/encore/config.rb
180
+ - lib/encore/persister/errors_parser.rb
181
+ - lib/encore/persister/instance.rb
182
+ - lib/encore/persister/key_mapping.rb
183
+ - lib/encore/persister/links_parser.rb
184
+ - lib/encore/persister/param_injection.rb
185
+ - lib/encore/serializer/base.rb
186
+ - lib/encore/serializer/eager_loading_manager.rb
187
+ - lib/encore/serializer/instance.rb
188
+ - lib/encore/serializer/linked_resource_manager.rb
189
+ - lib/encore/serializer/links_reflection_includer.rb
190
+ - lib/encore/serializer/main_resource_links_manager.rb
191
+ - lib/encore/serializer/main_resource_links_manager/reflection_belongs_to.rb
192
+ - lib/encore/serializer/main_resource_links_manager/reflection_has_many.rb
193
+ - lib/encore/serializer/main_resource_links_manager/reflection_has_one.rb
194
+ - lib/encore/serializer/main_resource_manager.rb
195
+ - lib/encore/serializer/meta_manager.rb
196
+ - lib/encore/serializer/options_parser.rb
197
+ - lib/encore/serializer/utils.rb
129
198
  - lib/encore/version.rb
130
- - spec/encore/entity/input/associations_spec.rb
131
- - spec/encore/entity/input/exposed_attributes_spec.rb
132
- - spec/encore/entity/input_spec.rb
133
- - spec/encore/entity/output/json_spec.rb
134
- - spec/encore/entity/output_spec.rb
135
- - spec/encore/entity_spec.rb
136
- - spec/encore/helpers/controller_helper_spec.rb
199
+ - spec/encore/persister/belongs_to_links_spec.rb
200
+ - spec/encore/persister/create_resources_spec.rb
201
+ - spec/encore/persister/errors_resources_spec.rb
202
+ - spec/encore/persister/has_many_links_spec.rb
203
+ - spec/encore/persister/has_one_links_spec.rb
204
+ - spec/encore/persister/key_mappings_spec.rb
205
+ - spec/encore/persister/param_injection_spec.rb
206
+ - spec/encore/persister/update_resources_spec.rb
207
+ - spec/encore/serializer/linked/always_include_resources_spec.rb
208
+ - spec/encore/serializer/linked/can_include_resources_spec.rb
209
+ - spec/encore/serializer/linked/linked_resources_spec.rb
210
+ - spec/encore/serializer/links_resource_spec.rb
211
+ - spec/encore/serializer/main_meta_spec.rb
212
+ - spec/encore/serializer/main_resource_spec.rb
213
+ - spec/encore/serializer/no_paging_spec.rb
214
+ - spec/encore_spec.rb
137
215
  - spec/spec_helper.rb
216
+ - spec/support/macros/database/database_adapter.rb
217
+ - spec/support/macros/database/sqlite3_adapter.rb
138
218
  - spec/support/macros/database_macros.rb
139
219
  - spec/support/macros/model_macros.rb
140
220
  homepage: https://github.com/mirego/encore
@@ -147,28 +227,19 @@ require_paths:
147
227
  - lib
148
228
  required_ruby_version: !ruby/object:Gem::Requirement
149
229
  requirements:
150
- - - '>='
230
+ - - ">="
151
231
  - !ruby/object:Gem::Version
152
232
  version: '0'
153
233
  required_rubygems_version: !ruby/object:Gem::Requirement
154
234
  requirements:
155
- - - '>='
235
+ - - ">="
156
236
  - !ruby/object:Gem::Version
157
237
  version: '0'
158
238
  requirements: []
159
239
  rubyforge_project:
160
- rubygems_version: 2.0.2
240
+ rubygems_version: 2.2.2
161
241
  signing_key:
162
242
  specification_version: 4
163
- summary: Encore is a Ruby framework to help dealing with entities.
164
- test_files:
165
- - spec/encore/entity/input/associations_spec.rb
166
- - spec/encore/entity/input/exposed_attributes_spec.rb
167
- - spec/encore/entity/input_spec.rb
168
- - spec/encore/entity/output/json_spec.rb
169
- - spec/encore/entity/output_spec.rb
170
- - spec/encore/entity_spec.rb
171
- - spec/encore/helpers/controller_helper_spec.rb
172
- - spec/spec_helper.rb
173
- - spec/support/macros/database_macros.rb
174
- - spec/support/macros/model_macros.rb
243
+ summary: Encore provides serializers and persisters to build JSON API-compliant Web
244
+ services with Ruby on Rails.
245
+ test_files: []
@@ -1,12 +0,0 @@
1
- module Encore
2
- class Association
3
- class HasManyAssociation < Association
4
-
5
- protected
6
-
7
- def column_accessor
8
- :"#{self.column.to_s.singularize}_ids"
9
- end
10
- end
11
- end
12
- end
@@ -1,12 +0,0 @@
1
- module Encore
2
- class Association
3
- class HasOneAssociation < Association
4
-
5
- protected
6
-
7
- def column_accessor
8
- :"#{self.column}_id"
9
- end
10
- end
11
- end
12
- end
@@ -1,39 +0,0 @@
1
- module Encore
2
- class Association < Attribute
3
- def initialize(*args)
4
- super
5
-
6
- # Create a temporary accessor to store new values
7
- @klass.send :attr_accessor, self.tmp_column_accessor
8
-
9
- # Add callback on `before_save` to apply new values
10
- association = self
11
- @klass.before_save lambda { association.apply(self) }
12
- end
13
-
14
- # Store the value in a temporary accessor used by the `before_save` callback
15
- def assign(object, value)
16
- object.send :"#{tmp_column_accessor}=", value
17
- end
18
-
19
- # Apply the new value to the column and reset the temporary accessor
20
- def apply(object)
21
- object.send :"#{column_accessor}=", object.send(tmp_column_accessor)
22
- object.send :"#{tmp_column_accessor}=", nil
23
- end
24
-
25
- # Return the value of the association
26
- def fetch(object)
27
- object.send :"#{column_accessor}"
28
- end
29
-
30
- protected
31
-
32
- def tmp_column_accessor
33
- :"encore_tmp_#{column_accessor}"
34
- end
35
- end
36
- end
37
-
38
- require 'encore/association/has_one_association'
39
- require 'encore/association/has_many_association'
@@ -1,42 +0,0 @@
1
- module Encore
2
- class Attribute
3
- attr_reader :attribute, :klass, :column, :opts
4
-
5
- def initialize(klass, column, opts = {})
6
- @klass = klass
7
- @column = column
8
- @attribute = opts[:as].try(:to_sym) || @column
9
- @opts = opts
10
- end
11
-
12
- def assign(object, value)
13
- object.send :"#{self.column}=", value
14
- end
15
-
16
- def fetch(object)
17
- object.send :"#{self.column}"
18
- end
19
-
20
- def ==(other_attribute)
21
- hash == other_attribute.hash
22
- end
23
-
24
- def eql?(other)
25
- self == other
26
- end
27
-
28
- # Generate a hash for comparisons
29
- def hash
30
- [@attribute, @klass].hash
31
- end
32
-
33
- # Return whether the attribute is read-only
34
- def readonly?
35
- !!@opts[:readonly]
36
- end
37
-
38
- def to_s
39
- "#<Encore::Attribute #{@klass}@#{@attribute}>"
40
- end
41
- end
42
- end
@@ -1,27 +0,0 @@
1
- module Encore
2
- module Entity
3
- module Input
4
- module Associations
5
- extend ActiveSupport::Concern
6
-
7
- module ClassMethods
8
- def expose_one(column, opts = {})
9
- expose_association(:one, column, opts)
10
- end
11
-
12
- def expose_many(column, opts = {})
13
- expose_association(:many, column, opts)
14
- end
15
-
16
- protected
17
-
18
- def expose_association(type, column, opts)
19
- @exposed_attributes ||= []
20
- association_class = Encore::Association.const_get("Has#{type.to_s.capitalize}Association")
21
- @exposed_attributes << association_class.new(self, column.to_sym, opts)
22
- end
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,8 +0,0 @@
1
- module Encore
2
- module Entity
3
- module Input
4
- class InvalidAttributeError < StandardError
5
- end
6
- end
7
- end
8
- end
@@ -1,37 +0,0 @@
1
- module Encore
2
- module Entity
3
- module Input
4
- module ExposedAttributes
5
- extend ActiveSupport::Concern
6
-
7
- module ClassMethods
8
- # Declare a new exposed attribute
9
- def expose(column, opts = {})
10
- @exposed_attributes ||= []
11
- @exposed_attributes << Encore::Attribute.new(self, column.to_sym, opts)
12
- end
13
-
14
- # Return whether an attribute is exposed by the entity
15
- def exposed_attribute?(attribute)
16
- @exposed_attributes.detect { |a| attribute == a }
17
- end
18
-
19
- # Return whether an attribute is exposed by the entity
20
- def modifiable_attribute?(attribute)
21
- modifiable_attributes.include?(attribute)
22
- end
23
-
24
- # Return a list of all exposed attributes
25
- def exposed_attributes
26
- @exposed_attributes
27
- end
28
-
29
- # Return a list of all attributes that can be modified
30
- def modifiable_attributes
31
- @exposed_attributes.reject(&:readonly?)
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,73 +0,0 @@
1
- require 'encore/entity/input/errors'
2
- require 'encore/entity/input/exposed_attributes'
3
- require 'encore/entity/input/associations'
4
-
5
- module Encore
6
- module Entity
7
- module Input
8
- extend ActiveSupport::Concern
9
-
10
- included do
11
- extend ActiveModel::Callbacks
12
- include Encore::Entity::Input::ExposedAttributes
13
- include Encore::Entity::Input::Associations
14
- define_model_callbacks :save
15
-
16
- # Expose ID as a readonly attribute
17
- expose :id, readonly: true
18
-
19
- # Make the context accessible
20
- attr_reader :context
21
- end
22
-
23
- # Assign specific attribute to the object
24
- def assign_attributes(attributes = {}, opts = nil)
25
- # Find the right context
26
- unless @context = opts.try(:delete, :context)
27
- @context = @object.persisted? ? :partial_update : :create
28
- end
29
-
30
- # Convert attribute keys to Attribute objects
31
- attributes = self.class.map_attributes(self.class, attributes)
32
-
33
- # Loop through every passed attribute and set it
34
- assign_provided_attributes(attributes)
35
-
36
- # If we're doing an update, for any exposed attribute that wasn't passed, set it to nil
37
- reset_forgotten_attributes(attributes)
38
- end
39
-
40
- protected
41
-
42
- def assign_provided_attributes(attributes)
43
- attributes.each_pair do |attribute, value|
44
- if self.class.modifiable_attribute?(attribute)
45
- attribute.assign(self, value)
46
- else
47
- raise Encore::Entity::Input::InvalidAttributeError.new("The #{attribute} attribute is not exposed.")
48
- end
49
- end
50
- end
51
-
52
- def reset_forgotten_attributes(attributes)
53
- if @context == :update
54
- (self.class.modifiable_attributes - attributes.keys).each do |attribute|
55
- attribute.assign(self, nil)
56
- end
57
- end
58
- end
59
-
60
- module ClassMethods
61
- def map_attributes(klass, params)
62
- params.inject({}) do |memo, (attribute, value)|
63
- exposed_attribute = klass.exposed_attributes.detect { |a| a.attribute == attribute }
64
- key = exposed_attribute || Attribute.new(klass, attribute.to_sym)
65
-
66
- memo.merge key => value
67
- end
68
- end
69
-
70
- end
71
- end
72
- end
73
- end
@@ -1,14 +0,0 @@
1
- module Encore
2
- module Entity
3
- module Output
4
- module JSON
5
- # Return each exposed attribute as a JSON value
6
- def as_json(args = {})
7
- self.class.exposed_attributes.inject({}) do |memo, item|
8
- memo.merge item.attribute => item.fetch(self)
9
- end
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,13 +0,0 @@
1
- require 'encore/entity/output/json'
2
-
3
- module Encore
4
- module Entity
5
- module Output
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- include Encore::Entity::Output::JSON
10
- end
11
- end
12
- end
13
- end
data/lib/encore/entity.rb DELETED
@@ -1,44 +0,0 @@
1
- require 'encore/entity/input'
2
- require 'encore/entity/output'
3
-
4
- module Encore
5
- module Entity
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- include Input
10
- include Output
11
-
12
- # Make `object` accessible for others
13
- attr_accessor :object
14
- end
15
-
16
- # Initialize a new entity, linked to an ActiveRecord object
17
- # If no object is passed, let's try to build a new one
18
- def initialize(object = nil)
19
- @object = object || self.class.linked_class.new
20
- end
21
-
22
- # Save the object
23
- def save
24
- run_callbacks(:save) { @object.save }
25
- end
26
-
27
- # Delegate everything to the object
28
- def method_missing(method, *args, &blk)
29
- if @object.respond_to?(method)
30
- @object.send(method, *args, &blk)
31
- else
32
- super
33
- end
34
- end
35
-
36
- module ClassMethods
37
- # Return the object class. If no object is present, try to
38
- # guess the class with our own class name (`UserEntity` would be `User`)
39
- def linked_class
40
- @object ? @object.class : self.name.split(/Entity$/).first.constantize
41
- end
42
- end
43
- end
44
- end
@@ -1,37 +0,0 @@
1
- module Encore
2
- module Helpers
3
- module ControllerHelper
4
- HTTP_ERROR_STATUS_CODE = 422
5
- HTTP_SUCCESS_CODE = 200
6
- HTTP_SUCCESS_CREATED_CODE = 201
7
-
8
- # Inject itself into the ActionController::Base class
9
- def self.inject_into_action_controller
10
- ::ActionController::Base.send :include, self
11
- end
12
-
13
- # Save the entity and return a JSON response
14
- def process!(entity)
15
- error_status = HTTP_ERROR_STATUS_CODE
16
- success_status = success_status_code_from_entity(entity)
17
-
18
- if entity.save
19
- render json: entity, status: success_status
20
- else
21
- render json: { errors: entity.errors }, status: error_status
22
- end
23
- end
24
-
25
- protected
26
-
27
- # Return the status code to send after a successful processing
28
- # of an entity, depending on its context
29
- def success_status_code_from_entity(entity)
30
- case entity.context
31
- when :create then HTTP_SUCCESS_CREATED_CODE
32
- else HTTP_SUCCESS_CODE
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1 +0,0 @@
1
- require 'encore/helpers/controller_helper'