Skip to content

Feature/persistence #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 18, 2015
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
4 changes: 2 additions & 2 deletions lib/jsonapi/consumer/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(klass, response)
end

def attributes(item)
item.except(:links)
item.except(:links, :type, :href)
end

def associations(item)
Expand Down Expand Up @@ -60,7 +60,7 @@ def linked
def build
_body.fetch(klass.json_key, []).collect do |attrs|
attrs_hash = attributes(attrs).merge(associations(attrs))
klass.new(attrs_hash)
klass.new(attrs_hash, false)
end
end

Expand Down
10 changes: 7 additions & 3 deletions lib/jsonapi/consumer/query/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ class Base
class << self
attr_accessor :request_method
end
attr_reader :klass, :headers, :path, :params
attr_reader :klass, :path, :headers, :params

def initialize(klass, payload)
@klass = klass
build_params(payload) if payload.is_a?(Hash) && payload.keys != [klass.primary_key]

@path = begin
if payload.is_a?(Hash) && payload.has_key?(klass.primary_key)
[klass.path, payload.delete(klass.primary_key)].join('/')
if payload.is_a?(Hash) && persisted? && payload.has_key?(klass.primary_key)
[klass.path, payload.fetch(klass.primary_key)].join('/')
else
klass.path
end
end
end

def persisted?
false
end

def build_params(args)
@params = args.dup
end
Expand Down
4 changes: 4 additions & 0 deletions lib/jsonapi/consumer/query/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class Delete < Base
def params
nil
end

def persisted?
true
end
end
end

4 changes: 4 additions & 0 deletions lib/jsonapi/consumer/query/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ def build_params(args)
{klass.primary_key => args}
end
end

def persisted?
true
end
end
end
4 changes: 4 additions & 0 deletions lib/jsonapi/consumer/query/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def build_params(args)
args = args.dup
@params = {klass.json_key => args.except(klass.primary_key)}
end

def persisted?
true
end
end
end

3 changes: 2 additions & 1 deletion lib/jsonapi/consumer/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def lookup_ancestors
end
end

def initialize(params={})
def initialize(params={}, new_record = true)
@new_record = new_record
(params || {}).slice(*association_names).each do |key, value|
send(:"#{key}=", value)
end
Expand Down
6 changes: 5 additions & 1 deletion lib/jsonapi/consumer/resource/attributes_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def update_attributes(attrs={})
end

def persisted?
!self.to_param.blank?
!new_record?
end

def new_record?
@new_record
end

def to_param
Expand Down
1 change: 1 addition & 0 deletions lib/jsonapi/consumer/resource/connection_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def save

if self.errors.empty?
self.attributes = results.first.attributes
@new_record = false
true
else
false
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/consumer/resource/serializer_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module SerializerConcern
extend ActiveSupport::Concern

def serializable_hash(options={})
@hash = persisted? ? attributes : attributes.except(self.class.primary_key)
@hash = self.to_param.blank? ? attributes.except(self.class.primary_key) : attributes

self.each_association do |name, association, options|
@hash[:links] ||= {}
Expand Down
47 changes: 42 additions & 5 deletions spec/jsonapi/consumer/attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
RSpec.describe 'Attributes' do
let(:test_class) do
Class.new do
AttrRecord ||= Class.new do
include JSONAPI::Consumer::Resource
self.host = 'http://localhost:3000/api/'
end
end

Expand All @@ -17,11 +18,47 @@
end
end

describe '#persisted?' do
it 'uses the primary key to decide' do
describe '#new_record? / #peristed?' do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got a typo here but I'll fix it up.

it 'changed only after saving' do
stub_request(:post, "http://localhost:3000/api/attr_records")
.with(headers: {accept: 'application/json', content_type: "application/json"})
.to_return(headers: {content_type: "application/json"}, status: 201, body: {
attr_records: [
{
type: :records,
id: '1',
name: "foobar.example",
created_at: "2014-10-16T18:49:40Z",
updated_at: "2014-10-18T18:59:40Z"
}
]
}.to_json)
obj.id = '8'
expect {
obj.id = '8'
}.to change{obj.persisted?}.from(false).to(true)
obj.save
}.to change{obj.new_record?}.from(true).to(false)
end
it 'is persisted after loading' do
stub_request(:get, "http://localhost:3000/api/attr_records/1")
.with(headers: {accept: 'application/json'})
.to_return(headers: {content_type: "application/json"}, body: {
attr_records: [
{type: :records, id: '1', name: "foobar.example"}
]
}.to_json)
stub_request(:get, "http://localhost:3000/api/attr_records")
.with(headers: {accept: 'application/json'})
.to_return(headers: {content_type: "application/json"}, body: {
attr_records: [
{type: :records, id: '1', name: "foo.example"},
{type: :records, id: '2', name: "bar.example"},
{type: :records, id: '3', name: "baz.example"}
]
}.to_json)
expect(test_class.all.all?{|record| record.new_record?}).to eq false
expect(test_class.all.all?{|record| record.persisted?}).to eq true
expect(test_class.find(1).first.new_record?).to eq false
expect(test_class.find(1).first.persisted?).to eq true
end
end
end
13 changes: 6 additions & 7 deletions spec/jsonapi/consumer/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end
end

let(:obj) { test_class.new(name: 'jsonapi.example') }
let(:obj) { test_class.new(name: 'jsonapi.example', id: 'client_provided_id') }

describe 'custom connection middleware' do

Expand Down Expand Up @@ -123,12 +123,11 @@
]
}.to_json)

obj.id = '1'
obj.updated_at = "2014-10-18T18:59:40Z"
expect(obj.updated_at).to eql("2014-10-18T18:59:40Z")

expect(obj.save).to eql(true)
expect(obj.updated_at).to eql("2016-10-18T18:59:40Z")
persisted_object = test_class.new({name: 'jsonapi.example', id: '1'}, false)
persisted_object.updated_at = "2014-10-18T18:59:40Z"
expect(persisted_object.updated_at).to eql("2014-10-18T18:59:40Z")
expect(persisted_object.save).to eql(true)
expect(persisted_object.updated_at).to eql("2016-10-18T18:59:40Z")
end
end

Expand Down