Skip to content

Commit

Permalink
Merge pull request #255 from ysyyork/active_model_serializer_support
Browse files Browse the repository at this point in the history
make it compatible with active_model_serializer
  • Loading branch information
rafaelfranca committed Jun 12, 2017
2 parents 84e5694 + 8b92916 commit 62d7c8d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/active_resource/base.rb
Expand Up @@ -1487,6 +1487,14 @@ def to_xml(options={})
super({ :root => self.class.element_name }.merge(options))
end

def read_attribute_for_serialization(n)
if !attributes[n].nil?
attributes[n]
elsif respond_to?(n)
send(n)
end
end

protected
def connection(refresh = false)
self.class.connection(refresh)
Expand Down Expand Up @@ -1539,10 +1547,6 @@ def collection_path(options = nil)

private

def read_attribute_for_serialization(n)
attributes[n]
end

# Determine whether the response is allowed to have a body per HTTP 1.1 spec section 4.4.1
def response_code_allows_body?(c)
!((100..199).include?(c) || [204,304].include?(c))
Expand Down
18 changes: 18 additions & 0 deletions test/cases/base_test.rb
Expand Up @@ -1250,6 +1250,24 @@ def test_exists_with_204_no_content
assert Person.exists?(1)
end

def test_read_attribute_for_serialization
joe = Person.find(6)
joe.singleton_class.class_eval do
def non_attribute_field
'foo'
end

def id
'bar'
end
end

assert_equal joe.read_attribute_for_serialization(:id), 6
assert_equal joe.read_attribute_for_serialization(:name), 'Joe'
assert_equal joe.read_attribute_for_serialization(:likes_hats), true
assert_equal joe.read_attribute_for_serialization(:non_attribute_field), 'foo'
end

def test_to_xml
Person.format = :xml
matz = Person.find(1)
Expand Down

0 comments on commit 62d7c8d

Please sign in to comment.