From 4f77da5b87293c1ef9f2c4f94a862f580bf0815d Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Sat, 23 Aug 2014 16:44:06 -0500 Subject: [PATCH 01/33] Step1: Create controllers templates, and create controller generator to copy ALL templates to rails app directory --- .../devise/controllers_generator.rb | 17 ++ .../controllers/confirmations_controller.rb | 16 ++ .../controllers/devise_controller.rb | 176 ++++++++++++++++++ .../omniauth_callbacks_controller.rb | 21 +++ .../controllers/passwords_controller.rb | 21 +++ .../controllers/registrations_controller.rb | 35 ++++ .../controllers/sessions_controller.rb | 16 ++ .../controllers/unlocks_controller.rb | 16 ++ 8 files changed, 318 insertions(+) create mode 100644 lib/generators/devise/controllers_generator.rb create mode 100644 lib/generators/templates/controllers/confirmations_controller.rb create mode 100644 lib/generators/templates/controllers/devise_controller.rb create mode 100644 lib/generators/templates/controllers/omniauth_callbacks_controller.rb create mode 100644 lib/generators/templates/controllers/passwords_controller.rb create mode 100644 lib/generators/templates/controllers/registrations_controller.rb create mode 100644 lib/generators/templates/controllers/sessions_controller.rb create mode 100644 lib/generators/templates/controllers/unlocks_controller.rb diff --git a/lib/generators/devise/controllers_generator.rb b/lib/generators/devise/controllers_generator.rb new file mode 100644 index 00000000..c3cd01fc --- /dev/null +++ b/lib/generators/devise/controllers_generator.rb @@ -0,0 +1,17 @@ +require 'rails/generators/base' + +module Devise + module Generators + class ControllersGenerator < Rails::Generators::Base + desc "Create inherited Devise controllers in your application." + source_root File.expand_path("../../templates/", __FILE__) + argument :scope, required: false, default: nil, + desc: "The scope to create controllers in" + + def create_controllers + directory "controllers", "app/controllers/#{scope || :devise}" + end + end + end +end + diff --git a/lib/generators/templates/controllers/confirmations_controller.rb b/lib/generators/templates/controllers/confirmations_controller.rb new file mode 100644 index 00000000..e3ce0872 --- /dev/null +++ b/lib/generators/templates/controllers/confirmations_controller.rb @@ -0,0 +1,16 @@ +class Users::ConfirmationsController < Devise::ConfirmationsController + # GET /resource/confirmation/new + def new + super + end + + # POST /resource/confirmation + def create + super + end + + # GET /resource/confirmation?confirmation_token=abcdef + def show + super + end +end diff --git a/lib/generators/templates/controllers/devise_controller.rb b/lib/generators/templates/controllers/devise_controller.rb new file mode 100644 index 00000000..7cdd0f7e --- /dev/null +++ b/lib/generators/templates/controllers/devise_controller.rb @@ -0,0 +1,176 @@ +# All Devise controllers are inherited from here. +class DeviseController < Devise.parent_controller.constantize + include Devise::Controllers::ScopedViews + + helper DeviseHelper + + helpers = %w(resource scope_name resource_name signed_in_resource + resource_class resource_params devise_mapping) + hide_action(*helpers) + helper_method(*helpers) + + prepend_before_filter :assert_is_devise_resource! + respond_to :html if mimes_for_respond_to.empty? + + # Gets the actual resource stored in the instance variable + def resource + instance_variable_get(:"@#{resource_name}") + end + + # Proxy to devise map name + def resource_name + devise_mapping.name + end + alias :scope_name :resource_name + + # Proxy to devise map class + def resource_class + devise_mapping.to + end + + # Returns a signed in resource from session (if one exists) + def signed_in_resource + warden.authenticate(scope: resource_name) + end + + # Attempt to find the mapped route for devise based on request path + def devise_mapping + @devise_mapping ||= request.env["devise.mapping"] + end + + # Override prefixes to consider the scoped view. + # Notice we need to check for the request due to a bug in + # Action Controller tests that forces _prefixes to be + # loaded before even having a request object. + def _prefixes #:nodoc: + @_prefixes ||= if self.class.scoped_views? && request && devise_mapping + ["#{devise_mapping.scoped_path}/#{controller_name}"] + super + else + super + end + end + + hide_action :_prefixes + + protected + + # Checks whether it's a devise mapped resource or not. + def assert_is_devise_resource! #:nodoc: + unknown_action! <<-MESSAGE unless devise_mapping +Could not find devise mapping for path #{request.fullpath.inspect}. +This may happen for two reasons: + +1) You forgot to wrap your route inside the scope block. For example: + + devise_scope :user do + get "/some/route" => "some_devise_controller" + end + +2) You are testing a Devise controller bypassing the router. + If so, you can explicitly tell Devise which mapping to use: + + @request.env["devise.mapping"] = Devise.mappings[:user] + +MESSAGE + end + + # Returns real navigational formats which are supported by Rails + def navigational_formats + @navigational_formats ||= Devise.navigational_formats.select { |format| Mime::EXTENSION_LOOKUP[format.to_s] } + end + + def unknown_action!(msg) + logger.debug "[Devise] #{msg}" if logger + raise AbstractController::ActionNotFound, msg + end + + # Sets the resource creating an instance variable + def resource=(new_resource) + instance_variable_set(:"@#{resource_name}", new_resource) + end + + # Helper for use in before_filters where no authentication is required. + # + # Example: + # before_filter :require_no_authentication, only: :new + def require_no_authentication + assert_is_devise_resource! + return unless is_navigational_format? + no_input = devise_mapping.no_input_strategies + + authenticated = if no_input.present? + args = no_input.dup.push scope: resource_name + warden.authenticate?(*args) + else + warden.authenticated?(resource_name) + end + + if authenticated && resource = warden.user(resource_name) + flash[:alert] = I18n.t("devise.failure.already_authenticated") + redirect_to after_sign_in_path_for(resource) + end + end + + # Helper for use after calling send_*_instructions methods on a resource. + # If we are in paranoid mode, we always act as if the resource was valid + # and instructions were sent. + def successfully_sent?(resource) + notice = if Devise.paranoid + resource.errors.clear + :send_paranoid_instructions + elsif resource.errors.empty? + :send_instructions + end + + if notice + set_flash_message :notice, notice if is_flashing_format? + true + end + end + + # Sets the flash message with :key, using I18n. By default you are able + # to setup your messages using specific resource scope, and if no one is + # found we look to default scope. + # Example (i18n locale file): + # + # en: + # devise: + # passwords: + # #default_scope_messages - only if resource_scope is not found + # user: + # #resource_scope_messages + # + # Please refer to README or en.yml locale file to check what messages are + # available. + def set_flash_message(key, kind, options = {}) + message = find_message(kind, options) + flash[key] = message if message.present? + end + + def devise_i18n_options(options) + options + end + + # Get message for given + def find_message(kind, options = {}) + options[:scope] = "devise.#{controller_name}" + options[:default] = Array(options[:default]).unshift(kind.to_sym) + options[:resource_name] = resource_name + options = devise_i18n_options(options) + I18n.t("#{options[:resource_name]}.#{kind}", options) + end + + def clean_up_passwords(object) + object.clean_up_passwords if object.respond_to?(:clean_up_passwords) + end + + def respond_with_navigational(*args, &block) + respond_with(*args) do |format| + format.any(*navigational_formats, &block) + end + end + + def resource_params + params.fetch(resource_name, {}) + end +end diff --git a/lib/generators/templates/controllers/omniauth_callbacks_controller.rb b/lib/generators/templates/controllers/omniauth_callbacks_controller.rb new file mode 100644 index 00000000..ecde9669 --- /dev/null +++ b/lib/generators/templates/controllers/omniauth_callbacks_controller.rb @@ -0,0 +1,21 @@ +class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController + # You should configure your model like this: + # devise :omniauthable, omniauth_providers: [:twitter] + + # You should also create an action method in this controller like this: + # def twitter + # end + + # More info at: + # https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview + + # GET|POST /resource/auth/twitter + def passthru + super + end + + # GET|POST /users/auth/twitter/callback + def failure + super + end +end diff --git a/lib/generators/templates/controllers/passwords_controller.rb b/lib/generators/templates/controllers/passwords_controller.rb new file mode 100644 index 00000000..69bb42a1 --- /dev/null +++ b/lib/generators/templates/controllers/passwords_controller.rb @@ -0,0 +1,21 @@ +class Users::PasswordsController < Devise::PasswordsController + # GET /resource/password/new + def new + super + end + + # POST /resource/password + def create + super + end + + # GET /resource/password/edit?reset_password_token=abcdef + def edit + super + end + + # PUT /resource/password + def update + super + end +end diff --git a/lib/generators/templates/controllers/registrations_controller.rb b/lib/generators/templates/controllers/registrations_controller.rb new file mode 100644 index 00000000..0c6cb093 --- /dev/null +++ b/lib/generators/templates/controllers/registrations_controller.rb @@ -0,0 +1,35 @@ +class Users::RegistrationsController < Devise::RegistrationsController + # GET /resource/sign_up + def new + super + end + + # POST /resource + def create + super + end + + # GET /resource/edit + def edit + super + end + + # PUT /resource + def update + super + end + + # DELETE /resource + def destroy + super + end + + # GET /resource/cancel + # Forces the session data which is usually expired after sign + # in to be expired now. This is useful if the user wants to + # cancel oauth signing in/up in the middle of the process, + # removing all OAuth session data. + def cancel + super + end +end diff --git a/lib/generators/templates/controllers/sessions_controller.rb b/lib/generators/templates/controllers/sessions_controller.rb new file mode 100644 index 00000000..68aa08ff --- /dev/null +++ b/lib/generators/templates/controllers/sessions_controller.rb @@ -0,0 +1,16 @@ +class Users::SessionsController < Devise::SessionsController + # GET /resource/sign_in + def new + super + end + + # POST /resource/sign_in + def create + super + end + + # DELETE /resource/sign_out + def destroy + super + end +end diff --git a/lib/generators/templates/controllers/unlocks_controller.rb b/lib/generators/templates/controllers/unlocks_controller.rb new file mode 100644 index 00000000..1ec445c5 --- /dev/null +++ b/lib/generators/templates/controllers/unlocks_controller.rb @@ -0,0 +1,16 @@ +class Users::UnlocksController < Devise::UnlocksController + # GET /resource/unlock/new + def new + super + end + + # POST /resource/unlock + def create + super + end + + # GET /resource/unlock?unlock_token=abcdef + def show + super + end +end From d8d9814ed4889e6bbe7fd2ee9e4b5a6823f1e0db Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Sat, 23 Aug 2014 23:29:45 -0500 Subject: [PATCH 02/33] user erb template to generate controllers, enable --controllers option to specify controllers, make 'scope' argument required --- .../devise/controllers_generator.rb | 18 +- ...roller.rb => confirmations_controller.erb} | 2 +- .../controllers/devise_controller.rb | 176 ------------------ ...r.rb => omniauth_callbacks_controller.erb} | 2 +- ...controller.rb => passwords_controller.erb} | 2 +- ...roller.rb => registrations_controller.erb} | 2 +- ..._controller.rb => sessions_controller.erb} | 2 +- ...s_controller.rb => unlocks_controller.erb} | 2 +- 8 files changed, 20 insertions(+), 186 deletions(-) rename lib/generators/templates/controllers/{confirmations_controller.rb => confirmations_controller.erb} (71%) delete mode 100644 lib/generators/templates/controllers/devise_controller.rb rename lib/generators/templates/controllers/{omniauth_callbacks_controller.rb => omniauth_callbacks_controller.erb} (82%) rename lib/generators/templates/controllers/{passwords_controller.rb => passwords_controller.erb} (77%) rename lib/generators/templates/controllers/{registrations_controller.rb => registrations_controller.erb} (86%) rename lib/generators/templates/controllers/{sessions_controller.rb => sessions_controller.erb} (69%) rename lib/generators/templates/controllers/{unlocks_controller.rb => unlocks_controller.erb} (71%) diff --git a/lib/generators/devise/controllers_generator.rb b/lib/generators/devise/controllers_generator.rb index c3cd01fc..d5b4f090 100644 --- a/lib/generators/devise/controllers_generator.rb +++ b/lib/generators/devise/controllers_generator.rb @@ -3,13 +3,23 @@ require 'rails/generators/base' module Devise module Generators class ControllersGenerator < Rails::Generators::Base + def self.all_controllers + @@controllers ||= %w(confirmations passwords registrations sessions unlocks omniauth_callbacks) + end + desc "Create inherited Devise controllers in your application." - source_root File.expand_path("../../templates/", __FILE__) - argument :scope, required: false, default: nil, - desc: "The scope to create controllers in" + source_root File.expand_path("../../templates/controllers", __FILE__) + argument :scope, required: true, + desc: "The scope to create controllers in, e.g. users, admins" + class_option :controllers, aliases: "-c", type: :array, desc: "Select specific controllers to generate (#{all_controllers})" def create_controllers - directory "controllers", "app/controllers/#{scope || :devise}" + @scope_module = scope.camelize + controllers = options[:controllers] || self.class.all_controllers + controllers.each do |name| + template "#{name}_controller.erb", + "app/controllers/#{scope}/#{name}_controller.rb" + end end end end diff --git a/lib/generators/templates/controllers/confirmations_controller.rb b/lib/generators/templates/controllers/confirmations_controller.erb similarity index 71% rename from lib/generators/templates/controllers/confirmations_controller.rb rename to lib/generators/templates/controllers/confirmations_controller.erb index e3ce0872..bc8f147c 100644 --- a/lib/generators/templates/controllers/confirmations_controller.rb +++ b/lib/generators/templates/controllers/confirmations_controller.erb @@ -1,4 +1,4 @@ -class Users::ConfirmationsController < Devise::ConfirmationsController +class <%= @scope_module %>::ConfirmationsController < Devise::ConfirmationsController # GET /resource/confirmation/new def new super diff --git a/lib/generators/templates/controllers/devise_controller.rb b/lib/generators/templates/controllers/devise_controller.rb deleted file mode 100644 index 7cdd0f7e..00000000 --- a/lib/generators/templates/controllers/devise_controller.rb +++ /dev/null @@ -1,176 +0,0 @@ -# All Devise controllers are inherited from here. -class DeviseController < Devise.parent_controller.constantize - include Devise::Controllers::ScopedViews - - helper DeviseHelper - - helpers = %w(resource scope_name resource_name signed_in_resource - resource_class resource_params devise_mapping) - hide_action(*helpers) - helper_method(*helpers) - - prepend_before_filter :assert_is_devise_resource! - respond_to :html if mimes_for_respond_to.empty? - - # Gets the actual resource stored in the instance variable - def resource - instance_variable_get(:"@#{resource_name}") - end - - # Proxy to devise map name - def resource_name - devise_mapping.name - end - alias :scope_name :resource_name - - # Proxy to devise map class - def resource_class - devise_mapping.to - end - - # Returns a signed in resource from session (if one exists) - def signed_in_resource - warden.authenticate(scope: resource_name) - end - - # Attempt to find the mapped route for devise based on request path - def devise_mapping - @devise_mapping ||= request.env["devise.mapping"] - end - - # Override prefixes to consider the scoped view. - # Notice we need to check for the request due to a bug in - # Action Controller tests that forces _prefixes to be - # loaded before even having a request object. - def _prefixes #:nodoc: - @_prefixes ||= if self.class.scoped_views? && request && devise_mapping - ["#{devise_mapping.scoped_path}/#{controller_name}"] + super - else - super - end - end - - hide_action :_prefixes - - protected - - # Checks whether it's a devise mapped resource or not. - def assert_is_devise_resource! #:nodoc: - unknown_action! <<-MESSAGE unless devise_mapping -Could not find devise mapping for path #{request.fullpath.inspect}. -This may happen for two reasons: - -1) You forgot to wrap your route inside the scope block. For example: - - devise_scope :user do - get "/some/route" => "some_devise_controller" - end - -2) You are testing a Devise controller bypassing the router. - If so, you can explicitly tell Devise which mapping to use: - - @request.env["devise.mapping"] = Devise.mappings[:user] - -MESSAGE - end - - # Returns real navigational formats which are supported by Rails - def navigational_formats - @navigational_formats ||= Devise.navigational_formats.select { |format| Mime::EXTENSION_LOOKUP[format.to_s] } - end - - def unknown_action!(msg) - logger.debug "[Devise] #{msg}" if logger - raise AbstractController::ActionNotFound, msg - end - - # Sets the resource creating an instance variable - def resource=(new_resource) - instance_variable_set(:"@#{resource_name}", new_resource) - end - - # Helper for use in before_filters where no authentication is required. - # - # Example: - # before_filter :require_no_authentication, only: :new - def require_no_authentication - assert_is_devise_resource! - return unless is_navigational_format? - no_input = devise_mapping.no_input_strategies - - authenticated = if no_input.present? - args = no_input.dup.push scope: resource_name - warden.authenticate?(*args) - else - warden.authenticated?(resource_name) - end - - if authenticated && resource = warden.user(resource_name) - flash[:alert] = I18n.t("devise.failure.already_authenticated") - redirect_to after_sign_in_path_for(resource) - end - end - - # Helper for use after calling send_*_instructions methods on a resource. - # If we are in paranoid mode, we always act as if the resource was valid - # and instructions were sent. - def successfully_sent?(resource) - notice = if Devise.paranoid - resource.errors.clear - :send_paranoid_instructions - elsif resource.errors.empty? - :send_instructions - end - - if notice - set_flash_message :notice, notice if is_flashing_format? - true - end - end - - # Sets the flash message with :key, using I18n. By default you are able - # to setup your messages using specific resource scope, and if no one is - # found we look to default scope. - # Example (i18n locale file): - # - # en: - # devise: - # passwords: - # #default_scope_messages - only if resource_scope is not found - # user: - # #resource_scope_messages - # - # Please refer to README or en.yml locale file to check what messages are - # available. - def set_flash_message(key, kind, options = {}) - message = find_message(kind, options) - flash[key] = message if message.present? - end - - def devise_i18n_options(options) - options - end - - # Get message for given - def find_message(kind, options = {}) - options[:scope] = "devise.#{controller_name}" - options[:default] = Array(options[:default]).unshift(kind.to_sym) - options[:resource_name] = resource_name - options = devise_i18n_options(options) - I18n.t("#{options[:resource_name]}.#{kind}", options) - end - - def clean_up_passwords(object) - object.clean_up_passwords if object.respond_to?(:clean_up_passwords) - end - - def respond_with_navigational(*args, &block) - respond_with(*args) do |format| - format.any(*navigational_formats, &block) - end - end - - def resource_params - params.fetch(resource_name, {}) - end -end diff --git a/lib/generators/templates/controllers/omniauth_callbacks_controller.rb b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb similarity index 82% rename from lib/generators/templates/controllers/omniauth_callbacks_controller.rb rename to lib/generators/templates/controllers/omniauth_callbacks_controller.erb index ecde9669..2b2dd0cd 100644 --- a/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +++ b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb @@ -1,4 +1,4 @@ -class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController +class <%= @scope_module %>::OmniauthCallbacksController < Devise::OmniauthCallbacksController # You should configure your model like this: # devise :omniauthable, omniauth_providers: [:twitter] diff --git a/lib/generators/templates/controllers/passwords_controller.rb b/lib/generators/templates/controllers/passwords_controller.erb similarity index 77% rename from lib/generators/templates/controllers/passwords_controller.rb rename to lib/generators/templates/controllers/passwords_controller.erb index 69bb42a1..b77c83b3 100644 --- a/lib/generators/templates/controllers/passwords_controller.rb +++ b/lib/generators/templates/controllers/passwords_controller.erb @@ -1,4 +1,4 @@ -class Users::PasswordsController < Devise::PasswordsController +class <%= @scope_module %>::PasswordsController < Devise::PasswordsController # GET /resource/password/new def new super diff --git a/lib/generators/templates/controllers/registrations_controller.rb b/lib/generators/templates/controllers/registrations_controller.erb similarity index 86% rename from lib/generators/templates/controllers/registrations_controller.rb rename to lib/generators/templates/controllers/registrations_controller.erb index 0c6cb093..8916b30a 100644 --- a/lib/generators/templates/controllers/registrations_controller.rb +++ b/lib/generators/templates/controllers/registrations_controller.erb @@ -1,4 +1,4 @@ -class Users::RegistrationsController < Devise::RegistrationsController +class <%= @scope_module %>::RegistrationsController < Devise::RegistrationsController # GET /resource/sign_up def new super diff --git a/lib/generators/templates/controllers/sessions_controller.rb b/lib/generators/templates/controllers/sessions_controller.erb similarity index 69% rename from lib/generators/templates/controllers/sessions_controller.rb rename to lib/generators/templates/controllers/sessions_controller.erb index 68aa08ff..acd52dcb 100644 --- a/lib/generators/templates/controllers/sessions_controller.rb +++ b/lib/generators/templates/controllers/sessions_controller.erb @@ -1,4 +1,4 @@ -class Users::SessionsController < Devise::SessionsController +module <%= @scope_module %>::SessionsController < Devise::SessionsController # GET /resource/sign_in def new super diff --git a/lib/generators/templates/controllers/unlocks_controller.rb b/lib/generators/templates/controllers/unlocks_controller.erb similarity index 71% rename from lib/generators/templates/controllers/unlocks_controller.rb rename to lib/generators/templates/controllers/unlocks_controller.erb index 1ec445c5..a4d2edf6 100644 --- a/lib/generators/templates/controllers/unlocks_controller.rb +++ b/lib/generators/templates/controllers/unlocks_controller.erb @@ -1,4 +1,4 @@ -class Users::UnlocksController < Devise::UnlocksController +class <%= @scope_module %>::UnlocksController < Devise::UnlocksController # GET /resource/unlock/new def new super From 8de94035f0f8908b15189c89a02b7c43a8ff5df4 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Sun, 24 Aug 2014 00:06:12 -0500 Subject: [PATCH 03/33] add better help description --- .../devise/controllers_generator.rb | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/generators/devise/controllers_generator.rb b/lib/generators/devise/controllers_generator.rb index d5b4f090..8ebc7e68 100644 --- a/lib/generators/devise/controllers_generator.rb +++ b/lib/generators/devise/controllers_generator.rb @@ -7,11 +7,31 @@ module Devise @@controllers ||= %w(confirmations passwords registrations sessions unlocks omniauth_callbacks) end - desc "Create inherited Devise controllers in your application." + desc <<-DESC +Create inherited Devise controllers in your app/controllers folder. + +User -c to specify which controller you want to overwrite. +If you do no specify a controller, all controllers will be created. + +Usage example: rails generate devise:controllers users -c=sessions +This will create a controller class at app/controllers/users/sessions_controller.rb like this: +> class Users::ConfirmationsController < Devise::ConfirmationsController +> content... +> end + +Note: you MUST specify a scope(like users here) and config your route.rb file accordingly like this: +> Rails.application.routes.draw do +> content... +> devise_for :users, controllers: { +> sessions: 'users/sessions', +> other controller... +> } +> end + DESC source_root File.expand_path("../../templates/controllers", __FILE__) argument :scope, required: true, desc: "The scope to create controllers in, e.g. users, admins" - class_option :controllers, aliases: "-c", type: :array, desc: "Select specific controllers to generate (#{all_controllers})" + class_option :controllers, aliases: "-c", type: :array, desc: "Select specific controllers to generate (#{all_controllers.join(', ')})" def create_controllers @scope_module = scope.camelize From 7b5293906b6da9d14e65abb92fa42c850ed1b0fe Mon Sep 17 00:00:00 2001 From: micat Date: Sat, 23 Aug 2014 22:33:08 +0900 Subject: [PATCH 04/33] Return Symbol in find_scope! if String passed --- lib/devise/mapping.rb | 2 +- test/controllers/url_helpers_test.rb | 6 ++++++ test/mapping_test.rb | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/devise/mapping.rb b/lib/devise/mapping.rb index de787dc5..5c40f762 100644 --- a/lib/devise/mapping.rb +++ b/lib/devise/mapping.rb @@ -33,7 +33,7 @@ module Devise def self.find_scope!(obj) case obj when String, Symbol - return obj + return obj.to_sym when Class Devise.mappings.each_value { |m| return m.name if obj <= m.to } else diff --git a/test/controllers/url_helpers_test.rb b/test/controllers/url_helpers_test.rb index 23ba5f15..06895b5d 100644 --- a/test/controllers/url_helpers_test.rb +++ b/test/controllers/url_helpers_test.rb @@ -13,6 +13,12 @@ class RoutesTest < ActionController::TestCase assert_equal @controller.send(:"#{prepend_path}#{name}_url", :user), send(:"#{prepend_path}user_#{name}_url") + # With string + assert_equal @controller.send(:"#{prepend_path}#{name}_path", "user"), + send(:"#{prepend_path}user_#{name}_path") + assert_equal @controller.send(:"#{prepend_path}#{name}_url", "user"), + send(:"#{prepend_path}user_#{name}_url") + # Default url params assert_equal @controller.send(:"#{prepend_path}#{name}_path", :user, param: 123), send(:"#{prepend_path}user_#{name}_path", param: 123) diff --git a/test/mapping_test.rb b/test/mapping_test.rb index 2ec97e19..d22bf0bc 100644 --- a/test/mapping_test.rb +++ b/test/mapping_test.rb @@ -62,6 +62,7 @@ class MappingTest < ActiveSupport::TestCase test 'find scope for a given object' do assert_equal :user, Devise::Mapping.find_scope!(User) assert_equal :user, Devise::Mapping.find_scope!(:user) + assert_equal :user, Devise::Mapping.find_scope!("user") assert_equal :user, Devise::Mapping.find_scope!(User.new) end From 02b1a9c4e0fa30fcc78618de552513f91433ee13 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Sun, 24 Aug 2014 21:28:06 -0500 Subject: [PATCH 05/33] enable empty scope when generating a controller --- lib/generators/devise/controllers_generator.rb | 4 ++-- .../templates/controllers/confirmations_controller.erb | 2 +- .../templates/controllers/omniauth_callbacks_controller.erb | 2 +- lib/generators/templates/controllers/passwords_controller.erb | 2 +- .../templates/controllers/registrations_controller.erb | 2 +- lib/generators/templates/controllers/sessions_controller.erb | 2 +- lib/generators/templates/controllers/unlocks_controller.erb | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/generators/devise/controllers_generator.rb b/lib/generators/devise/controllers_generator.rb index 8ebc7e68..46b50dea 100644 --- a/lib/generators/devise/controllers_generator.rb +++ b/lib/generators/devise/controllers_generator.rb @@ -29,12 +29,12 @@ Note: you MUST specify a scope(like users here) and config your route.rb file ac > end DESC source_root File.expand_path("../../templates/controllers", __FILE__) - argument :scope, required: true, + argument :scope, required: false, default: nil, desc: "The scope to create controllers in, e.g. users, admins" class_option :controllers, aliases: "-c", type: :array, desc: "Select specific controllers to generate (#{all_controllers.join(', ')})" def create_controllers - @scope_module = scope.camelize + @scope_prefix = scope.blank? ? '' : (scope.camelize + '::') controllers = options[:controllers] || self.class.all_controllers controllers.each do |name| template "#{name}_controller.erb", diff --git a/lib/generators/templates/controllers/confirmations_controller.erb b/lib/generators/templates/controllers/confirmations_controller.erb index bc8f147c..520e5e23 100644 --- a/lib/generators/templates/controllers/confirmations_controller.erb +++ b/lib/generators/templates/controllers/confirmations_controller.erb @@ -1,4 +1,4 @@ -class <%= @scope_module %>::ConfirmationsController < Devise::ConfirmationsController +class <%= @scope_prefix %>ConfirmationsController < Devise::ConfirmationsController # GET /resource/confirmation/new def new super diff --git a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb index 2b2dd0cd..4769fd92 100644 --- a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb +++ b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb @@ -1,4 +1,4 @@ -class <%= @scope_module %>::OmniauthCallbacksController < Devise::OmniauthCallbacksController +class <%= @scope_prefix %>OmniauthCallbacksController < Devise::OmniauthCallbacksController # You should configure your model like this: # devise :omniauthable, omniauth_providers: [:twitter] diff --git a/lib/generators/templates/controllers/passwords_controller.erb b/lib/generators/templates/controllers/passwords_controller.erb index b77c83b3..17b7573c 100644 --- a/lib/generators/templates/controllers/passwords_controller.erb +++ b/lib/generators/templates/controllers/passwords_controller.erb @@ -1,4 +1,4 @@ -class <%= @scope_module %>::PasswordsController < Devise::PasswordsController +class <%= @scope_prefix %>PasswordsController < Devise::PasswordsController # GET /resource/password/new def new super diff --git a/lib/generators/templates/controllers/registrations_controller.erb b/lib/generators/templates/controllers/registrations_controller.erb index 8916b30a..32bf8196 100644 --- a/lib/generators/templates/controllers/registrations_controller.erb +++ b/lib/generators/templates/controllers/registrations_controller.erb @@ -1,4 +1,4 @@ -class <%= @scope_module %>::RegistrationsController < Devise::RegistrationsController +class <%= @scope_prefix %>RegistrationsController < Devise::RegistrationsController # GET /resource/sign_up def new super diff --git a/lib/generators/templates/controllers/sessions_controller.erb b/lib/generators/templates/controllers/sessions_controller.erb index acd52dcb..beedf72b 100644 --- a/lib/generators/templates/controllers/sessions_controller.erb +++ b/lib/generators/templates/controllers/sessions_controller.erb @@ -1,4 +1,4 @@ -module <%= @scope_module %>::SessionsController < Devise::SessionsController +class <%= @scope_prefix %>SessionsController < Devise::SessionsController # GET /resource/sign_in def new super diff --git a/lib/generators/templates/controllers/unlocks_controller.erb b/lib/generators/templates/controllers/unlocks_controller.erb index a4d2edf6..61fb2f44 100644 --- a/lib/generators/templates/controllers/unlocks_controller.erb +++ b/lib/generators/templates/controllers/unlocks_controller.erb @@ -1,4 +1,4 @@ -class <%= @scope_module %>::UnlocksController < Devise::UnlocksController +class <%= @scope_prefix %>UnlocksController < Devise::UnlocksController # GET /resource/unlock/new def new super From 992ee86b4123132abe939d0304c8b4306ca92d26 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Sun, 24 Aug 2014 22:05:54 -0500 Subject: [PATCH 06/33] add test for controllers generator --- test/generators/controllers_generator_test.rb | 47 +++++++++++++++++++ test/test_helper.rb | 1 + 2 files changed, 48 insertions(+) create mode 100644 test/generators/controllers_generator_test.rb diff --git a/test/generators/controllers_generator_test.rb b/test/generators/controllers_generator_test.rb new file mode 100644 index 00000000..e3363b6a --- /dev/null +++ b/test/generators/controllers_generator_test.rb @@ -0,0 +1,47 @@ +require "test_helper" + +class ControllersGeneratorTest < Rails::Generators::TestCase + tests Devise::Generators::ControllersGenerator + destination File.expand_path("../../tmp", __FILE__) + setup :prepare_destination + + test "Assert all controllers are properly created with no params" do + run_generator + assert_class_names + end + + test "Assert all controllers are properly created with scope param" do + run_generator %w(users) + assert_class_names 'users' + + run_generator %w(admins) + assert_class_names 'admins' + end + + test "Assert only controllers with specific names" do + run_generator %w(-c sessions registrations) + assert_file "app/controllers/sessions_controller.rb" + assert_file "app/controllers/registrations_controller.rb" + assert_no_file "app/controllers/confirmations_controller.rb" + assert_no_file "app/controllers/passwords_controller.rb" + assert_no_file "app/controllers/unlocks_controller.rb" + assert_no_file "app/controllers/omniauth_callbacks_controller.rb" + end + + test "Assert specified controllers with scope" do + run_generator %w(users -c sessions) + assert_file "app/controllers/users/sessions_controller.rb" + assert_no_file "app/controllers/users/confirmations_controller.rb" + end + + def assert_class_names(scope = nil, options = {}) + base_dir = "app/controllers#{scope.blank? ? '' : ('/' + scope)}" + scope_prefix = scope.blank? ? '' : (scope.camelize + '::') + controllers = options[:controllers] || + %w(confirmations passwords registrations sessions unlocks omniauth_callbacks) + + controllers.each do |c| + assert_file "#{base_dir}/#{c}_controller.rb", /#{scope_prefix + c.camelize}/ + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index dd200611..49553d82 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -27,3 +27,4 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } require "rails/generators/test_case" require "generators/devise/install_generator" require "generators/devise/views_generator" +require "generators/devise/controllers_generator" From af47bb5542874d296bb6cd0c4d73c0f23dd48db5 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Sun, 24 Aug 2014 22:41:00 -0500 Subject: [PATCH 07/33] update description message in the controllers generator --- lib/generators/devise/controllers_generator.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/generators/devise/controllers_generator.rb b/lib/generators/devise/controllers_generator.rb index 46b50dea..1ee08eae 100644 --- a/lib/generators/devise/controllers_generator.rb +++ b/lib/generators/devise/controllers_generator.rb @@ -19,7 +19,8 @@ This will create a controller class at app/controllers/users/sessions_controller > content... > end -Note: you MUST specify a scope(like users here) and config your route.rb file accordingly like this: +Note: you also need to override routes in your route.rb file accordingly. +For example, you can do it like this: > Rails.application.routes.draw do > content... > devise_for :users, controllers: { From 220c8ff089e797c9f230e4c06a10e6af4a86542c Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Wed, 27 Aug 2014 23:19:43 -0500 Subject: [PATCH 08/33] print README instruction after running controller generator --- .../devise/controllers_generator.rb | 24 +++++++++---------- lib/generators/templates/README | 4 ++++ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/generators/devise/controllers_generator.rb b/lib/generators/devise/controllers_generator.rb index 1ee08eae..0f275630 100644 --- a/lib/generators/devise/controllers_generator.rb +++ b/lib/generators/devise/controllers_generator.rb @@ -12,23 +12,17 @@ Create inherited Devise controllers in your app/controllers folder. User -c to specify which controller you want to overwrite. If you do no specify a controller, all controllers will be created. +For example: + + rails generate devise:controllers users -c=sessions -Usage example: rails generate devise:controllers users -c=sessions This will create a controller class at app/controllers/users/sessions_controller.rb like this: -> class Users::ConfirmationsController < Devise::ConfirmationsController -> content... -> end -Note: you also need to override routes in your route.rb file accordingly. -For example, you can do it like this: -> Rails.application.routes.draw do -> content... -> devise_for :users, controllers: { -> sessions: 'users/sessions', -> other controller... -> } -> end + class Users::ConfirmationsController < Devise::ConfirmationsController + content... + end DESC + source_root File.expand_path("../../templates/controllers", __FILE__) argument :scope, required: false, default: nil, desc: "The scope to create controllers in, e.g. users, admins" @@ -42,6 +36,10 @@ For example, you can do it like this: "app/controllers/#{scope}/#{name}_controller.rb" end end + + def show_readme + readme "README" if behavior == :invoke + end end end end diff --git a/lib/generators/templates/README b/lib/generators/templates/README index 001e6e5a..985dfe94 100644 --- a/lib/generators/templates/README +++ b/lib/generators/templates/README @@ -32,4 +32,8 @@ Some setup you must do manually if you haven't yet: rails g devise:views + 6. You can generate inherited controller templates by running: + + rails g devise:controllers + =============================================================================== From 316be42ed6167e813e901256a6d3f3811986cbb8 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Wed, 27 Aug 2014 23:20:26 -0500 Subject: [PATCH 09/33] add instruction in the README file for controllers generator. This readme will be printed when user invoke devise:install --- lib/generators/templates/controllers/README | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 lib/generators/templates/controllers/README diff --git a/lib/generators/templates/controllers/README b/lib/generators/templates/controllers/README new file mode 100644 index 00000000..b130ed33 --- /dev/null +++ b/lib/generators/templates/controllers/README @@ -0,0 +1,14 @@ +=============================================================================== + +Some setup you must do manually if you haven't yet: + + Ensure you have overridden routes for generated controllers in your route.rb. + For example: + + Rails.application.routes.draw do + devise_for :users, controllers: { + sessions: 'users/sessions' + } + end + +=============================================================================== From 274daad005b2afa674ff5e30d1af63da4534bae3 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Wed, 27 Aug 2014 23:25:48 -0500 Subject: [PATCH 10/33] fix code example shown in README user will see after running generator --- lib/generators/templates/controllers/README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/templates/controllers/README b/lib/generators/templates/controllers/README index b130ed33..d8fa757f 100644 --- a/lib/generators/templates/controllers/README +++ b/lib/generators/templates/controllers/README @@ -7,7 +7,7 @@ Some setup you must do manually if you haven't yet: Rails.application.routes.draw do devise_for :users, controllers: { - sessions: 'users/sessions' + sessions: 'sessions' } end From 6117a3b56d8f599622d06c11e70f49096165696a Mon Sep 17 00:00:00 2001 From: Anton Rieder Date: Tue, 26 Aug 2014 15:54:49 +0200 Subject: [PATCH 11/33] Update Simple Form templates for 3.3.0 --- .../templates/simple_form_for/registrations/new.html.erb | 2 +- .../templates/simple_form_for/sessions/new.html.erb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/generators/templates/simple_form_for/registrations/new.html.erb b/lib/generators/templates/simple_form_for/registrations/new.html.erb index 090fb295..4ea8fb95 100644 --- a/lib/generators/templates/simple_form_for/registrations/new.html.erb +++ b/lib/generators/templates/simple_form_for/registrations/new.html.erb @@ -5,7 +5,7 @@
<%= f.input :email, required: true, autofocus: true %> - <%= f.input :password, required: true %> + <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @validatable) %> <%= f.input :password_confirmation, required: true %>
diff --git a/lib/generators/templates/simple_form_for/sessions/new.html.erb b/lib/generators/templates/simple_form_for/sessions/new.html.erb index c790b498..31f8a4d4 100644 --- a/lib/generators/templates/simple_form_for/sessions/new.html.erb +++ b/lib/generators/templates/simple_form_for/sessions/new.html.erb @@ -1,4 +1,4 @@ -

