From dca286b0ec0a791d0e0937e1034247abf3c3ef64 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Mon, 17 Aug 2015 15:13:44 -0500 Subject: [PATCH] Lead by example: lint PORO model --- lib/active_model/serializer/lint.rb | 2 +- test/fixtures/poro.rb | 19 +++++++++++++------ test/poro_test.rb | 9 +++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 test/poro_test.rb diff --git a/lib/active_model/serializer/lint.rb b/lib/active_model/serializer/lint.rb index 1da9bc5bc..97ffcd7fd 100644 --- a/lib/active_model/serializer/lint.rb +++ b/lib/active_model/serializer/lint.rb @@ -106,7 +106,7 @@ def test_model_name private def resource - @resource + @resource or fail "'@resource' must be set as the linted object" end def assert_instance_of(result, name) diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 263971913..2b281049c 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -13,12 +13,8 @@ def cache_key "#{self.class.name.downcase}/#{self.id}-#{self.updated_at.strftime("%Y%m%d%H%M%S%9N")}" end - def cache_key_with_digest - "#{cache_key}/#{FILE_DIGEST}" - end - - def updated_at - @attributes[:updated_at] ||= DateTime.now.to_time + def serializable_hash(options = nil) + @attributes end def read_attribute_for_serialization(name) @@ -33,6 +29,9 @@ def id @attributes[:id] || @attributes['id'] || object_id end + ### Helper methods, not required to be serializable + # + # Convenience for adding @attributes readers and writers def method_missing(meth, *args) if meth.to_s =~ /^(.*)=$/ @attributes[$1.to_sym] = args[0] @@ -42,6 +41,14 @@ def method_missing(meth, *args) super end end + + def cache_key_with_digest + "#{cache_key}/#{FILE_DIGEST}" + end + + def updated_at + @attributes[:updated_at] ||= DateTime.now.to_time + end end class Profile < Model diff --git a/test/poro_test.rb b/test/poro_test.rb new file mode 100644 index 000000000..09191b8e6 --- /dev/null +++ b/test/poro_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class PoroTest < Minitest::Test + include ActiveModel::Serializer::Lint::Tests + + def setup + @resource = Model.new + end +end