Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lead by example: lint PORO model #1063

Merged
merged 1 commit into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_model/serializer/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 13 additions & 6 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, this method was missing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

end

def read_attribute_for_serialization(name)
Expand All @@ -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]
Expand All @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really be Time.now. We can be pretty confident we're using the Gregorian calendar, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind keeping it 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't change it in this PR. :) OT: In 1.8 and 1.9 I think DateTime was much slower than Time. Now, I think avoiding DateTime is mostly about it being the wrong tool and being weird with timezones sometimes...

end
end

class Profile < Model
Expand Down
9 changes: 9 additions & 0 deletions test/poro_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class PoroTest < Minitest::Test
include ActiveModel::Serializer::Lint::Tests

def setup
@resource = Model.new
end
end