Sign in

+

Log in

<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
@@ -8,7 +8,7 @@
- <%= f.button :submit, "Sign in" %> + <%= f.button :submit, "Log in" %>
<% end %> From 930bed7a103f162c80a87e30190d05a0d3d98c94 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 1 Sep 2014 19:38:07 -0300 Subject: [PATCH 12/33] Add changelog for #3175 [ci skip] --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6968e7c0..225199d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ### Unreleased * enhancements + * The Simple Form templates follow the same change from 3.3.0 by using `Log in` and adding + a hint about the minimum password length when `validatable` is enabled (by @aried3r) + * bug fixes ### 3.3.0 From 6af18ed9e3f2344ac020d51fe6e06a9ee245ee0b Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Tue, 2 Sep 2014 11:24:31 -0300 Subject: [PATCH 13/33] Change the 'postgresql?' check to not connect to the database. Closes #3181 --- lib/generators/active_record/devise_generator.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/generators/active_record/devise_generator.rb b/lib/generators/active_record/devise_generator.rb index 2ab56b3f..7394bd84 100644 --- a/lib/generators/active_record/devise_generator.rb +++ b/lib/generators/active_record/devise_generator.rb @@ -83,7 +83,8 @@ RUBY end def postgresql? - ActiveRecord::Base.connection.adapter_name.downcase == "postgresql" + config = ActiveRecord::Base.configurations[Rails.env] + config && config['adapter'] == 'postgresql' end end end From 6ba87ed339c255e932812b1392122c9a3f28e1d2 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Tue, 2 Sep 2014 22:34:13 -0500 Subject: [PATCH 14/33] comment out all stubs --- .../controllers/confirmations_controller.erb | 18 ++++----- .../omniauth_callbacks_controller.erb | 12 +++--- .../controllers/passwords_controller.erb | 24 +++++------ .../controllers/registrations_controller.erb | 40 +++++++++---------- .../controllers/sessions_controller.erb | 18 ++++----- .../controllers/unlocks_controller.erb | 18 ++++----- 6 files changed, 63 insertions(+), 67 deletions(-) diff --git a/lib/generators/templates/controllers/confirmations_controller.erb b/lib/generators/templates/controllers/confirmations_controller.erb index 520e5e23..d8446a8b 100644 --- a/lib/generators/templates/controllers/confirmations_controller.erb +++ b/lib/generators/templates/controllers/confirmations_controller.erb @@ -1,16 +1,16 @@ class <%= @scope_prefix %>ConfirmationsController < Devise::ConfirmationsController # GET /resource/confirmation/new - def new - super - end + # def new + # super + # end # POST /resource/confirmation - def create - super - end + # def create + # super + # end # GET /resource/confirmation?confirmation_token=abcdef - def show - super - end + # def show + # super + # end end diff --git a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb index 4769fd92..dce4fb64 100644 --- a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb +++ b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb @@ -10,12 +10,12 @@ class <%= @scope_prefix %>OmniauthCallbacksController < Devise::OmniauthCallback # https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview # GET|POST /resource/auth/twitter - def passthru - super - end + # def passthru + # super + # end # GET|POST /users/auth/twitter/callback - def failure - super - end + # def failure + # super + # end end diff --git a/lib/generators/templates/controllers/passwords_controller.erb b/lib/generators/templates/controllers/passwords_controller.erb index 17b7573c..af02f9f3 100644 --- a/lib/generators/templates/controllers/passwords_controller.erb +++ b/lib/generators/templates/controllers/passwords_controller.erb @@ -1,21 +1,21 @@ class <%= @scope_prefix %>PasswordsController < Devise::PasswordsController # GET /resource/password/new - def new - super - end + # def new + # super + # end # POST /resource/password - def create - super - end + # def create + # super + # end # GET /resource/password/edit?reset_password_token=abcdef - def edit - super - end + # def edit + # super + # end # PUT /resource/password - def update - super - end + # def update + # super + # end end diff --git a/lib/generators/templates/controllers/registrations_controller.erb b/lib/generators/templates/controllers/registrations_controller.erb index 32bf8196..98986e85 100644 --- a/lib/generators/templates/controllers/registrations_controller.erb +++ b/lib/generators/templates/controllers/registrations_controller.erb @@ -1,35 +1,31 @@ class <%= @scope_prefix %>RegistrationsController < Devise::RegistrationsController # GET /resource/sign_up - def new - super - end + # def new + # super + # end # POST /resource - def create - super - end + # def create + # super + # end # GET /resource/edit - def edit - super - end + # def edit + # super + # end # PUT /resource - def update - super - end + # def update + # super + # end # DELETE /resource - def destroy - super - end + # def destroy + # super + # end # GET /resource/cancel - # Forces the session data which is usually expired after sign - # in to be expired now. This is useful if the user wants to - # cancel oauth signing in/up in the middle of the process, - # removing all OAuth session data. - def cancel - super - end + # def cancel + # super + # end end diff --git a/lib/generators/templates/controllers/sessions_controller.erb b/lib/generators/templates/controllers/sessions_controller.erb index beedf72b..941abdb0 100644 --- a/lib/generators/templates/controllers/sessions_controller.erb +++ b/lib/generators/templates/controllers/sessions_controller.erb @@ -1,16 +1,16 @@ class <%= @scope_prefix %>SessionsController < Devise::SessionsController # GET /resource/sign_in - def new - super - end + # def new + # super + # end # POST /resource/sign_in - def create - super - end + # def create + # super + # end # DELETE /resource/sign_out - def destroy - super - end + # def destroy + # super + # end end diff --git a/lib/generators/templates/controllers/unlocks_controller.erb b/lib/generators/templates/controllers/unlocks_controller.erb index 61fb2f44..b5ae5071 100644 --- a/lib/generators/templates/controllers/unlocks_controller.erb +++ b/lib/generators/templates/controllers/unlocks_controller.erb @@ -1,16 +1,16 @@ class <%= @scope_prefix %>UnlocksController < Devise::UnlocksController # GET /resource/unlock/new - def new - super - end + # def new + # super + # end # POST /resource/unlock - def create - super - end + # def create + # super + # end # GET /resource/unlock?unlock_token=abcdef - def show - super - end + # def show + # super + # end end From 2f81a832326674ab907471444c548c9d26e6aeec Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Tue, 2 Sep 2014 22:54:06 -0500 Subject: [PATCH 15/33] refactor code, use a constant insead of a class variable --- .../devise/controllers_generator.rb | 32 +++++++++---------- test/generators/controllers_generator_test.rb | 18 ++++++----- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/generators/devise/controllers_generator.rb b/lib/generators/devise/controllers_generator.rb index 0f275630..537ffb48 100644 --- a/lib/generators/devise/controllers_generator.rb +++ b/lib/generators/devise/controllers_generator.rb @@ -3,34 +3,33 @@ require 'rails/generators/base' module Devise module Generators class ControllersGenerator < Rails::Generators::Base - def self.all_controllers - @@controllers ||= %w(confirmations passwords registrations sessions unlocks omniauth_callbacks) - end + CONTROLLERS = %w(confirmations passwords registrations sessions unlocks omniauth_callbacks).freeze - desc <<-DESC -Create inherited Devise controllers in your app/controllers folder. + desc <<-DESC.strip_heredoc + Create inherited Devise controllers in your app/controllers folder. -User -c to specify which controller you want to overwrite. -If you do no specify a controller, all controllers will be created. -For example: + User -c to specify which controller you want to overwrite. + If you do no specify a controller, all controllers will be created. + For example: - rails generate devise:controllers users -c=sessions + rails generate devise:controllers users -c=sessions -This will create a controller class at app/controllers/users/sessions_controller.rb like this: + This will create a controller class at app/controllers/users/sessions_controller.rb like this: - class Users::ConfirmationsController < Devise::ConfirmationsController - content... - end + class Users::ConfirmationsController < Devise::ConfirmationsController + content... + end DESC source_root File.expand_path("../../templates/controllers", __FILE__) argument :scope, required: false, default: nil, - desc: "The scope to create controllers in, e.g. users, admins" - class_option :controllers, aliases: "-c", type: :array, desc: "Select specific controllers to generate (#{all_controllers.join(', ')})" + desc: "The scope to create controllers in, e.g. users, admins" + class_option :controllers, aliases: "-c", type: :array, + desc: "Select specific controllers to generate (#{CONTROLLERS.join(', ')})" def create_controllers @scope_prefix = scope.blank? ? '' : (scope.camelize + '::') - controllers = options[:controllers] || self.class.all_controllers + controllers = options[:controllers] || CONTROLLERS controllers.each do |name| template "#{name}_controller.erb", "app/controllers/#{scope}/#{name}_controller.rb" @@ -43,4 +42,3 @@ This will create a controller class at app/controllers/users/sessions_controller end end end - diff --git a/test/generators/controllers_generator_test.rb b/test/generators/controllers_generator_test.rb index e3363b6a..759ba7dc 100644 --- a/test/generators/controllers_generator_test.rb +++ b/test/generators/controllers_generator_test.rb @@ -34,14 +34,16 @@ class ControllersGeneratorTest < Rails::Generators::TestCase assert_no_file "app/controllers/users/confirmations_controller.rb" end - def assert_class_names(scope = nil, options = {}) - base_dir = "app/controllers#{scope.blank? ? '' : ('/' + scope)}" - scope_prefix = scope.blank? ? '' : (scope.camelize + '::') - controllers = options[:controllers] || - %w(confirmations passwords registrations sessions unlocks omniauth_callbacks) + private - controllers.each do |c| - assert_file "#{base_dir}/#{c}_controller.rb", /#{scope_prefix + c.camelize}/ + def assert_class_names(scope = nil, options = {}) + base_dir = "app/controllers#{scope.blank? ? '' : ('/' + scope)}" + scope_prefix = scope.blank? ? '' : (scope.camelize + '::') + controllers = options[:controllers] || + %w(confirmations passwords registrations sessions unlocks omniauth_callbacks) + + controllers.each do |c| + assert_file "#{base_dir}/#{c}_controller.rb", /#{scope_prefix + c.camelize}/ + end end - end end From daad552e1b13c5fe68114ce563ca1d83e44fc1b6 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Tue, 2 Sep 2014 23:04:27 -0500 Subject: [PATCH 16/33] make scope required --- .../devise/controllers_generator.rb | 2 +- test/generators/controllers_generator_test.rb | 25 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/generators/devise/controllers_generator.rb b/lib/generators/devise/controllers_generator.rb index 537ffb48..721c5e49 100644 --- a/lib/generators/devise/controllers_generator.rb +++ b/lib/generators/devise/controllers_generator.rb @@ -22,7 +22,7 @@ module Devise DESC source_root File.expand_path("../../templates/controllers", __FILE__) - argument :scope, required: false, default: nil, + argument :scope, required: true, desc: "The scope to create controllers in, e.g. users, admins" class_option :controllers, aliases: "-c", type: :array, desc: "Select specific controllers to generate (#{CONTROLLERS.join(', ')})" diff --git a/test/generators/controllers_generator_test.rb b/test/generators/controllers_generator_test.rb index 759ba7dc..11d1c35d 100644 --- a/test/generators/controllers_generator_test.rb +++ b/test/generators/controllers_generator_test.rb @@ -5,9 +5,14 @@ class ControllersGeneratorTest < Rails::Generators::TestCase destination File.expand_path("../../tmp", __FILE__) setup :prepare_destination - test "Assert all controllers are properly created with no params" do + test "Assert no controllers are created with no params" do run_generator - assert_class_names + assert_no_file "app/controllers/sessions_controller.rb" + assert_no_file "app/controllers/registrations_controller.rb" + assert_no_file "app/controllers/confirmations_controller.rb" + assert_no_file "app/controllers/passwords_controller.rb" + assert_no_file "app/controllers/unlocks_controller.rb" + assert_no_file "app/controllers/omniauth_callbacks_controller.rb" end test "Assert all controllers are properly created with scope param" do @@ -18,25 +23,19 @@ class ControllersGeneratorTest < Rails::Generators::TestCase assert_class_names 'admins' end - test "Assert only controllers with specific names" do - run_generator %w(-c sessions registrations) - assert_file "app/controllers/sessions_controller.rb" - assert_file "app/controllers/registrations_controller.rb" - assert_no_file "app/controllers/confirmations_controller.rb" - assert_no_file "app/controllers/passwords_controller.rb" - assert_no_file "app/controllers/unlocks_controller.rb" - assert_no_file "app/controllers/omniauth_callbacks_controller.rb" - end - test "Assert specified controllers with scope" do run_generator %w(users -c sessions) assert_file "app/controllers/users/sessions_controller.rb" + assert_no_file "app/controllers/users/registrations_controller.rb" assert_no_file "app/controllers/users/confirmations_controller.rb" + assert_no_file "app/controllers/users/passwords_controller.rb" + assert_no_file "app/controllers/users/unlocks_controller.rb" + assert_no_file "app/controllers/users/omniauth_callbacks_controller.rb" end private - def assert_class_names(scope = nil, options = {}) + def assert_class_names(scope, options = {}) base_dir = "app/controllers#{scope.blank? ? '' : ('/' + scope)}" scope_prefix = scope.blank? ? '' : (scope.camelize + '::') controllers = options[:controllers] || From e2e9e90d6953a2a3f7b980540e42aa8907f70bf4 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Tue, 2 Sep 2014 23:25:38 -0500 Subject: [PATCH 17/33] move controller generator info from lib/generators/templates/README to README --- README.md | 16 +++++++++++++--- lib/generators/templates/README | 4 ---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 76cc78cf..79e992c5 100644 --- a/README.md +++ b/README.md @@ -282,15 +282,25 @@ rails generate devise:views -v registrations confirmations If the customization at the views level is not enough, you can customize each controller by following these steps: -1. Create your custom controller, for example a `Admins::SessionsController`: +1. Create your custom controllers using the generator which requires a scope: + + ```console + rails generate devise:controllers [scope] + ``` + + If you specify `admins` as the scope, controllers will be created in `app/controllers/admins/`. + And the sessions controller will look like this: ```ruby class Admins::SessionsController < Devise::SessionsController + # GET /resource/sign_in + # def new + # super + # end + ... end ``` - Note that in the above example, the controller needs to be created in the `app/controllers/admins/` directory. - 2. Tell the router to use this controller: ```ruby diff --git a/lib/generators/templates/README b/lib/generators/templates/README index 985dfe94..001e6e5a 100644 --- a/lib/generators/templates/README +++ b/lib/generators/templates/README @@ -32,8 +32,4 @@ Some setup you must do manually if you haven't yet: rails g devise:views - 6. You can generate inherited controller templates by running: - - rails g devise:controllers - =============================================================================== From 86ac552287eb3db9934641e761eaa3ba5b8ee7ee Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Tue, 2 Sep 2014 23:49:37 -0500 Subject: [PATCH 18/33] add *_path_for stubs in all controllers --- .../controllers/confirmations_controller.erb | 12 ++++++++++++ .../omniauth_callbacks_controller.erb | 7 +++++++ .../controllers/passwords_controller.erb | 11 +++++++++++ .../controllers/registrations_controller.erb | 16 ++++++++++++++++ .../templates/controllers/unlocks_controller.erb | 12 ++++++++++++ 5 files changed, 58 insertions(+) diff --git a/lib/generators/templates/controllers/confirmations_controller.erb b/lib/generators/templates/controllers/confirmations_controller.erb index d8446a8b..a92d3703 100644 --- a/lib/generators/templates/controllers/confirmations_controller.erb +++ b/lib/generators/templates/controllers/confirmations_controller.erb @@ -13,4 +13,16 @@ class <%= @scope_prefix %>ConfirmationsController < Devise::ConfirmationsControl # def show # super # end + + # protected + + # The path used after resending confirmation instructions. + # def after_resending_confirmation_instructions_path_for(resource_name) + # super(resource_name) + # end + + # The path used after confirmation. + # def after_confirmation_path_for(resource_name, resource) + # super(resource_name, resource) + # end end diff --git a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb index dce4fb64..a54181c2 100644 --- a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb +++ b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb @@ -18,4 +18,11 @@ class <%= @scope_prefix %>OmniauthCallbacksController < Devise::OmniauthCallback # def failure # super # end + + # protected + + # The path used when omniauth fails + # def after_omniauth_failure_path_for(scope) + # super(scope) + # end end diff --git a/lib/generators/templates/controllers/passwords_controller.erb b/lib/generators/templates/controllers/passwords_controller.erb index af02f9f3..fe0514fa 100644 --- a/lib/generators/templates/controllers/passwords_controller.erb +++ b/lib/generators/templates/controllers/passwords_controller.erb @@ -18,4 +18,15 @@ class <%= @scope_prefix %>PasswordsController < Devise::PasswordsController # def update # super # end + + # protected + + # def after_resetting_password_path_for(resource) + # super(resource) + # end + + # The path used after sending reset password instructions + # def after_sending_reset_password_instructions_path_for(resource_name) + # super(resource_name) + # end end diff --git a/lib/generators/templates/controllers/registrations_controller.erb b/lib/generators/templates/controllers/registrations_controller.erb index 98986e85..9425e758 100644 --- a/lib/generators/templates/controllers/registrations_controller.erb +++ b/lib/generators/templates/controllers/registrations_controller.erb @@ -25,7 +25,23 @@ class <%= @scope_prefix %>RegistrationsController < Devise::RegistrationsControl # end # GET /resource/cancel + # Forces the session data which is usually expired after sign + # in to be expired now. This is useful if the user wants to + # cancel oauth signing in/up in the middle of the process, + # removing all OAuth session data. # def cancel # super # end + + # protected + + # The path used after sign up. + # def after_sign_up_path_for(resource) + # super(resource) + # end + + # The path used after sign up for inactive accounts. + # def after_inactive_sign_up_path_for(resource) + # super(resource) + # end end diff --git a/lib/generators/templates/controllers/unlocks_controller.erb b/lib/generators/templates/controllers/unlocks_controller.erb index b5ae5071..42aa96bf 100644 --- a/lib/generators/templates/controllers/unlocks_controller.erb +++ b/lib/generators/templates/controllers/unlocks_controller.erb @@ -13,4 +13,16 @@ class <%= @scope_prefix %>UnlocksController < Devise::UnlocksController # def show # super # end + + # protected + + # The path used after sending unlock password instructions + # def after_sending_unlock_instructions_path_for(resource) + # super(resource) + # end + + # The path used after unlocking the resource + # def after_unlock_path_for(resource) + # super(resource) + # end end From a9095b86d4f5d53f7dd77f231762a03d3f7f6ae2 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Wed, 3 Sep 2014 00:02:07 -0500 Subject: [PATCH 19/33] fix typo: 'new' action should be 'create' action --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 79e992c5..28aad919 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ When you customize your own views, you may end up adding new attributes to forms There are just three actions in Devise that allows any set of parameters to be passed down to the model, therefore requiring sanitization. Their names and the permitted parameters by default are: -* `sign_in` (`Devise::SessionsController#new`) - Permits only the authentication keys (like `email`) +* `sign_in` (`Devise::SessionsController#create`) - Permits only the authentication keys (like `email`) * `sign_up` (`Devise::RegistrationsController#create`) - Permits authentication keys plus `password` and `password_confirmation` * `account_update` (`Devise::RegistrationsController#update`) - Permits authentication keys plus `password`, `password_confirmation` and `current_password` From 67dc138d949b15f9d203372f54f62c71ae591c41 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Wed, 3 Sep 2014 00:10:32 -0500 Subject: [PATCH 20/33] add before_action hooks for registrations and sessions controllers to enable user to add extra params --- .../controllers/registrations_controller.erb | 17 +++++++++++++++++ .../controllers/sessions_controller.erb | 11 +++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/generators/templates/controllers/registrations_controller.erb b/lib/generators/templates/controllers/registrations_controller.erb index 9425e758..3acbafd9 100644 --- a/lib/generators/templates/controllers/registrations_controller.erb +++ b/lib/generators/templates/controllers/registrations_controller.erb @@ -1,4 +1,7 @@ class <%= @scope_prefix %>RegistrationsController < Devise::RegistrationsController +# before_action :configure_sign_up_params, only: [:create] +# before_action :configure_account_update_params, only: [:update] + # GET /resource/sign_up # def new # super @@ -35,6 +38,20 @@ class <%= @scope_prefix %>RegistrationsController < Devise::RegistrationsControl # protected + # You can put the params you want to permit in the empty array. + # def configure_sign_up_params + # [].each do |field| + # devise_parameter_sanitizer.for(:sign_up) << field.to_sym + # end + # end + + # You can put the params you want to permit in the empty array. + # def configure_account_update_params + # [].each do |field| + # devise_parameter_sanitizer.for(:account_update) << field.to_sym + # end + # end + # The path used after sign up. # def after_sign_up_path_for(resource) # super(resource) diff --git a/lib/generators/templates/controllers/sessions_controller.erb b/lib/generators/templates/controllers/sessions_controller.erb index 941abdb0..1384a9ab 100644 --- a/lib/generators/templates/controllers/sessions_controller.erb +++ b/lib/generators/templates/controllers/sessions_controller.erb @@ -1,4 +1,6 @@ class <%= @scope_prefix %>SessionsController < Devise::SessionsController +# before_action :configure_sign_in_params, only: [:create] + # GET /resource/sign_in # def new # super @@ -13,4 +15,13 @@ class <%= @scope_prefix %>SessionsController < Devise::SessionsController # def destroy # super # end + + # protected + + # You can put the params you want to permit in the empty array. + # def configure_sign_in_params + # [].each do |field| + # devise_parameter_sanitizer.for(:sign_in) << field.to_sym + # end + # end end From cc21c87bb385831f2f3e4988428fffe0b5d3604c Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Wed, 3 Sep 2014 21:58:44 -0500 Subject: [PATCH 21/33] Update CHANGELOG [ci skip] --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 225199d9..ec66617d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ a hint about the minimum password length when `validatable` is enabled (by @aried3r) * bug fixes + * Fixed a regression where the devise generator would fail with a `ConnectionNotEstablished` + exception when executed inside a mountable engine. ### 3.3.0 From db883414024853163507818608417fe7e2118e9c Mon Sep 17 00:00:00 2001 From: Fred Grosskopf Date: Mon, 8 Sep 2014 14:21:33 +0200 Subject: [PATCH 22/33] Removes reloading of routes when eager loading ... ... is enabled. The old code may cause conflicts when developing an engine (i.e. invalid route name) and according to @josevalim the reason for it to remain is unknown. --- lib/devise/rails.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/devise/rails.rb b/lib/devise/rails.rb index 14de2375..84169810 100644 --- a/lib/devise/rails.rb +++ b/lib/devise/rails.rb @@ -10,9 +10,6 @@ module Devise Devise.warden_config = config end - # Force routes to be loaded if we are doing any eager load. - config.before_eager_load { |app| app.reload_routes! } - initializer "devise.url_helpers" do Devise.include_helpers(Devise::Controllers) end From 6003c6981806adc35d84c66a0a3c27f3b12deb0c Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Tue, 9 Sep 2014 20:31:48 -0500 Subject: [PATCH 23/33] change indentation --- .../controllers/confirmations_controller.erb | 16 ++++---- .../omniauth_callbacks_controller.erb | 8 ++-- .../controllers/passwords_controller.erb | 14 +++---- .../controllers/registrations_controller.erb | 40 +++++++++---------- .../controllers/sessions_controller.erb | 12 +++--- .../controllers/unlocks_controller.erb | 16 ++++---- 6 files changed, 53 insertions(+), 53 deletions(-) diff --git a/lib/generators/templates/controllers/confirmations_controller.erb b/lib/generators/templates/controllers/confirmations_controller.erb index a92d3703..480ee9a0 100644 --- a/lib/generators/templates/controllers/confirmations_controller.erb +++ b/lib/generators/templates/controllers/confirmations_controller.erb @@ -16,13 +16,13 @@ class <%= @scope_prefix %>ConfirmationsController < Devise::ConfirmationsControl # protected - # The path used after resending confirmation instructions. - # def after_resending_confirmation_instructions_path_for(resource_name) - # super(resource_name) - # end + # The path used after resending confirmation instructions. + # def after_resending_confirmation_instructions_path_for(resource_name) + # super(resource_name) + # end - # The path used after confirmation. - # def after_confirmation_path_for(resource_name, resource) - # super(resource_name, resource) - # end + # The path used after confirmation. + # def after_confirmation_path_for(resource_name, resource) + # super(resource_name, resource) + # end end diff --git a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb index a54181c2..d6838366 100644 --- a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb +++ b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb @@ -21,8 +21,8 @@ class <%= @scope_prefix %>OmniauthCallbacksController < Devise::OmniauthCallback # protected - # The path used when omniauth fails - # def after_omniauth_failure_path_for(scope) - # super(scope) - # end + # The path used when omniauth fails + # def after_omniauth_failure_path_for(scope) + # super(scope) + # end end diff --git a/lib/generators/templates/controllers/passwords_controller.erb b/lib/generators/templates/controllers/passwords_controller.erb index fe0514fa..3409d435 100644 --- a/lib/generators/templates/controllers/passwords_controller.erb +++ b/lib/generators/templates/controllers/passwords_controller.erb @@ -21,12 +21,12 @@ class <%= @scope_prefix %>PasswordsController < Devise::PasswordsController # protected - # def after_resetting_password_path_for(resource) - # super(resource) - # end + # def after_resetting_password_path_for(resource) + # super(resource) + # end - # The path used after sending reset password instructions - # def after_sending_reset_password_instructions_path_for(resource_name) - # super(resource_name) - # end + # The path used after sending reset password instructions + # def after_sending_reset_password_instructions_path_for(resource_name) + # super(resource_name) + # end end diff --git a/lib/generators/templates/controllers/registrations_controller.erb b/lib/generators/templates/controllers/registrations_controller.erb index 3acbafd9..579adfaa 100644 --- a/lib/generators/templates/controllers/registrations_controller.erb +++ b/lib/generators/templates/controllers/registrations_controller.erb @@ -38,27 +38,27 @@ class <%= @scope_prefix %>RegistrationsController < Devise::RegistrationsControl # protected - # You can put the params you want to permit in the empty array. - # def configure_sign_up_params - # [].each do |field| - # devise_parameter_sanitizer.for(:sign_up) << field.to_sym - # end - # end + # You can put the params you want to permit in the empty array. + # def configure_sign_up_params + # [].each do |field| + # devise_parameter_sanitizer.for(:sign_up) << field.to_sym + # end + # end - # You can put the params you want to permit in the empty array. - # def configure_account_update_params - # [].each do |field| - # devise_parameter_sanitizer.for(:account_update) << field.to_sym - # end - # end + # You can put the params you want to permit in the empty array. + # def configure_account_update_params + # [].each do |field| + # devise_parameter_sanitizer.for(:account_update) << field.to_sym + # end + # end - # The path used after sign up. - # def after_sign_up_path_for(resource) - # super(resource) - # end + # The path used after sign up. + # def after_sign_up_path_for(resource) + # super(resource) + # end - # The path used after sign up for inactive accounts. - # def after_inactive_sign_up_path_for(resource) - # super(resource) - # end + # The path used after sign up for inactive accounts. + # def after_inactive_sign_up_path_for(resource) + # super(resource) + # end end diff --git a/lib/generators/templates/controllers/sessions_controller.erb b/lib/generators/templates/controllers/sessions_controller.erb index 1384a9ab..f5b77176 100644 --- a/lib/generators/templates/controllers/sessions_controller.erb +++ b/lib/generators/templates/controllers/sessions_controller.erb @@ -18,10 +18,10 @@ class <%= @scope_prefix %>SessionsController < Devise::SessionsController # protected - # You can put the params you want to permit in the empty array. - # def configure_sign_in_params - # [].each do |field| - # devise_parameter_sanitizer.for(:sign_in) << field.to_sym - # end - # end + # You can put the params you want to permit in the empty array. + # def configure_sign_in_params + # [].each do |field| + # devise_parameter_sanitizer.for(:sign_in) << field.to_sym + # end + # end end diff --git a/lib/generators/templates/controllers/unlocks_controller.erb b/lib/generators/templates/controllers/unlocks_controller.erb index 42aa96bf..0e2cced5 100644 --- a/lib/generators/templates/controllers/unlocks_controller.erb +++ b/lib/generators/templates/controllers/unlocks_controller.erb @@ -16,13 +16,13 @@ class <%= @scope_prefix %>UnlocksController < Devise::UnlocksController # protected - # The path used after sending unlock password instructions - # def after_sending_unlock_instructions_path_for(resource) - # super(resource) - # end + # The path used after sending unlock password instructions + # def after_sending_unlock_instructions_path_for(resource) + # super(resource) + # end - # The path used after unlocking the resource - # def after_unlock_path_for(resource) - # super(resource) - # end + # The path used after unlocking the resource + # def after_unlock_path_for(resource) + # super(resource) + # end end From 17f6c76dc701d475e2787fa89eb6a3af0da8b60d Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Tue, 9 Sep 2014 20:35:46 -0500 Subject: [PATCH 24/33] simplify sanitizer in *_params function --- .../templates/controllers/registrations_controller.erb | 8 ++------ .../templates/controllers/sessions_controller.erb | 4 +--- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/generators/templates/controllers/registrations_controller.erb b/lib/generators/templates/controllers/registrations_controller.erb index 579adfaa..e08d0e6f 100644 --- a/lib/generators/templates/controllers/registrations_controller.erb +++ b/lib/generators/templates/controllers/registrations_controller.erb @@ -40,16 +40,12 @@ class <%= @scope_prefix %>RegistrationsController < Devise::RegistrationsControl # You can put the params you want to permit in the empty array. # def configure_sign_up_params - # [].each do |field| - # devise_parameter_sanitizer.for(:sign_up) << field.to_sym - # end + # devise_parameter_sanitizer.for(:sign_up) << :attribute # end # You can put the params you want to permit in the empty array. # def configure_account_update_params - # [].each do |field| - # devise_parameter_sanitizer.for(:account_update) << field.to_sym - # end + # devise_parameter_sanitizer.for(:account_update) << :attribute # end # The path used after sign up. diff --git a/lib/generators/templates/controllers/sessions_controller.erb b/lib/generators/templates/controllers/sessions_controller.erb index f5b77176..b750038e 100644 --- a/lib/generators/templates/controllers/sessions_controller.erb +++ b/lib/generators/templates/controllers/sessions_controller.erb @@ -20,8 +20,6 @@ class <%= @scope_prefix %>SessionsController < Devise::SessionsController # You can put the params you want to permit in the empty array. # def configure_sign_in_params - # [].each do |field| - # devise_parameter_sanitizer.for(:sign_in) << field.to_sym - # end + # devise_parameter_sanitizer.for(:sign_in) << :attribute # end end From 439112adf3abadf2f1dc7e8352533754e8e4f6d8 Mon Sep 17 00:00:00 2001 From: Chun-Yang Date: Tue, 9 Sep 2014 20:40:05 -0500 Subject: [PATCH 25/33] change before_action to before_filter to make it backward compatible to rails 3.2 --- .../templates/controllers/registrations_controller.erb | 4 ++-- lib/generators/templates/controllers/sessions_controller.erb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/generators/templates/controllers/registrations_controller.erb b/lib/generators/templates/controllers/registrations_controller.erb index e08d0e6f..3566e453 100644 --- a/lib/generators/templates/controllers/registrations_controller.erb +++ b/lib/generators/templates/controllers/registrations_controller.erb @@ -1,6 +1,6 @@ class <%= @scope_prefix %>RegistrationsController < Devise::RegistrationsController -# before_action :configure_sign_up_params, only: [:create] -# before_action :configure_account_update_params, only: [:update] +# before_filter :configure_sign_up_params, only: [:create] +# before_filter :configure_account_update_params, only: [:update] # GET /resource/sign_up # def new diff --git a/lib/generators/templates/controllers/sessions_controller.erb b/lib/generators/templates/controllers/sessions_controller.erb index b750038e..7a243f4e 100644 --- a/lib/generators/templates/controllers/sessions_controller.erb +++ b/lib/generators/templates/controllers/sessions_controller.erb @@ -1,5 +1,5 @@ class <%= @scope_prefix %>SessionsController < Devise::SessionsController -# before_action :configure_sign_in_params, only: [:create] +# before_filter :configure_sign_in_params, only: [:create] # GET /resource/sign_in # def new From 6e0c287391f4b7c876d5bba1673c34db616d6435 Mon Sep 17 00:00:00 2001 From: "Paul A. Jungwirth" Date: Thu, 11 Sep 2014 08:38:14 -0700 Subject: [PATCH 26/33] All causes of failed login should have the same error message --- config/locales/en.yml | 2 +- test/integration/http_authenticatable_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index e419f779..fa69b16d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -12,7 +12,7 @@ en: invalid: "Invalid email or password." locked: "Your account is locked." last_attempt: "You have one more attempt before your account is locked." - not_found_in_database: "Invalid email address or password." + not_found_in_database: "Invalid email or password." timeout: "Your session expired. Please sign in again to continue." unauthenticated: "You need to sign in or sign up before continuing." unconfirmed: "You have to confirm your email address before continuing." diff --git a/test/integration/http_authenticatable_test.rb b/test/integration/http_authenticatable_test.rb index b7e770e1..aad51f62 100644 --- a/test/integration/http_authenticatable_test.rb +++ b/test/integration/http_authenticatable_test.rb @@ -42,7 +42,7 @@ class HttpAuthenticationTest < ActionDispatch::IntegrationTest sign_in_as_new_user_with_http("unknown") assert_equal 401, status assert_equal "application/xml; charset=utf-8", headers["Content-Type"] - assert_match "Invalid email address or password.", response.body + assert_match "Invalid email or password.", response.body end test 'returns a custom response with www-authenticate and chosen realm' do From 432a172533e652ff96600210ada63c1ccd0a7535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 18 Sep 2014 19:44:19 +0200 Subject: [PATCH 27/33] Set null: false explicitly in timestamps --- test/rails_app/db/migrate/20100401102949_create_tables.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/rails_app/db/migrate/20100401102949_create_tables.rb b/test/rails_app/db/migrate/20100401102949_create_tables.rb index 5aaf7364..fd90b504 100644 --- a/test/rails_app/db/migrate/20100401102949_create_tables.rb +++ b/test/rails_app/db/migrate/20100401102949_create_tables.rb @@ -33,7 +33,7 @@ class CreateTables < ActiveRecord::Migration t.string :unlock_token # Only if unlock strategy is :email or :both t.datetime :locked_at - t.timestamps + t.timestamps null: false end create_table :admins do |t| @@ -60,7 +60,7 @@ class CreateTables < ActiveRecord::Migration ## Attribute for testing route blocks t.boolean :active, default: false - t.timestamps + t.timestamps null: false end end From 73c406b48146139a5daee6652b49bb418df81c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 18 Sep 2014 19:50:52 +0200 Subject: [PATCH 28/33] Fix docs for http_authenticatable_on_xhr, closes #3112 --- lib/generators/templates/devise.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/templates/devise.rb b/lib/generators/templates/devise.rb index a2090e62..589063d0 100644 --- a/lib/generators/templates/devise.rb +++ b/lib/generators/templates/devise.rb @@ -65,7 +65,7 @@ Devise.setup do |config| # :database = Support basic authentication with authentication key + password # config.http_authenticatable = false - # If http headers should be returned for AJAX requests. True by default. + # If 401 status code should be returned for AJAX requests. True by default. # config.http_authenticatable_on_xhr = true # The realm used in Http Basic Authentication. 'Application' by default. From 7029a5aaa88fcee9a9bf25a63d0105ad97728d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 18 Sep 2014 20:04:05 +0200 Subject: [PATCH 29/33] Decouple validation from remember_me in authentication strategies Closes #3118 --- lib/devise/strategies/authenticatable.rb | 3 +-- lib/devise/strategies/database_authenticatable.rb | 1 + lib/devise/strategies/rememberable.rb | 9 ++++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/devise/strategies/authenticatable.rb b/lib/devise/strategies/authenticatable.rb index cd3a15ea..1e2055ec 100644 --- a/lib/devise/strategies/authenticatable.rb +++ b/lib/devise/strategies/authenticatable.rb @@ -36,7 +36,6 @@ module Devise result = resource && resource.valid_for_authentication?(&block) if result - decorate(resource) true else if resource @@ -47,7 +46,7 @@ module Devise end # Get values from params and set in the resource. - def decorate(resource) + def remember_me(resource) resource.remember_me = remember_me? if resource.respond_to?(:remember_me=) end diff --git a/lib/devise/strategies/database_authenticatable.rb b/lib/devise/strategies/database_authenticatable.rb index c3828279..2a8e91d0 100644 --- a/lib/devise/strategies/database_authenticatable.rb +++ b/lib/devise/strategies/database_authenticatable.rb @@ -9,6 +9,7 @@ module Devise encrypted = false if validate(resource){ encrypted = true; resource.valid_password?(password) } + remember_me(resource) resource.after_database_authentication success!(resource) end diff --git a/lib/devise/strategies/rememberable.rb b/lib/devise/strategies/rememberable.rb index 1aadde2c..69066273 100644 --- a/lib/devise/strategies/rememberable.rb +++ b/lib/devise/strategies/rememberable.rb @@ -25,15 +25,18 @@ module Devise end if validate(resource) + remember_me(resource) + extend_remember_me_period(resource) success!(resource) end end private - def decorate(resource) - super - resource.extend_remember_period = mapping.to.extend_remember_period if resource.respond_to?(:extend_remember_period=) + def extend_remember_me_period(resource) + if resource.respond_to?(:extend_remember_period=) + resource.extend_remember_period = mapping.to.extend_remember_period + end end def remember_me? From a5158939de3cdd4370be300fb2e2c06410c996eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 18 Sep 2014 20:28:24 +0200 Subject: [PATCH 30/33] Update CHANGELOG --- CHANGELOG.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec66617d..d0c53e8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,13 @@ * enhancements * The Simple Form templates follow the same change from 3.3.0 by using `Log in` and adding a hint about the minimum password length when `validatable` is enabled (by @aried3r) + * Remove reloading of routes when eager loading is enabled. This change was added during Rails 3 and it doesn't seem to be relevant to currently supported Rails versions (by @fgro) -* bug fixes +* bug fix * Fixed a regression where the devise generator would fail with a `ConnectionNotEstablished` - exception when executed inside a mountable engine. + exception when executed inside a mountable engine + * Ensure to return symbols in find_scope! fixing a previous regression from 3.3.0 (by @micat) + * Ensure all causes of failed login have the same error message (by @pjungwir) ### 3.3.0 From 79a4b93993db60f6f1529c7b5d8e4264cafe9e5c Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Thu, 18 Sep 2014 17:18:11 -0300 Subject: [PATCH 31/33] Point users to the canonical Omniauth docs in our README, not the wiki. --- .../templates/controllers/omniauth_callbacks_controller.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb index d6838366..6516213c 100644 --- a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb +++ b/lib/generators/templates/controllers/omniauth_callbacks_controller.erb @@ -7,7 +7,7 @@ class <%= @scope_prefix %>OmniauthCallbacksController < Devise::OmniauthCallback # end # More info at: - # https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview + # https://github.com/plataformatec/devise#omniauth # GET|POST /resource/auth/twitter # def passthru From 131013ac5679da46d2beb0b6625b3a04379b15bd Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Thu, 18 Sep 2014 17:21:25 -0300 Subject: [PATCH 32/33] Controller templates should have the `.rb` extension, not `.erb`. --- lib/generators/devise/controllers_generator.rb | 2 +- ...confirmations_controller.erb => confirmations_controller.rb} | 0 ...allbacks_controller.erb => omniauth_callbacks_controller.rb} | 0 .../{passwords_controller.erb => passwords_controller.rb} | 0 ...registrations_controller.erb => registrations_controller.rb} | 0 .../{sessions_controller.erb => sessions_controller.rb} | 0 .../{unlocks_controller.erb => unlocks_controller.rb} | 0 7 files changed, 1 insertion(+), 1 deletion(-) rename lib/generators/templates/controllers/{confirmations_controller.erb => confirmations_controller.rb} (100%) rename lib/generators/templates/controllers/{omniauth_callbacks_controller.erb => omniauth_callbacks_controller.rb} (100%) rename lib/generators/templates/controllers/{passwords_controller.erb => passwords_controller.rb} (100%) rename lib/generators/templates/controllers/{registrations_controller.erb => registrations_controller.rb} (100%) rename lib/generators/templates/controllers/{sessions_controller.erb => sessions_controller.rb} (100%) rename lib/generators/templates/controllers/{unlocks_controller.erb => unlocks_controller.rb} (100%) diff --git a/lib/generators/devise/controllers_generator.rb b/lib/generators/devise/controllers_generator.rb index 721c5e49..921c21c7 100644 --- a/lib/generators/devise/controllers_generator.rb +++ b/lib/generators/devise/controllers_generator.rb @@ -31,7 +31,7 @@ module Devise @scope_prefix = scope.blank? ? '' : (scope.camelize + '::') controllers = options[:controllers] || CONTROLLERS controllers.each do |name| - template "#{name}_controller.erb", + template "#{name}_controller.rb", "app/controllers/#{scope}/#{name}_controller.rb" end end diff --git a/lib/generators/templates/controllers/confirmations_controller.erb b/lib/generators/templates/controllers/confirmations_controller.rb similarity index 100% rename from lib/generators/templates/controllers/confirmations_controller.erb rename to lib/generators/templates/controllers/confirmations_controller.rb diff --git a/lib/generators/templates/controllers/omniauth_callbacks_controller.erb b/lib/generators/templates/controllers/omniauth_callbacks_controller.rb similarity index 100% rename from lib/generators/templates/controllers/omniauth_callbacks_controller.erb rename to lib/generators/templates/controllers/omniauth_callbacks_controller.rb diff --git a/lib/generators/templates/controllers/passwords_controller.erb b/lib/generators/templates/controllers/passwords_controller.rb similarity index 100% rename from lib/generators/templates/controllers/passwords_controller.erb rename to lib/generators/templates/controllers/passwords_controller.rb diff --git a/lib/generators/templates/controllers/registrations_controller.erb b/lib/generators/templates/controllers/registrations_controller.rb similarity index 100% rename from lib/generators/templates/controllers/registrations_controller.erb rename to lib/generators/templates/controllers/registrations_controller.rb diff --git a/lib/generators/templates/controllers/sessions_controller.erb b/lib/generators/templates/controllers/sessions_controller.rb similarity index 100% rename from lib/generators/templates/controllers/sessions_controller.erb rename to lib/generators/templates/controllers/sessions_controller.rb diff --git a/lib/generators/templates/controllers/unlocks_controller.erb b/lib/generators/templates/controllers/unlocks_controller.rb similarity index 100% rename from lib/generators/templates/controllers/unlocks_controller.erb rename to lib/generators/templates/controllers/unlocks_controller.rb From 952c162bee8dbf7fb8ef0522a5142d8567bb4f1b Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Thu, 18 Sep 2014 17:25:13 -0300 Subject: [PATCH 33/33] Update CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0c53e8f..7b104052 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ * The Simple Form templates follow the same change from 3.3.0 by using `Log in` and adding a hint about the minimum password length when `validatable` is enabled (by @aried3r) * Remove reloading of routes when eager loading is enabled. This change was added during Rails 3 and it doesn't seem to be relevant to currently supported Rails versions (by @fgro) - + * Controller generator added as `devise:controllers SCOPE`. You can use the `-c` flag + to pick which controllers (`unlocks`, `confirmations`, etc) you want to generate. (by @Chun-Yang) * bug fix * Fixed a regression where the devise generator would fail with a `ConnectionNotEstablished` exception when executed inside a mountable engine