Skip to content

Commit

Permalink
change config to enforce_all_conditions
Browse files Browse the repository at this point in the history
Signed-off-by: alejandroereyes <[email protected]>
  • Loading branch information
alejandroereyes committed Sep 20, 2023
1 parent 75a5b12 commit f96ac5e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -670,15 +670,15 @@ Output:


Both the `field` and the global Blueprinter Configuration supports `:if` and `:unless` options that can be used to serialize fields conditionally.
By default, field-level conditions override global conditions. By enabling `roll_up_conditions`, BOTH field-level and global conditions must be met in order for the field to be serialized.
By default, field-level conditions override global conditions. By enabling `enforce_all_conditions`, BOTH field-level and global conditions must be met in order for the field to be serialized.

### Global Config Setting - if and unless

```ruby
Blueprinter.configure do |config|
config.if = ->(field_name, obj, _options) { !obj[field_name].nil? }
config.unless = ->(field_name, obj, _options) { obj[field_name].nil? }
config.roll_up_conditions = true
config.enforce_all_conditions = true
end
```

Expand Down
4 changes: 2 additions & 2 deletions lib/blueprinter/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Blueprinter
class Configuration
attr_accessor :association_default, :datetime_format, :deprecations, :field_default, :generator, :if, :method,
:sort_fields_by, :unless, :extractor_default, :default_transformers, :custom_array_like_classes,
:roll_up_conditions
:enforce_all_conditions

VALID_CALLABLES = %i[if unless].freeze

Expand All @@ -21,7 +21,7 @@ def initialize
@extractor_default = AutoExtractor
@default_transformers = []
@custom_array_like_classes = []
@roll_up_conditions = false
@enforce_all_conditions = false
end

def array_like_classes
Expand Down
2 changes: 1 addition & 1 deletion lib/blueprinter/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def extract(object, local_options)
end

def skip?(field_name, object, local_options)
if Blueprinter.configuration.roll_up_conditions
if Blueprinter.configuration.enforce_all_conditions
any_global_or_field_condition_failed?(field_name, object, local_options)
else
return true if if_callable && !if_callable.call(field_name, object, local_options)
Expand Down
6 changes: 3 additions & 3 deletions spec/integrations/shared/base_render_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ def self.unless_method(_field_name, _object, _options)
end
end

context 'roll_up_conditions is enabled' do
context 'enforce_all_conditions is enabled' do
let(:local_options) { {x: 1, y: 2, v1: value, v2: other_value} }

before do
Blueprinter.configuration.roll_up_conditions = true
Blueprinter.configuration.enforce_all_conditions = true
Blueprinter.configuration.if = ->(field_name, object, local_opts) {
if local_opts[:sparse_fields]
local_opts[:sparse_fields].include?(field_name.to_s)
Expand All @@ -349,7 +349,7 @@ def self.unless_method(_field_name, _object, _options)
Blueprinter.configuration.unless = ->(_a,_b,_c) { other_value }
end
after do
Blueprinter.configuration.roll_up_conditions = false
Blueprinter.configuration.enforce_all_conditions = false
Blueprinter.configuration.if = nil
Blueprinter.configuration.unless = nil
end
Expand Down

0 comments on commit f96ac5e

Please sign in to comment.