From 888272769b6d368e7bcbaeead165df24d77a60bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 3 Nov 2009 10:11:36 -0200 Subject: [PATCH] Created a generator that copies an initializer with configuration values. --- CHANGELOG.rdoc | 2 + README.rdoc | 51 +++++-------------- generators/devise_install/USAGE | 3 ++ .../devise_install_generator.rb | 9 ++++ generators/devise_install/templates/devise.rb | 33 ++++++++++++ generators/devise_views/USAGE | 2 +- lib/devise/models.rb | 24 ++------- 7 files changed, 64 insertions(+), 60 deletions(-) create mode 100644 generators/devise_install/USAGE create mode 100644 generators/devise_install/devise_install_generator.rb create mode 100644 generators/devise_install/templates/devise.rb diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index b0a0a84965..7b0ec305eb 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -5,6 +5,8 @@ * enhancement * [#16] Allow devise to be more agnostic. Do not require ActiveRecord to be loaded. + * Allow Warden::Manager to be configured through Devise + * Created a generator which creates an initializer == 0.3.0 diff --git a/README.rdoc b/README.rdoc index 61487aed9e..0d0762e7e7 100644 --- a/README.rdoc +++ b/README.rdoc @@ -91,29 +91,11 @@ You could also include the other devise modules as below: Note that validations aren't added by default, so you're able to customize it. In order to have automatic validations working just include :validatable. -== Configuration values +== Model configuration -In addition to :except, you can provide some options to devise call: +In addition to :except, you can provide :pepper, :stretches, :confirm_within and :remember_for as options to devise method. -* pepper: setup a pepper to generate de encrypted password. By default no pepper is used: - - devise :all, :pepper => 'my_pepper' - -* stretches: configure how many times you want the password is reencrypted. - - devise :all, :stretches => 20 - -* confirm_within: the time the user can access the site before being blocked because his account was not confirmed - - devise :all, :confirm_within => 1.week - -* remember_for: the time to store the remember me cookie in the user - - devise :all, :remember_for => 2.weeks - -All those values can also be set in a global way by setting them in Devise::Models: - - Devise::Models.confirm_within = 1.week +All those options are described in "config/initializers/devise.rb", which is generated when you invoke `ruby script/generate devise_install` in your application root. == Routes @@ -140,23 +122,12 @@ This is going to look inside you User model and create the needed routes: POST /users/confirmation(.:format) {:controller=>"confirmations", :action=>"create"} You can run the routes rake task to verify what routes are being created by devise. -There are also some options available for configuring your routes: - -* :class_name => setup a different class to be looked up by devise, if it cannot be correctly find by the route name. - - map.devise_for :users, :class_name => 'Account' - -* :as => allows you to setup path name that will be used, as rails routes does. The following route configuration would setup your route as /accounts instead of /users: - map.devise_for :users, :as => 'accounts' +There are also some options available for configuring your routes, as :class_name (to set the class for that route), :as and :path_names, where the last two have the same meaning as in routes. The available :path_names are: -* :singular => setup the name used to create named routes. By default, for a :users key, it is going to be the singularized version, :user. To configure a named route like account_session_path instead of user_session_path just do: + map.devise_for :users, :as => "usuarios", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' } - map.devise_for :users, :singular => :account - -* :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :password and :confirmation. - - map.devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' } +Be sure to check devise_for documentation for detailed description. == Controller filters @@ -212,13 +183,17 @@ Devise let's you setup as many roles as you want, so let's say you already have Devise comes with some generators to help you start: - script/generate devise Model + ruby script/generate devise_install + +This will generate an initializer, with a description of all configuration values. You can also generate models through: + + ruby script/generate devise Model -Will generate a model, configured with all devise modules, and add attr_accessible for default fields, so you can setup more accessible attributes later. The generator will also create the migration and configure your route for devise. +A model configured with all devise modules and attr_accessible for default fields will be created. The generator will also create the migration and configure your routes for devise. You can also copy devise views to your application, being able to modify them based on your needs. To do it so, run the following command: - script/generate devise_views + ruby script/generate devise_views This is gonna copy all session, password, confirmation and mailer views to your app/views folder. diff --git a/generators/devise_install/USAGE b/generators/devise_install/USAGE new file mode 100644 index 0000000000..6085390409 --- /dev/null +++ b/generators/devise_install/USAGE @@ -0,0 +1,3 @@ +To copy a Devise initializer to your Rails App, with some configuration values, just do: + + script/generate devise_install diff --git a/generators/devise_install/devise_install_generator.rb b/generators/devise_install/devise_install_generator.rb new file mode 100644 index 0000000000..0f5c13cafe --- /dev/null +++ b/generators/devise_install/devise_install_generator.rb @@ -0,0 +1,9 @@ +class DeviseInstallGenerator < Rails::Generator::Base + + def manifest + record do |m| + m.file "devise.rb", "config/initializers/devise.rb" + end + end + +end diff --git a/generators/devise_install/templates/devise.rb b/generators/devise_install/templates/devise.rb new file mode 100644 index 0000000000..e910d4a5ee --- /dev/null +++ b/generators/devise_install/templates/devise.rb @@ -0,0 +1,33 @@ +# Use this hook to configure devise mailer, warden hooks and so forth. The first +# four configuration values can also be set straight in your models. +Devise.setup do |config| + # Invoke `rake secret` and use the printed value to setup a pepper to generate + # the encrypted password. By default no pepper is used. + # config.pepper = "rake secret output" + + # Configure how many times you want the password is reencrypted. Default is 10. + # config.stretches = 10 + + # The time you want give to your user to confirm his account. During this time + # he will be able to access your application without confirming. Default is nil. + # config.confirm_within = 2.days + + # The time the user will be remembered without asking for credentials again. + # config.remember_for = 2.weeks + + # Configure the e-mail address which will be shown in DeviseMailer. + # config.mail_sender = "foo.bar@yourapp.com" + + # If you want to use other strategies, that are not (yet) supported by Devise, + # you can configure them inside the config.warden block. The example below + # allows you to setup OAuth, using http://github.com/roman/warden_oauth + # + # config.manager do |manager| + # manager.oauth(:twitter) do |twitter| + # twitter.consumer_secret = + # twitter.consumer_key = + # twitter.options :site => 'http://twitter.com' + # end + # manager.default_strategies.unshift :twitter_oauth + # end +end diff --git a/generators/devise_views/USAGE b/generators/devise_views/USAGE index 7fcc503bb4..c90f2e7cb2 100644 --- a/generators/devise_views/USAGE +++ b/generators/devise_views/USAGE @@ -1,3 +1,3 @@ -To copy all session, password and confirmation views from devise to your app just run the following command: +To copy all session, password, confirmation and mailer views from devise to your app just run the following command: script/generate devise_views diff --git a/lib/devise/models.rb b/lib/devise/models.rb index d94d282577..c2979df19d 100644 --- a/lib/devise/models.rb +++ b/lib/devise/models.rb @@ -51,27 +51,9 @@ def #{accessor}=(value) # # devise :all, :except => :rememberable # - # * pepper: setup a pepper to generate de encrypted password. By default no - # pepper is used: - # - # devise :all, :pepper => 'my_pepper' - # - # * stretches: configure how many times you want the password is reencrypted. - # - # devise :all, :stretches => 20 - # - # * confirm_within: the time you want your user to confirm it's account. During - # this time he will be able to access your application without confirming. - # - # devise :all, :confirm_within => 7.days - # - # * remember_for: the time the user will be remembered without asking for - # credentials again. - # - # devise :all, :remember_for => 2.weeks - # - # You can refer to Authenticable, Confirmable and Rememberable for more - # information about writing your own method to setup each model apart. + # You can also give the following configuration values in a hash: :pepper, + # :stretches, :confirm_within and :remember_for. Please check your Devise + # initialiazer for a complete description on those values. # # Examples: #