Skip to content

Commit 85f417f

Browse files
committed
re: RuboCop - Use nested module/class definition instead of compact style.
1 parent 8a2beac commit 85f417f

File tree

15 files changed

+653
-626
lines changed

15 files changed

+653
-626
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Style/AlignParameters:
4646
EnforcedStyle: with_fixed_indentation
4747

4848
Style/ClassAndModuleChildren:
49-
EnforcedStyle: compact
49+
EnforcedStyle: nested
5050

5151
Style/Documentation:
5252
Enabled: false

.rubocop_todo.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ Lint/HandleExceptions:
1212
- 'Rakefile'
1313

1414

15-
# Offense count: 271
16-
# Configuration parameters: EnforcedStyle, SupportedStyles.
17-
# SupportedStyles: nested, compact
18-
Style/ClassAndModuleChildren:
19-
Enabled: false
20-
21-
2215

2316
# Offense count: 3
2417
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
require 'active_model/serializer/collection_serializer'
2-
class ActiveModel::Serializer
3-
class ArraySerializer < CollectionSerializer
4-
class << self
5-
extend ActiveModelSerializers::Deprecate
6-
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
2+
3+
module ActiveModel
4+
class Serializer
5+
class ArraySerializer < CollectionSerializer
6+
class << self
7+
extend ActiveModelSerializers::Deprecate
8+
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
9+
end
710
end
811
end
912
end
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
class ActiveModel::Serializer::ErrorSerializer < ActiveModel::Serializer
2-
# @return [Hash<field_name,Array<error_message>>]
3-
def as_json
4-
object.errors.messages
5-
end
1+
module ActiveModel
2+
class Serializer
3+
class ErrorSerializer < ActiveModel::Serializer
4+
# @return [Hash<field_name,Array<error_message>>]
5+
def as_json
6+
object.errors.messages
7+
end
68

7-
def success?
8-
false
9+
def success?
10+
false
11+
end
12+
end
913
end
1014
end
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
require 'active_model/serializer/error_serializer'
2-
class ActiveModel::Serializer::ErrorsSerializer
3-
include Enumerable
4-
delegate :each, to: :@serializers
5-
attr_reader :object, :root
62

7-
def initialize(resources, options = {})
8-
@root = options[:root]
9-
@object = resources
10-
@serializers = resources.map do |resource|
11-
serializer_class = options.fetch(:serializer) { ActiveModel::Serializer::ErrorSerializer }
12-
serializer_class.new(resource, options.except(:serializer))
13-
end
14-
end
3+
module ActiveModel
4+
class Serializer
5+
class ErrorsSerializer
6+
include Enumerable
7+
delegate :each, to: :@serializers
8+
attr_reader :object, :root
159

16-
def success?
17-
false
18-
end
10+
def initialize(resources, options = {})
11+
@root = options[:root]
12+
@object = resources
13+
@serializers = resources.map do |resource|
14+
serializer_class = options.fetch(:serializer) { ActiveModel::Serializer::ErrorSerializer }
15+
serializer_class.new(resource, options.except(:serializer))
16+
end
17+
end
1918

20-
def json_key
21-
nil
22-
end
19+
def success?
20+
false
21+
end
2322

24-
protected
23+
def json_key
24+
nil
25+
end
2526

26-
attr_reader :serializers
27+
protected
28+
29+
attr_reader :serializers
30+
end
31+
end
2732
end

lib/active_model/serializer/lint.rb

