Skip to content

Commit

Permalink
Merge 3b0f5a0 into 2e31a14
Browse files Browse the repository at this point in the history
  • Loading branch information
mmosche2 committed Mar 31, 2014
2 parents 2e31a14 + 3b0f5a0 commit 1468e21
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ test/version_tmp
tmp
*.swp
.ruby-version
.ruby-gemset
.idea
13 changes: 9 additions & 4 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def serializer_for(resource)
end
end

attr_accessor :_root, :_attributes, :_associations
attr_accessor :_root, :_attributes, :_associations, :_serialize_all_attributes
alias root _root=
alias root= _root=

Expand All @@ -71,6 +71,10 @@ def root_name
def attributes(*attrs)
@_attributes.concat attrs

if attrs.empty?
@_serialize_all_attributes = true
end

attrs.each do |attr|
define_method attr do
object.read_attribute_for_serialization attr
Expand Down Expand Up @@ -121,10 +125,11 @@ def json_key
end
end

def attributes
filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
def attributes(object=nil)
attribute_hash = filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
hash[name] = send(name)
end
self.class._serialize_all_attributes && object && object.respond_to?(:attributes) ? object.attributes : attribute_hash
end

def associations
Expand Down Expand Up @@ -192,7 +197,7 @@ def serialize_ids(association)

def serializable_object(options={})
return @wrap_in_array ? [] : nil if @object.nil?
hash = attributes
hash = attributes(@object)
hash.merge! associations
@wrap_in_array ? [hash] : hash
end
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def profile
class Profile < Model
end

# Allows us to test ActiveRecord::Base-like attributes capability
class ProfileWithAttributes < Profile
attr_accessor :attributes
end

class Post < Model
def comments
@comments ||= [Comment.new(content: 'C1'),
Expand Down Expand Up @@ -53,6 +58,13 @@ def description
attributes :name, :description
end

class ProfileAllAttributesSerializer < ActiveModel::Serializer
attributes
end

class ProfileNoAttributesSerializer < ActiveModel::Serializer
end

class PostSerializer < ActiveModel::Serializer
attributes :title, :body

Expand Down
16 changes: 16 additions & 0 deletions test/unit/active_model/serializer/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ def test_attributes_definition
@profile_serializer.class._attributes)
end

def test_all_attributes_definition
initial_attributes = { name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }
@profile_with_attributes = ProfileWithAttributes.new(initial_attributes)
@profile_all_attributes_serializer = ProfileAllAttributesSerializer.new(@profile_with_attributes)
serialized_attributes = @profile_all_attributes_serializer.as_json
assert_equal({"profile_all_attributes" => initial_attributes}, serialized_attributes)
end

def test_no_attributes_definition
initial_attributes = { name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }
@profile_with_attributes = ProfileWithAttributes.new(initial_attributes)
@profile_no_attributes_serializer = ProfileNoAttributesSerializer.new(@profile_with_attributes)
serialized_attributes = @profile_no_attributes_serializer.as_json
assert_equal({"profile_no_attributes" => {}}, serialized_attributes)
end

def test_attributes_serialization_using_serializable_hash
assert_equal({
name: 'Name 1', description: 'Description 1'
Expand Down

0 comments on commit 1468e21

Please sign in to comment.