Skip to content

Commit

Permalink
Serializers hot plugged
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Oct 30, 2014
1 parent ebaef83 commit 782ca24
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
33 changes: 7 additions & 26 deletions app/controllers/devise_token_auth/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,19 @@ class ApplicationController < DeviseController
respond_to :json

def success_message(message = nil)
json_response = { status: 'success' }
json_response[:message] = message if message
json_response
serializer = DeviseTokenAuth.success_message_serializer || SuccessMessageSerializer
serializer.new(message)
end

def error_messages(*args)
{
status: 'error',
errors: args
}
end

def resource_serializer(resource)
{
status: "success",
data: resource.as_json(except: [:tokens, :created_at, :updated_at])
}
serializer = DeviseTokenAuth.error_messages_serializer || ErrorMessagesSerializer
serializer.new(*args)
end

def error_serializer(*args)
resource = args[0]
response = {
status: "error",
data: resource.as_json(except: [:tokens, :created_at, :updated_at])
}
if args.length > 1
args.shift
response[:errors] = args
else
response[:errors] = resource.errors.to_hash.merge(full_messages: resource.errors.full_messages)
end
response
serializer = DeviseTokenAuth.error_serializer || ResourceErrorsSerializer
serializer.new(*args)
end

end
end
4 changes: 4 additions & 0 deletions app/controllers/devise_token_auth/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,9 @@ def password_resource_params
devise_parameter_sanitizer.sanitize(:account_update)
end

def resource_serializer(user)
serializer = DeviseTokenAuth.password_serializer || ResourceSerializer
serializer.new(user)
end
end
end
5 changes: 5 additions & 0 deletions app/controllers/devise_token_auth/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,10 @@ def sign_up_params
def account_update_params
devise_parameter_sanitizer.sanitize(:account_update)
end

def resource_serializer(user)
serializer = DeviseTokenAuth.registration_serializer || ResourceSerializer
serializer.new(user)
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/devise_token_auth/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ def destroy
end
end

def resource_serializer(user)
serializer = DeviseTokenAuth.session_serializer || ResourceSerializer
serializer.new(user)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ def validate_token
end


def resource_serializer(user)
serializer = DeviseTokenAuth.token_validation_serializer || ResourceSerializer
serializer.new(user)
end

end
end
9 changes: 8 additions & 1 deletion lib/devise_token_auth/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ class Engine < ::Rails::Engine
mattr_accessor :change_headers_on_each_request,
:token_lifespan,
:batch_request_buffer_throttle,
:omniauth_prefix
:omniauth_prefix,
:session_serializer,
:registration_serializer,
:token_validation_serializer,
:password_serializer,
:error_serializer,
:error_messages_serializer,
:success_message_serializer

self.change_headers_on_each_request = true
self.token_lifespan = 2.weeks
Expand Down

0 comments on commit 782ca24

Please sign in to comment.