Module: Serega::SeregaPlugins::If

Defined in:
lib/serega/plugins/if/if.rb,
lib/serega/plugins/if/validations/check_opt_if.rb,
lib/serega/plugins/if/validations/check_opt_unless.rb,
lib/serega/plugins/if/validations/check_opt_if_value.rb,
lib/serega/plugins/if/validations/check_opt_unless_value.rb

Overview

Plugin :if

Adds :if, :unless, :if_value, :unless_value attribute options to conditionally remove attributes from the response.

:if/:unless receive the serialized object and context, and are checked before the attribute value is found. :if_value/:unless_value receive the already-found value and context, checked after. The latter two cannot be used with the :serializer option, since a relationship has no "serialized value" of its own — use :if/:unless instead.

See also the plugin-free :hide option (README.md#selecting-fields), which hides an attribute unconditionally.

Examples:

class UserSerializer < Serega
  attribute :email, if: :active? # if user.active?
  attribute :email, if: proc { |user, ctx| user == ctx[:current_user] } # using context
  attribute :email, if: CustomPolicy.method(:view_email?) # any callable

  attribute :email, unless: :hidden? # unless user.hidden?
  attribute :email, if_value: :present? # if email.present?
  attribute :email, unless_value: :blank? # unless email.blank?
end

Defined Under Namespace

Modules: AttributeInstanceMethods, AttributeNormalizerInstanceMethods, CheckAttributeParamsInstanceMethods, DataBuilderClassMethods, ObjectSerializerInstanceMethods, PlanPointInstanceMethods Classes: CheckOptIf, CheckOptIfValue, CheckOptUnless, CheckOptUnlessValue, KeywordConditionResolver

Class Method Summary collapse

Class Method Details

.after_load_plugin(serializer_class, **opts) ⇒ void

This method returns an undefined value.

Adds config options and runs other callbacks after plugin was loaded

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    Plugin options



70
71
72
# File 'lib/serega/plugins/if/if.rb', line 70

def self.after_load_plugin(serializer_class, **opts)
  serializer_class.config.attribute_keys << :if << :if_value << :unless << :unless_value
end

.load_plugin(serializer_class, **_opts) ⇒ void

This method returns an undefined value.

Applies plugin code to specific serializer

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • _opts (Hash)

    Plugin options



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/serega/plugins/if/if.rb', line 47

def self.load_plugin(serializer_class, **_opts)
  require_relative "validations/check_opt_if"
  require_relative "validations/check_opt_if_value"
  require_relative "validations/check_opt_unless"
  require_relative "validations/check_opt_unless_value"

  serializer_class::SeregaAttribute.include(AttributeInstanceMethods)
  serializer_class::SeregaAttributeNormalizer.include(AttributeNormalizerInstanceMethods)
  serializer_class::SeregaPlanPoint.include(PlanPointInstanceMethods)
  serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)
  serializer_class::SeregaObjectSerializer.include(ObjectSerializerInstanceMethods)
  serializer_class::SeregaDataBuilder.extend(DataBuilderClassMethods)
end

.plugin_nameSymbol

Returns Plugin name.

Returns:

  • (Symbol)

    Plugin name



34
35
36
# File 'lib/serega/plugins/if/if.rb', line 34

def self.plugin_name
  :if
end