Skip to content

Commit

Permalink
Merge pull request #118 from RestPack/gj/hash-as-model
Browse files Browse the repository at this point in the history
models can now be hashes
  • Loading branch information
GavinJoyce committed Feb 4, 2015
2 parents 9ccc090 + 3c93153 commit 60cb5bf
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 @@ -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
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 @@ -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

0 comments on commit 60cb5bf

Please sign in to comment.