Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from maddiesch/ms/nested-attributes
Browse files Browse the repository at this point in the history
Add support for nested params
  • Loading branch information
Maddie committed Nov 21, 2018
2 parents ae07852 + fed9ddd commit 91f063a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Version 1.0.1

## Released November 21, 2018

### Changes

- Fix for nested complex attributes.

# Version 1.0.0

## Released November 15, 2018
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018 Skylar Schipper
Copyright (c) 2018 Madison Schipper

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 1 deletion lib/onsi/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def parse_json(body, attributes = [], relationships = [])
def permit_attributes(data, attributes)
return {} if Array(attributes).empty?

data.require(:attributes).permit(*attributes)
normalized_attributes = attributes.map(&:to_sym)

data.require(:attributes).permit!.to_h.select { |k, _| normalized_attributes.include?(k.to_sym) }
end

def permit_relationships(data, relationships)
Expand Down
2 changes: 1 addition & 1 deletion lib/onsi/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Onsi
##
# The current version of Onsi
VERSION = '1.0.0'.freeze
VERSION = '1.0.1'.freeze
end
28 changes: 16 additions & 12 deletions spec/onsi/params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
data: {
type: 'person',
attributes: {
name: 'Skylar',
foo: 'Bar'
name: 'Madison',
foo: 'Bar',
nicknames: [
'Maddie',
'Mads'
]
},
relationships: {
person: {
Expand All @@ -29,11 +33,11 @@

describe '.parse' do
context 'given valid params' do
subject { described_class.parse(params, [:name], %i[person access_tokens]) }
subject { described_class.parse(params, %i[name nicknames], %i[person access_tokens]) }

it { expect { subject }.to_not raise_error }

it { expect(subject.attributes).to eq('name' => 'Skylar') }
it { expect(subject.attributes).to eq('name' => 'Madison', 'nicknames' => %w[Maddie Mads]) }

it { expect(subject.relationships).to eq(person_id: '7', access_token_ids: %w[1 2]) }
end
Expand All @@ -44,7 +48,7 @@
data: {
type: 'person',
attributes: {
name: 'Skylar',
name: 'Maddie',
foo: 'Bar'
},
relationships: {
Expand Down Expand Up @@ -75,7 +79,7 @@
data: {
type: 'person',
attributes: {
name: 'Skylar',
name: 'Maddie',
foo: 'Bar'
},
relationships: {
Expand Down Expand Up @@ -106,7 +110,7 @@
data: {
type: 'person',
attributes: {
name: 'Skylar'
name: 'Maddie'
},
relationships: {
person: {
Expand Down Expand Up @@ -173,7 +177,7 @@

it 'merges attributes & relationships' do
expect(subject.flatten).to eq(
'name' => 'Skylar',
'name' => 'Madison',
'person_id' => '7',
'access_token_ids' => %w[1 2]
)
Expand All @@ -184,8 +188,8 @@
subject { described_class.parse(params, [:name], %i[person access_tokens]) }

it 'returns a valid attribute' do
expect(subject.require(:name)).to eq 'Skylar'
expect(subject.require('name')).to eq 'Skylar'
expect(subject.require(:name)).to eq 'Madison'
expect(subject.require('name')).to eq 'Madison'
end

it 'raises on missing key' do
Expand All @@ -199,7 +203,7 @@
subject { described_class.parse(params, [:name], %i[person access_tokens]) }

it 'returns a valid attribute' do
expect(subject.fetch(:name)).to eq 'Skylar'
expect(subject.fetch(:name)).to eq 'Madison'
end

it 'returns nil' do
Expand Down Expand Up @@ -236,7 +240,7 @@

it 'runs through the transform block' do
subject.transform(:name) { |name| "tested transform #{name}" }
expect(subject.flatten[:name]).to eq 'tested transform Skylar'
expect(subject.flatten[:name]).to eq 'tested transform Madison'
end
end

Expand Down

0 comments on commit 91f063a

Please sign in to comment.