diff --git a/app/controllers/confirmations_controller.rb b/app/controllers/confirmations_controller.rb index dc4afcd1..c4ecfcec 100644 --- a/app/controllers/confirmations_controller.rb +++ b/app/controllers/confirmations_controller.rb @@ -1,6 +1,23 @@ class ConfirmationsController < ApplicationController include Devise::Controllers::InternalHelpers - include Devise::Controllers::Common + + # GET /resource/confirmation/new + def new + build_resource + render_with_scope :new + end + + # POST /resource/confirmation + def create + self.resource = resource_class.send_confirmation_instructions(params[resource_name]) + + if resource.errors.empty? + set_flash_message :notice, :send_instructions + redirect_to new_session_path(resource_name) + else + render_with_scope :new + end + end # GET /resource/confirmation?confirmation_token=abcdef def show @@ -13,10 +30,4 @@ class ConfirmationsController < ApplicationController render_with_scope :new end end - - protected - - def send_instructions_with - :send_confirmation_instructions - end end diff --git a/app/controllers/passwords_controller.rb b/app/controllers/passwords_controller.rb index 9b4ac14c..3a47523a 100644 --- a/app/controllers/passwords_controller.rb +++ b/app/controllers/passwords_controller.rb @@ -1,9 +1,26 @@ class PasswordsController < ApplicationController include Devise::Controllers::InternalHelpers - include Devise::Controllers::Common before_filter :require_no_authentication + # GET /resource/password/new + def new + build_resource + render_with_scope :new + end + + # POST /resource/password + def create + self.resource = resource_class.send_reset_password_instructions(params[resource_name]) + + if resource.errors.empty? + set_flash_message :notice, :send_instructions + redirect_to new_session_path(resource_name) + else + render_with_scope :new + end + end + # GET /resource/password/edit?reset_password_token=abcdef def edit self.resource = resource_class.new @@ -22,10 +39,4 @@ class PasswordsController < ApplicationController render_with_scope :edit end end - - protected - - def send_instructions_with - :send_reset_password_instructions - end end diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index b032ec3f..5f6af555 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -1,10 +1,18 @@ class RegistrationsController < ApplicationController include Devise::Controllers::InternalHelpers - include Devise::Controllers::Common - # POST /resource/registration + before_filter :require_no_authentication, :only => [ :new, :create ] + before_filter :authenticate_scope!, :only => [:edit, :update, :destroy] + + # GET /resource/sign_in + def new + build_resource + render_with_scope :new + end + + # POST /resource/sign_up def create - self.resource = resource_class.new(params[resource_name]) + build_resource if resource.save flash[:"#{resource_name}_signed_up"] = true @@ -14,4 +22,33 @@ class RegistrationsController < ApplicationController render_with_scope :new end end -end + + # GET /resource/edit + def edit + render_with_scope :edit + end + + # PUT /resource + def update + if self.resource.update_with_password(params[resource_name]) + set_flash_message :notice, :updated + redirect_to after_sign_in_path_for(self.resource) + else + render_with_scope :edit + end + end + + # DELETE /resource + def destroy + self.resource.destroy + sign_out_and_redirect(self.resource) + end + + protected + + # Authenticates the current scope and dup the resource + def authenticate_scope! + send(:"authenticate_#{resource_name}!") + self.resource = send(:"current_#{resource_name}").dup + end +end \ No newline at end of file diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index fd11a954..9f8dff41 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,15 +1,18 @@ class SessionsController < ApplicationController include Devise::Controllers::InternalHelpers - include Devise::Controllers::Common before_filter :require_no_authentication, :only => [ :new, :create ] # GET /resource/sign_in def new - Devise::FLASH_MESSAGES.each do |message| - set_now_flash_message :alert, message if params.try(:[], message) == "true" - end unless resource_just_signed_up? - super + unless resource_just_signed_up? + Devise::FLASH_MESSAGES.each do |message| + set_now_flash_message :alert, message if params.try(:[], message) == "true" + end + end + + build_resource + render_with_scope :new end # POST /resource/sign_in @@ -19,7 +22,7 @@ class SessionsController < ApplicationController sign_in_and_redirect(resource_name, resource, true) else set_now_flash_message :alert, (warden.message || :invalid) - build_resource + clean_up_password_methods(build_resource) render_with_scope :new end end @@ -35,4 +38,10 @@ class SessionsController < ApplicationController def resource_just_signed_up? flash[:"#{resource_name}_signed_up"] end + + def clean_up_password_methods(object) + [:password=, :password_confirmation=].each do |method| + object.send(method, nil) if object.respond_to?(method) + end + end end diff --git a/app/controllers/unlocks_controller.rb b/app/controllers/unlocks_controller.rb index de7d344e..32109683 100644 --- a/app/controllers/unlocks_controller.rb +++ b/app/controllers/unlocks_controller.rb @@ -1,6 +1,23 @@ class UnlocksController < ApplicationController include Devise::Controllers::InternalHelpers - include Devise::Controllers::Common + + # GET /resource/unlock/new + def new + build_resource + render_with_scope :new + end + + # POST /resource/unlock + def create + self.resource = resource_class.send_unlock_instructions(params[resource_name]) + + if resource.errors.empty? + set_flash_message :notice, :send_instructions + redirect_to new_session_path(resource_name) + else + render_with_scope :new + end + end # GET /resource/unlock?unlock_token=abcdef def show @@ -13,10 +30,4 @@ class UnlocksController < ApplicationController render_with_scope :new end end - - protected - - def send_instructions_with - :send_unlock_instructions - end end diff --git a/lib/devise.rb b/lib/devise.rb index c0a918b2..5bc2e946 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -4,7 +4,6 @@ module Devise autoload :TestHelpers, 'devise/test_helpers' module Controllers - autoload :Common, 'devise/controllers/common' autoload :Helpers, 'devise/controllers/helpers' autoload :InternalHelpers, 'devise/controllers/internal_helpers' autoload :UrlHelpers, 'devise/controllers/url_helpers' diff --git a/lib/devise/controllers/common.rb b/lib/devise/controllers/common.rb deleted file mode 100644 index 1bfe7b6b..00000000 --- a/lib/devise/controllers/common.rb +++ /dev/null @@ -1,24 +0,0 @@ -module Devise - module Controllers - # Common actions shared between Devise controllers - module Common #:nodoc: - # GET /resource/controller/new - def new - build_resource - render_with_scope :new - end - - # POST /resource/controller - def create - self.resource = resource_class.send(send_instructions_with, params[resource_name]) - - if resource.errors.empty? - set_flash_message :notice, :send_instructions - redirect_to new_session_path(resource_name) - else - render_with_scope :new - end - end - end - end -end diff --git a/lib/devise/controllers/internal_helpers.rb b/lib/devise/controllers/internal_helpers.rb index ecfaa11a..76e16291 100644 --- a/lib/devise/controllers/internal_helpers.rb +++ b/lib/devise/controllers/internal_helpers.rb @@ -70,12 +70,9 @@ module Devise instance_variable_set(:"@#{resource_name}", new_resource) end - # Build a devise resource without setting password and password confirmation fields. + # Build a devise resource. def build_resource - self.resource ||= begin - attributes = params[resource_name].try(:except, :password, :password_confirmation) - resource_class.new(attributes || {}) - end + self.resource ||= resource_class.new(params[resource_name] || {}) end # Helper for use in before_filters where no authentication is required.