adequate_serializer 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75d51f7e119da79d4844eaf7588c35f5822f1d3a
4
- data.tar.gz: 5f287e45995db1e14a3cba9224c30696037005a5
3
+ metadata.gz: 41bf0c76c529a67b403e18abe9130b3f45c67212
4
+ data.tar.gz: 01f9a35b1d5b2563b1798915173144e89d5a7ea1
5
5
  SHA512:
6
- metadata.gz: 64b355e2ca1f0f92e0e7db1d321e8a98fbd15cd010c01616b05ccb5ef8d9da4341945710f95b6907b76c4eaf8805247458d15e5e41982f7a4fcef5803a8b031e
7
- data.tar.gz: a9c19e14e8ba3672d61b6e1311bc3ffe7ec1290d9e675d916b22a9acda90c9a258c5451879cc4057792fc4d2e541c9219b68df6beedd13f7eea6757fa4a16eab
6
+ metadata.gz: a837ae3287a6b6b38e3dd35c17a5705f19dcfde73c02a3045ab3fb2879bf53f3c620ef0bdb2c4d29c378110cadd5ad2c16903feb91798999a0eb5c2551e2c83d
7
+ data.tar.gz: 905b0dca87e74872e8618b4a511f896cbb369c261854e71dcaec3857187c7e2b3c2a1c423ddc3fb9ea80dee00e4160b6472d4796df5778277914217d50174c5d
data/.hound.yml ADDED
@@ -0,0 +1,3 @@
1
+ ruby:
2
+ enabled: true
3
+ config_file: .rubocop.yml
data/.rubocop.yml ADDED
@@ -0,0 +1,258 @@
1
+ AccessorMethodName:
2
+ Enabled: false
3
+
4
+ ActionFilter:
5
+ Enabled: false
6
+
7
+ Alias:
8
+ Enabled: false
9
+
10
+ ArrayJoin:
11
+ Enabled: false
12
+
13
+ AsciiComments:
14
+ Enabled: false
15
+
16
+ AsciiIdentifiers:
17
+ Enabled: false
18
+
19
+ Attr:
20
+ Enabled: false
21
+
22
+ BlockNesting:
23
+ Enabled: false
24
+
25
+ CaseEquality:
26
+ Enabled: false
27
+
28
+ CharacterLiteral:
29
+ Enabled: false
30
+
31
+ ClassAndModuleChildren:
32
+ Enabled: false
33
+
34
+ ClassLength:
35
+ Enabled: false
36
+
37
+ ClassVars:
38
+ Enabled: false
39
+
40
+ CollectionMethods:
41
+ PreferredMethods:
42
+ find: detect
43
+ reduce: inject
44
+ collect: map
45
+ find_all: select
46
+
47
+ ColonMethodCall:
48
+ Enabled: false
49
+
50
+ CommentAnnotation:
51
+ Enabled: false
52
+
53
+ CyclomaticComplexity:
54
+ Enabled: false
55
+
56
+ Delegate:
57
+ Enabled: false
58
+
59
+ DeprecatedHashMethods:
60
+ Enabled: false
61
+
62
+ Documentation:
63
+ Enabled: false
64
+
65
+ DotPosition:
66
+ EnforcedStyle: leading
67
+
68
+ DoubleNegation:
69
+ Enabled: false
70
+
71
+ EachWithObject:
72
+ Enabled: false
73
+
74
+ EmptyLiteral:
75
+ Enabled: false
76
+
77
+ Encoding:
78
+ Enabled: false
79
+
80
+ EvenOdd:
81
+ Enabled: false
82
+
83
+ FileName:
84
+ Enabled: false
85
+
86
+ FlipFlop:
87
+ Enabled: false
88
+
89
+ FormatString:
90
+ Enabled: false
91
+
92
+ GlobalVars:
93
+ Enabled: false
94
+
95
+ GuardClause:
96
+ Enabled: false
97
+
98
+ IfUnlessModifier:
99
+ Enabled: false
100
+
101
+ IfWithSemicolon:
102
+ Enabled: false
103
+
104
+ InlineComment:
105
+ Enabled: false
106
+
107
+ Lambda:
108
+ Enabled: false
109
+
110
+ LambdaCall:
111
+ Enabled: false
112
+
113
+ LineEndConcatenation:
114
+ Enabled: false
115
+
116
+ LineLength:
117
+ Max: 85
118
+
119
+ MethodLength:
120
+ Enabled: false
121
+
122
+ ModuleFunction:
123
+ Enabled: false
124
+
125
+ MultilineOperationIndentation:
126
+ EnforcedStyle: indented
127
+
128
+ NegatedIf:
129
+ Enabled: false
130
+
131
+ NegatedWhile:
132
+ Enabled: false
133
+
134
+ Next:
135
+ Enabled: false
136
+
137
+ NilComparison:
138
+ Enabled: false
139
+
140
+ Not:
141
+ Enabled: false
142
+
143
+ NumericLiterals:
144
+ Enabled: false
145
+
146
+ OneLineConditional:
147
+ Enabled: false
148
+
149
+ OpMethod:
150
+ Enabled: false
151
+
152
+ ParameterLists:
153
+ Enabled: false
154
+
155
+ PercentLiteralDelimiters:
156
+ Enabled: false
157
+
158
+ PerlBackrefs:
159
+ Enabled: false
160
+
161
+ PredicateName:
162
+ NamePrefixBlacklist:
163
+ - is_
164
+
165
+ Proc:
166
+ Enabled: false
167
+
168
+ RaiseArgs:
169
+ Enabled: false
170
+
171
+ RegexpLiteral:
172
+ Enabled: false
173
+
174
+ SelfAssignment:
175
+ Enabled: false
176
+
177
+ SingleLineBlockParams:
178
+ Enabled: false
179
+
180
+ SingleLineMethods:
181
+ Enabled: false
182
+
183
+ SignalException:
184
+ Enabled: false
185
+
186
+ SpecialGlobalVars:
187
+ Enabled: false
188
+
189
+ StringLiterals:
190
+ EnforcedStyle: single_quotes
191
+
192
+ VariableInterpolation:
193
+ Enabled: false
194
+
195
+ TrailingComma:
196
+ Enabled: false
197
+
198
+ TrivialAccessors:
199
+ Enabled: false
200
+
201
+ VariableInterpolation:
202
+ Enabled: false
203
+
204
+ WhenThen:
205
+ Enabled: false
206
+
207
+ WhileUntilModifier:
208
+ Enabled: false
209
+
210
+ WordArray:
211
+ Enabled: false
212
+
213
+ # Lint
214
+
215
+ AmbiguousOperator:
216
+ Enabled: false
217
+
218
+ AmbiguousRegexpLiteral:
219
+ Enabled: false
220
+
221
+ AssignmentInCondition:
222
+ Enabled: false
223
+
224
+ ConditionPosition:
225
+ Enabled: false
226
+
227
+ DeprecatedClassMethods:
228
+ Enabled: false
229
+
230
+ ElseLayout:
231
+ Enabled: false
232
+
233
+ HandleExceptions:
234
+ Enabled: false
235
+
236
+ InvalidCharacterLiteral:
237
+ Enabled: false
238
+
239
+ LiteralInCondition:
240
+ Enabled: false
241
+
242
+ LiteralInInterpolation:
243
+ Enabled: false
244
+
245
+ Loop:
246
+ Enabled: false
247
+
248
+ ParenthesesAsGroupedExpression:
249
+ Enabled: false
250
+
251
+ RequireParentheses:
252
+ Enabled: false
253
+
254
+ UnderscorePrefixedVariableName:
255
+ Enabled: false
256
+
257
+ Void:
258
+ Enabled: false
data/README.md CHANGED
@@ -139,6 +139,29 @@ class UserSerializer::Base
139
139
  end
