Created
January 8, 2018 15:04
-
-
Save rasmar/15396d745459532b75b6eabc823d51f0 to your computer and use it in GitHub Desktop.
DeviseOverrides Controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| module DeviseOverrides | |
| class PasswordsController < Devise::PasswordsController | |
| include DeviseOverrides::Concerns::RedirectionHandler | |
| skip_before_action :verify_authenticity_token | |
| # POST /resource/password | |
| def create | |
| self.resource = resource_class.send_reset_password_instructions(resource_params) | |
| yield resource if block_given? | |
| if successfully_sent?(resource) | |
| redirect_with_custom_params( | |
| front_root, :notice, :i18, "user.reset_password.instructions_sent" | |
| ) | |
| else | |
| redirect_with_custom_params( | |
| front_root, :error, :i18, "user.reset_password.instructions_invalid", "/recover-password" | |
| ) | |
| end | |
| end | |
| # PUT /resource/password | |
| def update | |
| self.resource = resource_class.reset_password_by_token(resource_params) | |
| yield resource if block_given? | |
| if resource.errors.empty? | |
| redirect_with_custom_params( | |
| front_root, :message, :i18, "user.reset_password.changed", "/login" | |
| ) | |
| else | |
| redirect_with_custom_params( | |
| front_root, :error, :str, compact_errors(resource), "/recover-password" | |
| ) | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| module DeviseOverrides | |
| module Concerns::RedirectionHandler | |
| extend ::ActiveSupport::Concern | |
| included do | |
| def redirect_with_custom_params(url, message_type, message_source, content, url_append = nil) | |
| custom_params = {} | |
| custom_params[message_type] = message_source == :i18 ? I18n.t(content) : content | |
| url += url_append if url_append.present? | |
| redirect_to append_params(url, custom_params).to_s, status: :see_other | |
| end | |
| def compact_errors(resource) | |
| resource.errors.full_messages.join(". ") | |
| end | |
| private | |
| def append_params(url, custom_params) | |
| callback = Addressable::URI.parse(url) | |
| callback.query_values = (callback.query_values || {}).merge(custom_params) | |
| callback | |
| end | |
| def front_root | |
| Rails.application.secrets.frontend_host | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment