Skip to content

[WIP] Add basic support for ActiveModel::Errors. #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
source 'https://rubygems.org'

gem 'jsonapi-deserializable', '0.1.1.beta3',
github: 'jsonapi-rb/deserializable', branch: 'reverse-mapping'

gemspec
24 changes: 21 additions & 3 deletions lib/jsonapi/rails/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ module ActionController
def self.included(base)
base.class_eval do
extend ClassMethods
prepend InstanceMethods
end
end

module InstanceMethods

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing top-level module documentation comment.

def render(opts = {})
if opts.key?(:jsonapi_error)
mapping = params.to_unsafe_h[:_jsonapi_mapping]
opts = opts.merge(_jsonapi_mapping: mapping)
end
super(opts)
end
end

Expand Down Expand Up @@ -42,16 +53,23 @@ def call(env)
request = Rack::Request.new(env)
body = request.params.slice(*JSONAPI_KEYS)
parser.parse!(body)
deserialized_hash = @deserializable_class.call(body)
deserialize!(request, body)

@app.call(env)
end

def deserialize!(request, body)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method has too many lines. [11/10]

deserialized = @deserializable_class.new(body)
deserialized_hash = deserialized.to_h
mapping = deserialized.mapping
jsonapi = {}
JSONAPI_KEYS.each do |key|
next unless request.params.key?(key)
jsonapi[key.to_sym] = request.delete_param(key)
end
request.update_param(:_jsonapi, jsonapi)
request.update_param(:_jsonapi_mapping, mapping)
request.update_param(@deserializable_key, deserialized_hash)

@app.call(env)
end
end

Expand Down
7 changes: 3 additions & 4 deletions lib/jsonapi/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
require 'action_controller'
require 'active_support'

require 'jsonapi/rails/parser'
require 'jsonapi/rails/renderer'

module JSONAPI
module Rails
class Railtie < ::Rails::Railtie
MEDIA_TYPE = 'application/vnd.api+json'.freeze
require 'jsonapi/rails/parser'
PARSER = JSONAPI::Rails.parser

initializer 'jsonapi-rails.action_controller' do
Expand All @@ -23,6 +21,7 @@ class Railtie < ::Rails::Railtie
::ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
end

require 'jsonapi/rails/renderer'
::ActionController::Renderers.add :jsonapi do |json, options|
unless json.is_a?(String)
json = JSONAPI::Rails::Renderer.render(json, options)
Expand All @@ -33,7 +32,7 @@ class Railtie < ::Rails::Railtie

::ActionController::Renderers.add :jsonapi_errors do |json, options|
unless json.is_a?(String)
json = JSONAPI::Rails::ErrorRender.render_errors(json, options)
json = JSONAPI::Rails::ErrorRenderer.render(json, options)
end
self.content_type ||= Mime[:jsonapi]
self.response_body = json
Expand Down
18 changes: 17 additions & 1 deletion lib/jsonapi/rails/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@ def self.render(resources, options)

class ErrorRenderer
def self.render(errors, options)
# TODO(beauby): SerializableError inference on AR validation errors.
if errors.is_a?(ActiveModel::Errors)
errors = build_active_model_errors(errors, options[:_jsonapi_mapping])
end
JSONAPI::Serializable::ErrorRenderer.render(errors, options)
end

def self.build_active_model_errors(errors, mapping)
error_class = Class.new(JSONAPI::Serializable::Error) do
detail { @detail }
source do
path, name = @attribute
pointer "#{path}/#{name}"
end
end
errors.map! do |attr, error|
attr = mapping[attr]
error_class.new(attribute: attr, detail: error)
end
end
end
end
end