140
140
  ```
141
141
 
142
+ #### Scope
143
+
144
+ By default, the `current_user` is available in the serializer as `scope`.
145
+
146
+ ```ruby
147
+ class UserSerializer::Base
148
+ attributes :id, :created_at, :updated_at
149
+ associations :posts
150
+
151
+ def posts
152
+ Post.where(user_id: scope.id).published
153
+ end
154
+ end
155
+ ```
156
+
157
+ The scope can be customized in the controller.
158
+
159
+ ```ruby
160
+ serialization_scope :user_id
161
+ ```
162
+
163
+ In this example the `user_id` will be available as `scope`.
164
+
142
165
  ## Development
143
166
 
144
167
  After checking out the repo, run `bin/setup` to install dependencies.
@@ -29,12 +29,13 @@ module AdequateSerializer
29
29
  end
30
30
  end
31
31
 
32
- attr_accessor :object, :includes, :root
32
+ attr_accessor :object, :includes, :root, :scope
33
33
 
34
- def initialize(object, includes: nil, root: nil)
34
+ def initialize(object, options = {})
35
35
  @object = object
36
- @includes = includes
37
- @root = root
36
+ @includes = options[:includes]
37
+ @root = options[:root]
38
+ @scope = options[:scope]
38
39
  end
39
40
 
40
41
  def as_json(options = {})
@@ -98,7 +99,9 @@ module AdequateSerializer
98
99
  associated_objects = associated_objects(association_key)
99
100
 
100
101
  unless associated_objects.nil?
101
- serialize(associated_objects, root: false, includes: includes)
102
+ serialize(
103
+ associated_objects, root: false, includes: includes, scope: scope
104
+ )
102
105
  end
103
106
  end
104
107
 
@@ -5,19 +5,17 @@ module AdequateSerializer
5
5
  class Collection
6
6
  include Helper
7
7
 
8
- attr_accessor :collection, :includes, :root, :serializer
8
+ attr_accessor :collection, :root, :collection_options
9
9
 
10
- def initialize(collection, includes: nil, root: nil, serializer: nil)
10
+ def initialize(collection, options = {})
11
11
  @collection = collection
12
- @includes = includes
13
- @root = root
14
- @serializer = serializer
12
+ @root = options.delete(:root)
13
+ @collection_options = options
14
+ @collection_options[:root] = false
15
15
  end
16
16
 
17
17
  def as_json(options = {})
18
- data = collection.map do |item|
19
- serialize(item, includes: includes, root: false, serializer: serializer)
20
- end
18
+ data = collection.map { |item| serialize(item, collection_options) }
21
19
 
22
20
  if root == false
23
21
  data
@@ -0,0 +1,44 @@
1
+ require 'adequate_serializer/helper'
2
+
3
+ module AdequateSerializer
4
+ module Controller
5
+ include Helper
6
+
7
+ alias_method :helper_serialize, :serialize
8
+
9
+ def self.included(base)
10
+ base.extend ClassMethods
11
+ base.class_eval do
12
+ self._serialization_scope = :current_user
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ attr_accessor :_serialization_scope
18
+
19
+ def inherited(base)
20
+ base._serialization_scope = _serialization_scope
21
+ end
22
+
23
+ def serialization_scope(scope)
24
+ self._serialization_scope = scope
25
+ end
26
+ end
27
+
28
+ def serialize(entity, options = {})
29
+ options[:scope] = serialization_scope
30
+
31
+ helper_serialize(entity, options)
32
+ end
33
+
34
+ private
35
+
36
+ def serialization_scope
37
+ scope = self.class._serialization_scope
38
+
39
+ if scope && respond_to?(scope, true)
40
+ send(scope)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,14 +1,19 @@
1
1
  module AdequateSerializer
2
2
  module Helper
3
- def serialize(entity, includes: nil, root: nil, serializer: nil)
3
+ def serialize(entity, options = {})
4
4
  if entity.respond_to?(:each)
5
- Collection.new(
6
- entity, includes: includes, root: root, serializer: serializer
7
- ).as_json
5
+ serializer = Collection
8
6
  else
9
- serializer ||= Object.const_get("#{entity.class}Serializer")
10
- serializer.new(entity, includes: includes, root: root).as_json
7
+ serializer = options[:serializer] || serializer_klass(entity)
11
8
  end
9
+
10
+ serializer.new(entity, options).as_json
11
+ end
12
+
13
+ private
14
+
15
+ def serializer_klass(entity)
16
+ Object.const_get("#{entity.class}Serializer")
12
17
  end
13
18
  end
14
19
  end
@@ -2,7 +2,7 @@ module AdequateSerializer
2
2
  class Railtie < Rails::Railtie
3
3
  initializer 'adequate_serializer.setup_helper' do
4
4
  ActiveSupport.on_load :action_controller do
5
- include AdequateSerializer::Helper
5
+ include AdequateSerializer::Controller
6
6
  end
7
7
  end
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module AdequateSerializer
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require 'adequate_serializer/base'
2
2
  require 'adequate_serializer/collection'
3
3
  require 'adequate_serializer/helper'
4
+ require 'adequate_serializer/controller'
4
5
  require 'adequate_serializer/version'
5
6
  require 'adequate_serializer/railtie' if defined?(Rails)
6
7
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adequate_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bastian Bartmann
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-25 00:00:00.000000000 Z
11
+ date: 2015-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -74,6 +74,8 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
+ - ".hound.yml"
78
+ - ".rubocop.yml"
77
79
  - ".travis.yml"
78
80
  - CODE_OF_CONDUCT.md
79
81
  - Gemfile
@@ -86,6 +88,7 @@ files:
86
88
  - lib/adequate_serializer.rb
87
89
  - lib/adequate_serializer/base.rb
88
90
  - lib/adequate_serializer/collection.rb
91
+ - lib/adequate_serializer/controller.rb
89
92
  - lib/adequate_serializer/helper.rb
90
93
  - lib/adequate_serializer/railtie.rb
91
94
  - lib/adequate_serializer/version.rb