Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,5 @@ on:
jobs:
test:
name: Run Tests
runs-on: ubuntu-20.04
strategy:
matrix:
ruby: [2.7, 3.0.3]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the Test Suite
run: bundle exec rspec --color --require spec_helper --format progress spec
uses: upfluence/actions/.github/workflows/lib-ruby-test.yml@master
secrets: inherit
14 changes: 7 additions & 7 deletions lib/userializer/base_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def initialize(obj, opts = {})
def serializable_hash(opts)
res = {}

attributes.each { |attr| attr.merge_attributes(res, self, opts) }
relations.each do |rel|
_attributes.each { |attr| attr.merge_attributes(res, self, opts) }
_relations.each do |rel|
rel.merge_attributes(res, self, opts)
end

Expand All @@ -78,7 +78,7 @@ def merge_root(res, key, single, opts)
end
end

relations.each { |rel| rel.merge_root(res, self, opts) }
_relations.each { |rel| rel.merge_root(res, self, opts) }
end

def to_hash
Expand All @@ -99,14 +99,14 @@ def method_missing(mth); @obj.send(mth); end

private

def attributes
@attributes ||= (self.class.attrs || {}).values.select do |attr|
def _attributes
@_attributes ||= (self.class.attrs || {}).values.select do |attr|
allow?(attr.key)
end
end

def relations
@relations ||= (self.class.relations || {}).values.select do |rel|
def _relations
@_relations ||= (self.class.relations || {}).values.select do |rel|
allow?(rel.key)
end
end
Expand Down
32 changes: 30 additions & 2 deletions spec/userializer/basic_serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ class FooSubSerializer < FooSerializer
end

class FooSkipNilSerializer < USerializer::BaseSerializer
attributes :bazs
attributes :bar, skip_nil: true
attributes :bazs
attributes :bar, skip_nil: true
end

class AttributesRelations
attr_accessor :id, :relations, :attributes
end

class AttributesRelationsSerializer < USerializer::BaseSerializer
attributes :relations, :attributes
end

RSpec.describe USerializer::BaseSerializer do
Expand Down Expand Up @@ -121,4 +129,24 @@ class FooSkipNilSerializer < USerializer::BaseSerializer
)
end
end

context 'attributes && relations' do
let(:f) do
f = AttributesRelations.new
f.id = 1
f.attributes = 'foo'
f.relations = 'bar'
f
end

it do
expect(AttributesRelationsSerializer.new(f).to_hash).to eq(
attributes_relations: {
id: 1,
attributes: 'foo',
relations: 'bar'
}
)
end
end
end