Skip to content

Commit dca286b

Browse files
committed
Lead by example: lint PORO model
1 parent e7d3323 commit dca286b

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

lib/active_model/serializer/lint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_model_name
106106
private
107107

108108
def resource
109-
@resource
109+
@resource or fail "'@resource' must be set as the linted object"
110110
end
111111

112112
def assert_instance_of(result, name)

test/fixtures/poro.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ def cache_key
1313
"#{self.class.name.downcase}/#{self.id}-#{self.updated_at.strftime("%Y%m%d%H%M%S%9N")}"
1414
end
1515

16-
def cache_key_with_digest
17-
"#{cache_key}/#{FILE_DIGEST}"
18-
end
19-
20-
def updated_at
21-
@attributes[:updated_at] ||= DateTime.now.to_time
16+
def serializable_hash(options = nil)
17+
@attributes
2218
end
2319

2420
def read_attribute_for_serialization(name)
@@ -33,6 +29,9 @@ def id
3329
@attributes[:id] || @attributes['id'] || object_id
3430
end
3531

32+
### Helper methods, not required to be serializable
33+
#
34+
# Convenience for adding @attributes readers and writers
3635
def method_missing(meth, *args)
3736
if meth.to_s =~ /^(.*)=$/
3837
@attributes[$1.to_sym] = args[0]
@@ -42,6 +41,14 @@ def method_missing(meth, *args)
4241
super
4342
end
4443
end
44+
45+
def cache_key_with_digest
46+
"#{cache_key}/#{FILE_DIGEST}"
47+
end
48+
49+
def updated_at
50+
@attributes[:updated_at] ||= DateTime.now.to_time
51+
end
4552
end
4653

4754
class Profile < Model

test/poro_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'test_helper'
2+
3+
class PoroTest < Minitest::Test
4+
include ActiveModel::Serializer::Lint::Tests
5+
6+
def setup
7+
@resource = Model.new
8+
end
9+
end

0 commit comments

Comments
 (0)