Skip to content
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

Allow "if: Proc..." on link #442

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
30 changes: 3 additions & 27 deletions lib/fast_jsonapi/attribute.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
module FastJsonapi
class Attribute
attr_reader :key, :method, :conditional_proc

def initialize(key:, method:, options: {})
@key = key
@method = method
@conditional_proc = options[:if]
end
require 'fast_jsonapi/scalar'

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
module FastJsonapi
class Attribute < Scalar; end
end
19 changes: 3 additions & 16 deletions lib/fast_jsonapi/link.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
module FastJsonapi
class Link
attr_reader :key, :method

def initialize(key:, method:)
@key = key
@method = method
end
require 'fast_jsonapi/scalar'

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
module FastJsonapi
class Link < Scalar; end
end
11 changes: 8 additions & 3 deletions lib/fast_jsonapi/object_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,19 @@ def fetch_polymorphic_option(options)
{}
end

def link(link_name, link_method_name = nil, &block)
# def link(link_name, link_method_name = nil, &block)
def link(*params, &block)
self.data_links = {} if self.data_links.nil?
link_method_name = link_name if link_method_name.nil?

options = params.last.is_a?(Hash) ? params.pop : {}
link_name = params.first
link_method_name = params[-1]
key = run_key_transform(link_name)

self.data_links[key] = Link.new(
key: key,
method: block || link_method_name
method: block || link_method_name,
options: options
)
end

Expand Down
29 changes: 29 additions & 0 deletions lib/fast_jsonapi/scalar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module FastJsonapi
class Scalar
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 conditionally_allowed?(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 conditionally_allowed?(record, serialization_params)
if conditional_proc.present?
conditional_proc.call(record, serialization_params)
else
true
end
end
end
end
20 changes: 20 additions & 0 deletions spec/lib/object_serializer_class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,26 @@ def year_since_release_calculator(release_year)
expect(action_serializable_hash[:data][:links][:url]).to eq "/action-movie/#{movie.id}"
end
end

describe 'optional links' do
subject(:downloadable_serializable_hash) { OptionalDownloadableMovieSerializer.new(movie, params).serializable_hash }

context 'when the link is provided' do
let(:params) { { params: { signed_url: signed_url } } }
let(:signed_url) { 'http://example.com/download_link?signature=abcdef' }

it 'includes the link' do
expect(downloadable_serializable_hash[:data][:links][:download]).to eq signed_url
end
end

context 'when the link is not provided' do
let(:params) { { params: {} } }
it 'does not include the link' do
expect(downloadable_serializable_hash[:data][:links]).to_not have_key(:download)
end
end
end
end

describe '#key_transform' do
Expand Down
7 changes: 7 additions & 0 deletions spec/shared/contexts/movie_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ class HorrorMovieSerializer < GenreMovieSerializer
link(:url) { |object| "/horror-movie/#{object.id}" }
end

class OptionalDownloadableMovieSerializer < MovieSerializer
link(:download, if: Proc.new { |record, params| params && params[:signed_url] }) do |movie, params|
params[:signed_url]
end
end

class MovieWithoutIdStructSerializer
include FastJsonapi::ObjectSerializer
attributes :name, :release_year
Expand Down Expand Up @@ -385,6 +391,7 @@ class MovieSerializer
ActionMovieSerializer
GenreMovieSerializer
HorrorMovieSerializer
OptionalDownloadableMovieSerializer
Movie
MovieSerializer
Actor
Expand Down