Skip to content

Commit

Permalink
models can now be hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinJoyce committed Feb 4, 2015
1 parent 5fea5c8 commit 3c93153
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/restpack_serializer/serializable/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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
Expand Down
11 changes: 11 additions & 0 deletions spec/serializable/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,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

0 comments on commit 3c93153

Please sign in to comment.