add unless conditional support
This commit is contained in:
parent
f8255771dc
commit
225bd22858
2
.idea/.gitignore
generated
vendored
Normal file
2
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/workspace.xml
|
||||||
6
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
.idea/inspectionProfiles/Project_Default.xml
generated
Normal 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
18
.idea/jsonapi-serializer.iml
generated
Normal 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
7
.idea/misc.xml
generated
Normal 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
8
.idea/modules.xml
generated
Normal 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
6
.idea/vcs.xml
generated
Normal 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>
|
||||||
30
lib/fast_jsonapi/conditional.rb
Normal file
30
lib/fast_jsonapi/conditional.rb
Normal 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
|
||||||
@ -4,10 +4,11 @@ require 'active_support/time'
|
|||||||
require 'active_support/concern'
|
require 'active_support/concern'
|
||||||
require 'active_support/inflector'
|
require 'active_support/inflector'
|
||||||
require 'active_support/core_ext/numeric/time'
|
require 'active_support/core_ext/numeric/time'
|
||||||
require 'fast_jsonapi/helpers'
|
|
||||||
require 'fast_jsonapi/attribute'
|
require 'fast_jsonapi/attribute'
|
||||||
require 'fast_jsonapi/relationship'
|
require 'fast_jsonapi/conditional'
|
||||||
|
require 'fast_jsonapi/helpers'
|
||||||
require 'fast_jsonapi/link'
|
require 'fast_jsonapi/link'
|
||||||
|
require 'fast_jsonapi/relationship'
|
||||||
require 'fast_jsonapi/serialization_core'
|
require 'fast_jsonapi/serialization_core'
|
||||||
|
|
||||||
module FastJsonapi
|
module FastJsonapi
|
||||||
@ -284,7 +285,7 @@ module FastJsonapi
|
|||||||
relationship_type: relationship_type,
|
relationship_type: relationship_type,
|
||||||
cached: options[:cached],
|
cached: options[:cached],
|
||||||
polymorphic: polymorphic,
|
polymorphic: polymorphic,
|
||||||
conditional_proc: options[:if],
|
conditional: Conditional.new(options.slice(*Conditional::CONDITIONAL_KEYS)),
|
||||||
transform_method: @transform_method,
|
transform_method: @transform_method,
|
||||||
meta: options[:meta],
|
meta: options[:meta],
|
||||||
links: options[:links],
|
links: options[:links],
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
module FastJsonapi
|
module FastJsonapi
|
||||||
class Relationship
|
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(
|
def initialize(
|
||||||
owner:,
|
owner:,
|
||||||
@ -14,7 +14,7 @@ module FastJsonapi
|
|||||||
relationship_type:,
|
relationship_type:,
|
||||||
cached: false,
|
cached: false,
|
||||||
polymorphic:,
|
polymorphic:,
|
||||||
conditional_proc:,
|
conditional:,
|
||||||
transform_method:,
|
transform_method:,
|
||||||
links:,
|
links:,
|
||||||
meta:,
|
meta:,
|
||||||
@ -31,7 +31,7 @@ module FastJsonapi
|
|||||||
@relationship_type = relationship_type
|
@relationship_type = relationship_type
|
||||||
@cached = cached
|
@cached = cached
|
||||||
@polymorphic = polymorphic
|
@polymorphic = polymorphic
|
||||||
@conditional_proc = conditional_proc
|
@conditional = conditional
|
||||||
@transform_method = transform_method
|
@transform_method = transform_method
|
||||||
@links = links || {}
|
@links = links || {}
|
||||||
@meta = meta || {}
|
@meta = meta || {}
|
||||||
@ -59,11 +59,7 @@ module FastJsonapi
|
|||||||
end
|
end
|
||||||
|
|
||||||
def include_relationship?(record, serialization_params)
|
def include_relationship?(record, serialization_params)
|
||||||
if conditional_proc.present?
|
conditional.allowed?(record: record, serialization_params: serialization_params)
|
||||||
FastJsonapi.call_proc(conditional_proc, record, serialization_params)
|
|
||||||
else
|
|
||||||
true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializer_for(record, serialization_params)
|
def serializer_for(record, serialization_params)
|
||||||
|
|||||||
@ -1,15 +1,17 @@
|
|||||||
|
require 'fast_jsonapi/conditional'
|
||||||
|
|
||||||
module FastJsonapi
|
module FastJsonapi
|
||||||
class Scalar
|
class Scalar
|
||||||
attr_reader :key, :method, :conditional_proc
|
attr_reader :key, :method, :conditional
|
||||||
|
|
||||||
def initialize(key:, method:, options: {})
|
def initialize(key:, method:, options: {})
|
||||||
@key = key
|
@key = key
|
||||||
@method = method
|
@method = method
|
||||||
@conditional_proc = options[:if]
|
@conditional = Conditional.new(options.slice(*Conditional::CONDITIONAL_KEYS))
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize(record, serialization_params, output_hash)
|
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)
|
if method.is_a?(Proc)
|
||||||
output_hash[key] = FastJsonapi.call_proc(method, record, serialization_params)
|
output_hash[key] = FastJsonapi.call_proc(method, record, serialization_params)
|
||||||
else
|
else
|
||||||
@ -17,13 +19,5 @@ module FastJsonapi
|
|||||||
end
|
end
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user