Skip to content

Commit

Permalink
Refactor resource_data out into app controller
Browse files Browse the repository at this point in the history
  • Loading branch information
djsegal committed Feb 12, 2016
1 parent 66dc3ba commit 3c37985
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
14 changes: 14 additions & 0 deletions app/controllers/devise_token_auth/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ module DeviseTokenAuth
class ApplicationController < DeviseController
include DeviseTokenAuth::Concerns::SetUserByToken

def resource_data
response_data = @resource.as_json
if is_json_api
response_data['type'] = @resource.class.name.parameterize
end
response_data
end

def resource_errors
return @resource.errors.to_hash.merge(full_messages: @resource.errors.full_messages)
end
Expand All @@ -21,5 +29,11 @@ def resource_class(m=nil)

mapping.to
end

def is_json_api
return false unless defined?(ActiveModel::Serializer)
return ActiveModel::Serializer.config.adapter == :json_api
end

end
end
9 changes: 4 additions & 5 deletions app/controllers/devise_token_auth/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,15 @@ def render_create_error_missing_redirect_url
def render_create_error_not_allowed_redirect_url
render json: {
status: 'error',
data: @resource.as_json,
data: resource_data,
errors: [I18n.t("devise_token_auth.passwords.not_allowed_redirect_url", redirect_url: @redirect_url)]
}, status: 422
end

def render_create_success
render json: {
success: true,
data: resource_data,
message: I18n.t("devise_token_auth.passwords.sended", email: @email)
}
end
Expand Down Expand Up @@ -207,10 +208,8 @@ def render_update_error_missing_password
def render_update_success
render json: {
success: true,
data: {
user: @resource,
message: I18n.t("devise_token_auth.passwords.successfully_updated")
}
data: resource_data,
message: I18n.t("devise_token_auth.passwords.successfully_updated")
}
end

Expand Down
17 changes: 6 additions & 11 deletions app/controllers/devise_token_auth/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,51 +110,46 @@ def account_update_params
def render_create_error_missing_confirm_success_url
render json: {
status: 'error',
data: @resource.as_json,
data: resource_data,
errors: [I18n.t("devise_token_auth.registrations.missing_confirm_success_url")]
}, status: 422
end

def render_create_error_redirect_url_not_allowed
render json: {
status: 'error',
data: @resource.as_json,
data: resource_data,
errors: [I18n.t("devise_token_auth.registrations.redirect_url_not_allowed", redirect_url: @redirect_url)]
}, status: 422
end

def render_create_success
response_data = @resource.as_json
if defined?(ActiveModel::Serializer) &&
ActiveModel::Serializer.config.adapter == :json_api
response_data['type'] = @resource.class.name.parameterize
end
render json: {
status: 'success',
data: response_data
data: resource_data
}
end

def render_create_error
render json: {
status: 'error',
data: @resource.as_json,
data: resource_data,
errors: resource_errors
}, status: 422
end

def render_create_error_email_already_exists
render json: {
status: 'error',
data: @resource.as_json,
data: resource_data,
errors: [I18n.t("devise_token_auth.registrations.email_already_exists", email: @resource.email)]
}, status: 422
end

def render_update_success
render json: {
status: 'success',
data: @resource.as_json
data: resource_data
}
end

Expand Down

0 comments on commit 3c37985

Please sign in to comment.