mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge pull request #1272 from bf4/add_public_poro_base
Add PORO serializable base class: ActiveModelSerializers::Model
This commit is contained in:
@@ -46,7 +46,7 @@ module ActionController
|
||||
|
||||
def test_render_skipping_adapter
|
||||
get :render_skipping_adapter
|
||||
assert_equal '{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}', response.body
|
||||
assert_equal '{"name":"Name 1","description":"Description 1","comments":"Comments 1"}', response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
9
test/active_model_serializers/model_test.rb
Normal file
9
test/active_model_serializers/model_test.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ActiveModelSerializers::ModelTest < Minitest::Test
|
||||
include ActiveModel::Serializer::Lint::Tests
|
||||
|
||||
def setup
|
||||
@resource = ActiveModelSerializers::Model.new
|
||||
end
|
||||
end
|
||||
@@ -36,7 +36,7 @@ module ActiveModel
|
||||
assert_equal({
|
||||
id: 42,
|
||||
tags: [
|
||||
{ 'attributes' => { 'id' => 1, 'name' => '#hash_tag' } }
|
||||
{ 'id' => 1, 'name' => '#hash_tag' }
|
||||
]
|
||||
}.to_json, adapter.serializable_hash[:post].to_json)
|
||||
end
|
||||
|
||||
44
test/fixtures/poro.rb
vendored
44
test/fixtures/poro.rb
vendored
@@ -1,44 +1,16 @@
|
||||
verbose = $VERBOSE
|
||||
$VERBOSE = nil
|
||||
class Model
|
||||
class Model < ActiveModelSerializers::Model
|
||||
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
|
||||
|
||||
def self.model_name
|
||||
@_model_name ||= ActiveModel::Name.new(self)
|
||||
end
|
||||
|
||||
def initialize(hash = {})
|
||||
@attributes = hash
|
||||
end
|
||||
|
||||
def cache_key
|
||||
"#{self.class.name.downcase}/#{self.id}-#{self.updated_at.strftime("%Y%m%d%H%M%S%9N")}"
|
||||
end
|
||||
|
||||
def serializable_hash(options = nil)
|
||||
@attributes
|
||||
end
|
||||
|
||||
def read_attribute_for_serialization(name)
|
||||
if name == :id || name == 'id'
|
||||
id
|
||||
else
|
||||
@attributes[name]
|
||||
end
|
||||
end
|
||||
|
||||
def id
|
||||
@attributes[:id] || @attributes['id'] || object_id
|
||||
end
|
||||
|
||||
### Helper methods, not required to be serializable
|
||||
#
|
||||
# Convenience for adding @attributes readers and writers
|
||||
|
||||
# Convenience when not adding @attributes readers and writers
|
||||
def method_missing(meth, *args)
|
||||
if meth.to_s =~ /^(.*)=$/
|
||||
@attributes[$1.to_sym] = args[0]
|
||||
elsif @attributes.key?(meth)
|
||||
@attributes[meth]
|
||||
attributes[$1.to_sym] = args[0]
|
||||
elsif attributes.key?(meth)
|
||||
attributes[meth]
|
||||
else
|
||||
super
|
||||
end
|
||||
@@ -47,10 +19,6 @@ class Model
|
||||
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
|
||||
|
||||
@@ -24,6 +24,9 @@ module ActiveModel
|
||||
def id
|
||||
end
|
||||
|
||||
def updated_at
|
||||
end
|
||||
|
||||
def self.model_name
|
||||
@_model_name ||= ActiveModel::Name.new(self)
|
||||
end
|
||||
|
||||
@@ -54,7 +54,7 @@ module ActiveModel
|
||||
|
||||
assert_equal key, :tags
|
||||
assert_equal serializer, nil
|
||||
assert_equal [{ attributes: { name: '#hashtagged' } }].to_json, options[:virtual_value].to_json
|
||||
assert_equal [{ name: '#hashtagged' }].to_json, options[:virtual_value].to_json
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user