-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from gadabout/feature/remove-related
Remove related resources for has-many relationship
- Loading branch information
Showing
9 changed files
with
85 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Lurch | ||
class PayloadBuilder | ||
def initialize(input, resource_identifier_only = false) | ||
@input = input | ||
@resource_identifier_only = resource_identifier_only | ||
end | ||
|
||
def build | ||
{ "data" => data } | ||
end | ||
|
||
private | ||
|
||
def data | ||
if @input.is_a?(Enumerable) | ||
@input.map { |resource| resource_object_for(resource) } | ||
else | ||
resource_object_for(@input) | ||
end | ||
end | ||
|
||
def resource_object_for(resource) | ||
{ | ||
"id" => resource.id, | ||
"type" => Inflecto.dasherize(resource.type.to_s), | ||
"attributes" => attributes_for(resource) | ||
}.reject { |_, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) } | ||
end | ||
|
||
def attributes_for(resource) | ||
return {} if @resource_identifier_only | ||
dasherize_keys(resource.attributes) | ||
end | ||
|
||
def dasherize_keys(attributes) | ||
attributes.each_with_object({}) do |(key, value), hash| | ||
hash[Inflecto.dasherize(key.to_s)] = value | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Lurch | ||
VERSION = "0.0.10".freeze | ||
VERSION = "0.0.11".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,5 @@ | ||
RSpec.describe Lurch::Changeset do | ||
let(:type) { :people } | ||
let(:type_or_resource) { type } | ||
let(:changes) { {} } | ||
let(:changeset) { Lurch::Changeset.new(type_or_resource, changes) } | ||
|
||
describe "#payload" do | ||
subject(:payload) { changeset.payload } | ||
|
||
context "when given a type" do | ||
it { is_expected.to include("type" => type) } | ||
it { is_expected.not_to include("id") } | ||
end | ||
|
||
context "when given a resource" do | ||
let(:type_or_resource) { Lurch::Resource.new(nil, type, 1) } | ||
|
||
it { is_expected.to include("type" => type) } | ||
it { is_expected.to include("id" => 1) } | ||
end | ||
|
||
context "when given some changes" do | ||
let(:name) { "Mr. Example" } | ||
let(:changes) { { name: name } } | ||
|
||
it { is_expected.to include("data" => { "attributes" => { "name" => name } }) } | ||
end | ||
|
||
context "when given changes with underscored keys" do | ||
let(:email_address) { "[email protected]" } | ||
let(:changes) { { email_address: email_address } } | ||
|
||
it "dasherizes the keys" do | ||
expect(payload).to include("data" => { "attributes" => { "email-address" => email_address } }) | ||
end | ||
end | ||
end | ||
let(:changeset) { Lurch::Changeset.new(:people) } | ||
|
||
describe "#inspect" do | ||
subject { changeset.inspect } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters