Merge pull request #92 from twinturbo/query-attributes

Close #86
This commit is contained in:
José Valim 2012-07-14 04:16:07 -07:00
commit 3e87c6414d
2 changed files with 45 additions and 1 deletions

View File

@ -263,7 +263,7 @@ module ActiveModel
end
def attribute(attr, options={})
self._attributes = _attributes.merge(attr => options[:key] || attr)
self._attributes = _attributes.merge(attr => options[:key] || attr.to_s.gsub(/\?$/, '').to_sym)
unless method_defined?(attr)
class_eval "def #{attr}() object.read_attribute_for_serialization(:#{attr}) end", __FILE__, __LINE__

View File

@ -908,4 +908,48 @@ class SerializerTest < ActiveModel::TestCase
end
assert_equal ActiveModel::Serializer, loaded
end
def tests_query_attributes_strip_question_mark
todo = Class.new do
def overdue?
true
end
def read_attribute_for_serialization(name)
send name
end
end
serializer = Class.new(ActiveModel::Serializer) do
attribute :overdue?
end
actual = serializer.new(todo.new).as_json
assert_equal({
:overdue => true
}, actual)
end
def tests_query_attributes_allow_key_option
todo = Class.new do
def overdue?
true
end
def read_attribute_for_serialization(name)
send name
end
end
serializer = Class.new(ActiveModel::Serializer) do
attribute :overdue?, :key => :foo
end
actual = serializer.new(todo.new).as_json
assert_equal({
:foo => true
}, actual)
end
end