Lines changed: 134 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,150 @@
1-
module ActiveModel::Serializer::Lint
2-
# == Active \Model \Serializer \Lint \Tests
3-
#
4-
# You can test whether an object is compliant with the Active \Model \Serializers
5-
# API by including <tt>ActiveModel::Serializer::Lint::Tests</tt> in your TestCase.
6-
# It will include tests that tell you whether your object is fully compliant,
7-
# or if not, which aspects of the API are not implemented.
8-
#
9-
# Note an object is not required to implement all APIs in order to work
10-
# with Active \Model \Serializers. This module only intends to provide guidance in case
11-
# you want all features out of the box.
12-
#
13-
# These tests do not attempt to determine the semantic correctness of the
14-
# returned values. For instance, you could implement <tt>serializable_hash</tt> to
15-
# always return +{}+, and the tests would pass. It is up to you to ensure
16-
# that the values are semantically meaningful.
17-
module Tests
18-
# Passes if the object responds to <tt>serializable_hash</tt> and if it takes
19-
# zero or one arguments.
20-
# Fails otherwise.
21-
#
22-
# <tt>serializable_hash</tt> returns a hash representation of a object's attributes.
23-
# Typically, it is implemented by including ActiveModel::Serialization.
24-
def test_serializable_hash
25-
assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash'
26-
resource.serializable_hash
27-
resource.serializable_hash(nil)
28-
end
1+
module ActiveModel
2+
class Serializer
3+
module Lint
4+
# == Active \Model \Serializer \Lint \Tests
5+
#
6+
# You can test whether an object is compliant with the Active \Model \Serializers
7+
# API by including <tt>ActiveModel::Serializer::Lint::Tests</tt> in your TestCase.
8+
# It will include tests that tell you whether your object is fully compliant,
9+
# or if not, which aspects of the API are not implemented.
10+
#
11+
# Note an object is not required to implement all APIs in order to work
12+
# with Active \Model \Serializers. This module only intends to provide guidance in case
13+
# you want all features out of the box.
14+
#
15+
# These tests do not attempt to determine the semantic correctness of the
16+
# returned values. For instance, you could implement <tt>serializable_hash</tt> to
17+
# always return +{}+, and the tests would pass. It is up to you to ensure
18+
# that the values are semantically meaningful.
19+
module Tests
20+
# Passes if the object responds to <tt>serializable_hash</tt> and if it takes
21+
# zero or one arguments.
22+
# Fails otherwise.
23+
#
24+
# <tt>serializable_hash</tt> returns a hash representation of a object's attributes.
25+
# Typically, it is implemented by including ActiveModel::Serialization.
26+
def test_serializable_hash
27+
assert_respond_to resource, :serializable_hash, 'The resource should respond to serializable_hash'
28+
resource.serializable_hash
29+
resource.serializable_hash(nil)
30+
end
2931

30-
# Passes if the object responds to <tt>read_attribute_for_serialization</tt>
31-
# and if it requires one argument (the attribute to be read).
32-
# Fails otherwise.
33-
#
34-
# <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization
35-
# Typically, it is implemented by including ActiveModel::Serialization.
36-
def test_read_attribute_for_serialization
37-
assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization'
38-
actual_arity = resource.method(:read_attribute_for_serialization).arity
39-
# using absolute value since arity is:
40-
# 1 for def read_attribute_for_serialization(name); end
41-
# -1 for alias :read_attribute_for_serialization :send
42-
assert_equal 1, actual_arity.abs, "expected #{actual_arity.inspect}.abs to be 1 or -1"
43-
end
32+
# Passes if the object responds to <tt>read_attribute_for_serialization</tt>
33+
# and if it requires one argument (the attribute to be read).
34+
# Fails otherwise.
35+
#
36+
# <tt>read_attribute_for_serialization</tt> gets the attribute value for serialization
37+
# Typically, it is implemented by including ActiveModel::Serialization.
38+
def test_read_attribute_for_serialization
39+
assert_respond_to resource, :read_attribute_for_serialization, 'The resource should respond to read_attribute_for_serialization'
40+
actual_arity = resource.method(:read_attribute_for_serialization).arity
41+
# using absolute value since arity is:
42+
# 1 for def read_attribute_for_serialization(name); end
43+
# -1 for alias :read_attribute_for_serialization :send
44+
assert_equal 1, actual_arity.abs, "expected #{actual_arity.inspect}.abs to be 1 or -1"
45+
end
4446

