Skip to content

Allow has_one nil association #48

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 6 commits into from
Feb 7, 2018
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
2 changes: 1 addition & 1 deletion lib/extensions/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.define_accessors(mixin, reflection)
name = reflection.name
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name.to_s}_id
association(:#{name}).reader.id
association(:#{name}).reader.try(:id)
end
CODE
end
Expand Down
8 changes: 7 additions & 1 deletion spec/lib/extensions/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
# Insert records
@account_id = 2
@supplier_id = 1
@supplier_id_without_account = 3
db.execute_batch <<-SQL
insert into suppliers values ('Supplier1', #{@supplier_id});
insert into suppliers values ('Supplier1', #{@supplier_id}),
('SupplierWithoutAccount', #{@supplier_id_without_account});
insert into accounts values ('Dollar Account', #{@account_id}, #{@supplier_id});
SQL
end
Expand Down Expand Up @@ -58,6 +60,10 @@ class Account < ActiveRecord::Base
expect(Supplier.first.account_id).to eq @account_id
end

it 'has account_id method return nil if account not present' do
expect(Supplier.find(@supplier_id_without_account).account_id).to eq nil
end

end

# Clean up DB
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/object_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
expect(serializable_hash['data']['relationships']['owner']['data']).to be nil
end

it 'returns correct json when has_one returns nil' do
supplier.account_id = nil
json = SupplierSerializer.new(supplier).serialized_json
serializable_hash = JSON.parse(json)
expect(serializable_hash['data']['relationships']['account']['data']).to be nil
end

it 'returns correct json when serializing []' do
json = MovieSerializer.new([]).serialized_json
serializable_hash = JSON.parse(json)
Expand Down
35 changes: 35 additions & 0 deletions spec/shared/contexts/movie_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ class MovieType
attr_accessor :id, :name
end

class Supplier
attr_accessor :id, :account_id

def account
if account_id
a = Account.new
a.id = account_id
a
end
end
end

class Account
attr_accessor :id
end

# serializers
class MovieSerializer
include FastJsonapi::ObjectSerializer
Expand Down Expand Up @@ -79,6 +95,18 @@ class MovieTypeSerializer
set_type :movie_type
attributes :name
end

class SupplierSerializer
include FastJsonapi::ObjectSerializer
set_type :supplier
has_one :account
end

class AccountSerializer
include FastJsonapi::ObjectSerializer
set_type :account
belongs_to :supplier
end
end


Expand Down Expand Up @@ -151,6 +179,13 @@ class MovieSerializer
m
end

let(:supplier) do
s = Supplier.new
s.id = 1
s.account_id = 1
s
end

def build_movies(count)
count.times.map do |i|
m = Movie.new
Expand Down