module SweetActions::Serialize

Private Instance Methods

destroyed?() click to toggle source
# File lib/sweet_actions/serialize.rb, line 61
def destroyed?
  resource.respond_to?(:destroyed?) && resource.destroyed?
end
errors() click to toggle source
# File lib/sweet_actions/serialize.rb, line 41
def errors
  resource.respond_to?(:errors) && resource.errors
end
map_base_to__error(error_obj) click to toggle source
# File lib/sweet_actions/serialize.rb, line 36
def map_base_to__error(error_obj)
  error_obj[:_error] = error_obj.delete(:base) if error_obj.key? :base
  error_obj
end
resource() click to toggle source
# File lib/sweet_actions/serialize.rb, line 9
def resource
  raise "resource method must be implemented in #{self.class.name} since it includes SweetActions::Serialize"
end
root_key() click to toggle source
# File lib/sweet_actions/serialize.rb, line 5
def root_key
  raise "root_key method must be implemented in #{self.class.name} since it includes SweetActions::Serialize"
end
serialize() click to toggle source
# File lib/sweet_actions/serialize.rb, line 13
def serialize
  return serialize_destroyed_resource if destroyed?

  { 
    data: {
      type: root_key,
      attributes: serialized_attributes
    }
  }
end
serialize_destroyed_resource() click to toggle source
# File lib/sweet_actions/serialize.rb, line 24
def serialize_destroyed_resource
  { root_key => {} }
end
serialize_errors() click to toggle source
# File lib/sweet_actions/serialize.rb, line 28
def serialize_errors
  {
    errors: {
      singular_key => map_base_to__error(errors.messages)
    }
  }
end
serialized_attributes() click to toggle source
# File lib/sweet_actions/serialize.rb, line 45
def serialized_attributes
  return resource.as_json unless serialized_resource
  serialized_resource.serializer_instance
end
serialized_resource() click to toggle source
# File lib/sweet_actions/serialize.rb, line 50
def serialized_resource
  return nil unless Object.const_defined?('ActiveModelSerializers')
  @serialized_resource ||= begin
    ActiveModelSerializers::SerializableResource.new(resource, serializer_options)
  end
end
serializer_options() click to toggle source
# File lib/sweet_actions/serialize.rb, line 57
def serializer_options
  {}
end