lib/quirky-api/serializers/quirky_serializer.rb
# This will ensure that content is secure by validating the passed block,
# and only showing an attribute's content if the block returns +true+.  If
# the block does not return +true+, this attribute's value will be +null+.
#
# @param attribute [Symbol or Array] The attribute(s) to run permission checks on.
# @param validation [Block] A block that will be run to verify that the
#                           viewing party has permission to see the
#                           attribute.
#
# @example
#   class UserSerializer < QuirkySerializer
#     attributes :id, :name, :email
#     verify_permissions :email, -> { @current_user.can? :update, object rescue false }
#   end
#
#   class UserSerializer < QuirkySerializer
#     attributes :id, :name, :email
#     verify_permissions [:name, :email] do
#       @current_user.can? :update, object rescue false
#     end
#   end
#
# @see validates?
This will ensure that content is secure by validating the passed block,
and only showing an attribute's content if the block returns +true+.  If
the block does not return +true+, this attribute's value will be +null+.
This will ensure that content is secure by validating the passed block and only showing an attribute ' s content if the block returns + true + . If the block does not return + true + this attribute ' s value will be + null + .
def verify_permissions(attributes, validation = nil, &block)
      attributes = [attributes] unless attributes.is_a? Array
      self._validations ||= {}
      attributes.each do |attribute|
        self._validations[attribute] = (validation.present? ? validation : block)
      end
    end
def verify_permissions ( attributes , validation = nil , & block ) attributes = [ attributes ] unless attributes . is_a? Array self . _validations ||= { } attributes . each do | attribute | self . _validations [ attribute ] = ( validation . present? ? validation : block ) end end
This will ensure that content is secure by validating the passed block,
and only showing an attribute's content if the block returns +true+.
This will ensure that content is secure by validating the passed block and only showing an attribute ' s content if the block returns + true + .
attributes
validation
A block that will be run to verify that the viewing party has permission to see the attribute.
A block that will be run to verify that the viewing party has permission to see the attribute .
block
attribute
The attribute(s) to run permission checks on.
The attribute ( s ) to run permission checks on .
example
class UserSerializer < QuirkySerializer attributes :id, :name, :email verify_permissions :email, -> { @current_user.can. :update, object rescue false } end class UserSerializer < QuirkySerializer attributes :id, :name, :email verify_permissions [:name, :email] do @current_user.can. :update, object rescue false end end
class UserSerializer < QuirkySerializer attributes : id : name : email verify_permissions : email - > { @current_user . can . : update object rescue false } end class UserSerializer < QuirkySerializer attributes : id : name : email verify_permissions [ : name : email ] do @current_user . can . : update object rescue false end end
see
None