45-
# Passes if the object responds to <tt>as_json</tt> and if it takes
46-
# zero or one arguments.
47-
# Fails otherwise.
48-
#
49-
# <tt>as_json</tt> returns a hash representation of a serialized object.
50-
# It may delegate to <tt>serializable_hash</tt>
51-
# Typically, it is implemented either by including ActiveModel::Serialization
52-
# which includes ActiveModel::Serializers::JSON.
53-
# or by the JSON gem when required.
54-
def test_as_json
55-
assert_respond_to resource, :as_json
56-
resource.as_json
57-
resource.as_json(nil)
58-
end
47+
# Passes if the object responds to <tt>as_json</tt> and if it takes
48+
# zero or one arguments.
49+
# Fails otherwise.
50+
#
51+
# <tt>as_json</tt> returns a hash representation of a serialized object.
52+
# It may delegate to <tt>serializable_hash</tt>
53+
# Typically, it is implemented either by including ActiveModel::Serialization
54+
# which includes ActiveModel::Serializers::JSON.
55+
# or by the JSON gem when required.
56+
def test_as_json
57+
assert_respond_to resource, :as_json
58+
resource.as_json
59+
resource.as_json(nil)
60+
end
5961

60-
# Passes if the object responds to <tt>to_json</tt> and if it takes
61-
# zero or one arguments.
62-
# Fails otherwise.
63-
#
64-
# <tt>to_json</tt> returns a string representation (JSON) of a serialized object.
65-
# It may be called on the result of <tt>as_json</tt>.
66-
# Typically, it is implemented on all objects when the JSON gem is required.
67-
def test_to_json
68-
assert_respond_to resource, :to_json
69-
resource.to_json
70-
resource.to_json(nil)
71-
end
62+
# Passes if the object responds to <tt>to_json</tt> and if it takes
63+
# zero or one arguments.
64+
# Fails otherwise.
65+
#
66+
# <tt>to_json</tt> returns a string representation (JSON) of a serialized object.
67+
# It may be called on the result of <tt>as_json</tt>.
68+
# Typically, it is implemented on all objects when the JSON gem is required.
69+
def test_to_json
70+
assert_respond_to resource, :to_json
71+
resource.to_json
72+
resource.to_json(nil)
73+
end
7274

73-
# Passes if the object responds to <tt>cache_key</tt>
74-
# Fails otherwise.
75-
#
76-
# <tt>cache_key</tt> returns a (self-expiring) unique key for the object,
77-
# and is part of the (self-expiring) cache_key, which is used by the
78-
# adapter. It is not required unless caching is enabled.
79-
def test_cache_key
80-
assert_respond_to resource, :cache_key
81-
actual_arity = resource.method(:cache_key).arity
82-
assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1"
83-
end
75+
# Passes if the object responds to <tt>cache_key</tt>
76+
# Fails otherwise.
77+
#
78+
# <tt>cache_key</tt> returns a (self-expiring) unique key for the object,
79+
# and is part of the (self-expiring) cache_key, which is used by the
80+
# adapter. It is not required unless caching is enabled.
81+
def test_cache_key
82+
assert_respond_to resource, :cache_key
83+
actual_arity = resource.method(:cache_key).arity
84+
assert_includes [-1, 0], actual_arity, "expected #{actual_arity.inspect} to be 0 or -1"
85+
end
8486

85-
# Passes if the object responds to <tt>updated_at</tt> and if it takes no
86-
# arguments.
87-
# Fails otherwise.
88-
#
89-
# <tt>updated_at</tt> returns a Time object or iso8601 string and
90-
# is part of the (self-expiring) cache_key, which is used by the adapter.
91-
# It is not required unless caching is enabled.
92-
def test_updated_at
93-
assert_respond_to resource, :updated_at
94-
actual_arity = resource.method(:updated_at).arity
95-
assert_equal 0, actual_arity
96-
end
87+
# Passes if the object responds to <tt>updated_at</tt> and if it takes no
88+
# arguments.
89+
# Fails otherwise.
90+
#
91+
# <tt>updated_at</tt> returns a Time object or iso8601 string and
92+
# is part of the (self-expiring) cache_key, which is used by the adapter.
93+
# It is not required unless caching is enabled.
94+
def test_updated_at
95+
assert_respond_to resource, :updated_at
96+
actual_arity = resource.method(:updated_at).arity
97+
assert_equal 0, actual_arity
98+
end
9799

