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

Conditional fields with key format #123

Open
wants to merge 2 commits into
base: master
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
11 changes: 11 additions & 0 deletions lib/jsonapi/serializable/resource/conditional_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def inherited(klass)
#
def attribute(name, options = {}, &block)
super
name = _format_key_for_condition(name)
_register_condition(field_condition_blocks, name, options)
end

Expand All @@ -57,6 +58,7 @@ def attribute(name, options = {}, &block)
#
def relationship(name, options = {}, &block)
super
name = _format_key_for_condition(name)
_register_condition(field_condition_blocks, name, options)
end

Expand Down Expand Up @@ -87,6 +89,15 @@ def _register_condition(condition_blocks, name, options)
proc { !instance_exec(&options[:unless]) }
end
end

def _format_key_for_condition(name)
ancestors = self.singleton_class.included_modules
if ancestors.include?(KeyFormat) && ancestors.index(KeyFormat) > ancestors.index(ConditionalFields)
_key_formatter.call(name)
else
name
end
end
end

module InstanceMethods
Expand Down
168 changes: 168 additions & 0 deletions spec/resource/conditional_fields_with_key_format_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
require 'spec_helper'

describe JSONAPI::Serializable::Resource do
let(:klass) do
Class.new(JSONAPI::Serializable::Resource) do
type 'foo'
id { 'bar' }
end
end

let(:object) { User.new }
let(:resource) do
klass.new(object: object, conditional: conditional)
end

context 'when the attribute is conditional and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields
end
end

subject { resource.as_jsonapi[:attributes] }

context 'via :if' do
before do
klass.class_eval do
attribute :name, if: proc { @conditional } do
'foo'
end
end
end

context 'and the clause is true' do
let(:conditional) { true }

it { is_expected.to eq(NAME: 'foo') }
end

context 'and the clause is false' do
let(:conditional) { false }

it { is_expected.to be_nil }
end
end

context 'via :unless' do
before do
klass.class_eval do
attribute :name, unless: proc { @conditional } do
'foo'
end
end
end

context 'and the clause is true' do
let(:conditional) { true }

it { is_expected.to be_nil }
end

context 'and the clause is false' do
let(:conditional) { false }

it { is_expected.to eq(NAME: 'foo') }
end
end
end

context 'when relationship is conditional and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields

relationship :posts, if: -> { false }
end
end

subject { resource.as_jsonapi[:relationships] }

context 'and the clause is false' do
let(:conditional) { false }

it { is_expected.to be_nil }
end
end

context 'when a link is conditional and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields

link :self, if: proc { @conditional } do
'https://example.com/users/42'
end
end
end

subject { resource.as_jsonapi[:links] }

context 'and the clause is true' do
let(:conditional) { true }

it { is_expected.to eq(self: 'https://example.com/users/42') }
end

context 'and the clause is false' do
let(:conditional) { false }

it { is_expected.to be_nil }
end
end

context 'when inheriting and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields

relationship :posts, if: -> { false }
end
end

let(:subclass) { Class.new(klass) }
let(:resource) { subclass.new(object: object) }

subject { resource.as_jsonapi[:relationships] }

context 'and the clause is false' do
let(:conditional) { false }

it { is_expected.to be_nil }
end
end

context 'when a field and a link have the same name and keys are formatted' do
before do
klass.class_eval do
extend JSONAPI::Serializable::Resource::KeyFormat
key_format -> (k) { k.to_s.upcase }
extend JSONAPI::Serializable::Resource::ConditionalFields

attribute :name, if: proc { @conditional } do
'attribute'
end

link :name, unless: proc { @conditional } do
'link'
end
end
end

let(:conditional) { true }
subject { resource.as_jsonapi }

it "doesn't override previously registered condition" do
expect(subject[:attributes]).to eq(NAME: 'attribute')
expect(subject).not_to have_key(:links)
end
end
end