add unless conditional support

This commit is contained in:
Andriy Byalyk 2020-10-04 14:41:06 +03:00
parent f8255771dc
commit 225bd22858
10 changed files with 90 additions and 22 deletions

2
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="Rubocop" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>

18
.idea/jsonapi-serializer.iml generated Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RUBY_MODULE" version="4">
<component name="ModuleRunConfigurationManager">
<shared />
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.17.2, ruby-2.6.3-p62) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rake (v12.3.2, ruby-2.6.3-p62) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="sqlite3 (v1.3.13, ruby-2.6.3-p62) [gem]" level="application" />
</component>
</module>

7
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="ruby-2.6.3-p62" project-jdk-type="RUBY_SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/jsonapi-serializer.iml" filepath="$PROJECT_DIR$/.idea/jsonapi-serializer.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,30 @@
module FastJsonapi
class Conditional
CONDITIONAL_KEYS = %i[if unless].freeze
def initialize(conditional_params)
@if_condition = conditional_params[:if]
@unless_condition = conditional_params[:unless]
end
def allowed?(record:, serialization_params:)
if_allowed?(record, serialization_params) && unless_allowed?(record, serialization_params)
end
private
attr_reader :if_condition, :unless_condition
def if_allowed?(record, serialization_params)
if_condition.present? ? check_condition?(if_condition, record, serialization_params) : true
end
def unless_allowed?(record, serialization_params)
unless_condition.present? ? !check_condition?(unless_condition, record, serialization_params) : true
end
def check_condition?(condition, record, serialization_params)
FastJsonapi.call_proc(condition, record, serialization_params)
end
end
end

View File

@ -4,10 +4,11 @@ require 'active_support/time'
require 'active_support/concern'
require 'active_support/inflector'
require 'active_support/core_ext/numeric/time'
require 'fast_jsonapi/helpers'
require 'fast_jsonapi/attribute'
require 'fast_jsonapi/relationship'
require 'fast_jsonapi/conditional'
require 'fast_jsonapi/helpers'
require 'fast_jsonapi/link'
require 'fast_jsonapi/relationship'
require 'fast_jsonapi/serialization_core'
module FastJsonapi
@ -284,7 +285,7 @@ module FastJsonapi
relationship_type: relationship_type,
cached: options[:cached],
polymorphic: polymorphic,
conditional_proc: options[:if],
conditional: Conditional.new(options.slice(*Conditional::CONDITIONAL_KEYS)),
transform_method: @transform_method,
meta: options[:meta],
links: options[:links],

View File

@ -1,6 +1,6 @@
module FastJsonapi
class Relationship
attr_reader :owner, :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional_proc, :transform_method, :links, :meta, :lazy_load_data
attr_reader :owner, :key, :name, :id_method_name, :record_type, :object_method_name, :object_block, :serializer, :relationship_type, :cached, :polymorphic, :conditional, :transform_method, :links, :meta, :lazy_load_data
def initialize(
owner:,
@ -14,7 +14,7 @@ module FastJsonapi
relationship_type:,
cached: false,
polymorphic:,
conditional_proc:,
conditional:,
transform_method:,
links:,
meta:,
@ -31,7 +31,7 @@ module FastJsonapi
@relationship_type = relationship_type
@cached = cached
@polymorphic = polymorphic
@conditional_proc = conditional_proc
@conditional = conditional
@transform_method = transform_method
@links = links || {}
@meta = meta || {}
@ -59,11 +59,7 @@ module FastJsonapi
end
def include_relationship?(record, serialization_params)
if conditional_proc.present?
FastJsonapi.call_proc(conditional_proc, record, serialization_params)
else
true
end
conditional.allowed?(record: record, serialization_params: serialization_params)
end
def serializer_for(record, serialization_params)

View File

@ -1,15 +1,17 @@
require 'fast_jsonapi/conditional'
module FastJsonapi
class Scalar
attr_reader :key, :method, :conditional_proc
attr_reader :key, :method, :conditional
def initialize(key:, method:, options: {})
@key = key
@method = method
@conditional_proc = options[:if]
@conditional = Conditional.new(options.slice(*Conditional::CONDITIONAL_KEYS))
end
def serialize(record, serialization_params, output_hash)
if conditionally_allowed?(record, serialization_params)
if conditional.allowed?(record: record, serialization_params: serialization_params)
if method.is_a?(Proc)
output_hash[key] = FastJsonapi.call_proc(method, record, serialization_params)
else
@ -17,13 +19,5 @@ module FastJsonapi
end
end
end
def conditionally_allowed?(record, serialization_params)
if conditional_proc.present?
FastJsonapi.call_proc(conditional_proc, record, serialization_params)
else
true
end
end
end
end