diff --git a/lib/restpack_serializer/serializable/attributes.rb b/lib/restpack_serializer/serializable/attributes.rb index 321232e..e55fccc 100644 --- a/lib/restpack_serializer/serializable/attributes.rb +++ b/lib/restpack_serializer/serializable/attributes.rb @@ -48,7 +48,11 @@ def define_attribute_method(name) unless method_defined?(name) define_method name do value = self.default_href if name == :href - value ||= @model.send(name) + if @model.is_a?(Hash) + value ||= @model[name] || @model[name.to_s] + else + value ||= @model.send(name) + end value = value.to_s if name == :id value end diff --git a/spec/serializable/attributes_spec.rb b/spec/serializable/attributes_spec.rb index b934643..c7389cc 100644 --- a/spec/serializable/attributes_spec.rb +++ b/spec/serializable/attributes_spec.rb @@ -55,4 +55,15 @@ class CustomSerializer expect(as_json[:gonzaga]).to eq('is a school') end end + + describe "model as a hash" do + let(:model) { { a: 'A', 'b' => 'B' } } + + subject(:as_json) { CustomSerializer.as_json(model, include_gonzaga?: false) } + + it 'uses the transform method on the model attribute' do + expect(as_json[:a]).to eq('A') + expect(as_json[:b]).to eq('B') + end + end end