-
Notifications
You must be signed in to change notification settings - Fork 420
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 #273 from Netflix/release-1.3
Release 1.3
- Loading branch information
Showing
17 changed files
with
647 additions
and
164 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
if defined?(::ActiveRecord) | ||
::ActiveRecord::Associations::Builder::HasOne.class_eval do | ||
# Based on | ||
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/collection_association.rb#L50 | ||
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/singular_association.rb#L11 | ||
def self.define_accessors(mixin, reflection) | ||
super | ||
name = reflection.name | ||
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 | ||
def #{name}_id | ||
# if an attribute is already defined with this methods name we should just use it | ||
return read_attribute(__method__) if has_attribute?(__method__) | ||
association(:#{name}).reader.try(:id) | ||
end | ||
CODE | ||
end | ||
::ActiveRecord::Associations::Builder::HasOne.class_eval do | ||
# Based on | ||
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/collection_association.rb#L50 | ||
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/builder/singular_association.rb#L11 | ||
def self.define_accessors(mixin, reflection) | ||
super | ||
name = reflection.name | ||
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 | ||
def #{name}_id | ||
# if an attribute is already defined with this methods name we should just use it | ||
return read_attribute(__method__) if has_attribute?(__method__) | ||
association(:#{name}).reader.try(:id) | ||
end | ||
CODE | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module FastJsonapi | ||
class Attribute | ||
attr_reader :key, :method, :conditional_proc | ||
|
||
def initialize(key:, method:, options: {}) | ||
@key = key | ||
@method = method | ||
@conditional_proc = options[:if] | ||
end | ||
|
||
def serialize(record, serialization_params, output_hash) | ||
if include_attribute?(record, serialization_params) | ||
output_hash[key] = if method.is_a?(Proc) | ||
method.arity.abs == 1 ? method.call(record) : method.call(record, serialization_params) | ||
else | ||
record.public_send(method) | ||
end | ||
end | ||
end | ||
|
||
def include_attribute?(record, serialization_params) | ||
if conditional_proc.present? | ||
conditional_proc.call(record, serialization_params) | ||
else | ||
true | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module FastJsonapi | ||
class Link | ||
attr_reader :key, :method | ||
|
||
def initialize(key:, method:) | ||
@key = key | ||
@method = method | ||
end | ||
|
||
def serialize(record, serialization_params, output_hash) | ||
output_hash[key] = if method.is_a?(Proc) | ||
method.arity == 1 ? method.call(record) : method.call(record, serialization_params) | ||
else | ||
record.public_send(method) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.