98-
# Passes if the object responds to <tt>id</tt> and if it takes no
99-
# arguments.
100-
# Fails otherwise.
101-
#
102-
# <tt>id</tt> returns a unique identifier for the object.
103-
# It is not required unless caching is enabled.
104-
def test_id
105-
assert_respond_to resource, :id
106-
assert_equal 0, resource.method(:id).arity
107-
end
100+
# Passes if the object responds to <tt>id</tt> and if it takes no
101+
# arguments.
102+
# Fails otherwise.
103+
#
104+
# <tt>id</tt> returns a unique identifier for the object.
105+
# It is not required unless caching is enabled.
106+
def test_id
107+
assert_respond_to resource, :id
108+
assert_equal 0, resource.method(:id).arity
109+
end
108110

109-
# Passes if the object's class responds to <tt>model_name</tt> and if it
110-
# is in an instance of +ActiveModel::Name+.
111-
# Fails otherwise.
112-
#
113-
# <tt>model_name</tt> returns an ActiveModel::Name instance.
114-
# It is used by the serializer to identify the object's type.
115-
# It is not required unless caching is enabled.
116-
def test_model_name
117-
resource_class = resource.class
118-
assert_respond_to resource_class, :model_name
119-
assert_instance_of resource_class.model_name, ActiveModel::Name
120-
end
111+
# Passes if the object's class responds to <tt>model_name</tt> and if it
112+
# is in an instance of +ActiveModel::Name+.
113+
# Fails otherwise.
114+
#
115+
# <tt>model_name</tt> returns an ActiveModel::Name instance.
116+
# It is used by the serializer to identify the object's type.
117+
# It is not required unless caching is enabled.
118+
def test_model_name
119+
resource_class = resource.class
120+
assert_respond_to resource_class, :model_name
121+
assert_instance_of resource_class.model_name, ActiveModel::Name
122+
end
121123

122-
def test_active_model_errors
123-
assert_respond_to resource, :errors
124-
end
124+
def test_active_model_errors
125+
assert_respond_to resource, :errors
126+
end
125127

126-
def test_active_model_errors_human_attribute_name
127-
assert_respond_to resource.class, :human_attribute_name
128-
assert_equal(-2, resource.class.method(:human_attribute_name).arity)
129-
end
128+
def test_active_model_errors_human_attribute_name
129+
assert_respond_to resource.class, :human_attribute_name
130+
assert_equal(-2, resource.class.method(:human_attribute_name).arity)
131+
end
130132

131-
def test_active_model_errors_lookup_ancestors
132-
assert_respond_to resource.class, :lookup_ancestors
133-
assert_equal 0, resource.class.method(:lookup_ancestors).arity
134-
end
133+
def test_active_model_errors_lookup_ancestors
134+
assert_respond_to resource.class, :lookup_ancestors
135+
assert_equal 0, resource.class.method(:lookup_ancestors).arity
136+
end
135137

136-
private
138+
private
137139

138-
def resource
139-
@resource or fail "'@resource' must be set as the linted object"
140-
end
140+
def resource
141+
@resource or fail "'@resource' must be set as the linted object"
142+
end
141143

142-
def assert_instance_of(result, name)
143-
assert result.instance_of?(name), "#{result} should be an instance of #{name}"
144+
def assert_instance_of(result, name)
145+
assert result.instance_of?(name), "#{result} should be an instance of #{name}"
146+
end
147+
end
144148
end
145149
end
146150
end

0 commit comments

Comments
 (0)