module I0n module Generators class AuthenticationGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) def gemfile gem("bcrypt-ruby", :require => 'bcrypt') end def setup_application application do ' # Configure sensitive parameters which will be filtered from the log file. config.filter_parameters += [:password, :password_confirmation] config.autoload_paths += %W(#{Rails.root}/lib) ' end end def create_authentication_lib_and_include_in_application_helper copy_file "lib/authentication.rb", "#{Rails.root}/lib/authentication.rb" inject_into_class("app/controllers/application_controller.rb", ApplicationController, "include Authentication\n") end def create_routes route("match 'register' => 'users#new', :as => :register") route("resources :users") route("resources :user_sessions, :only => [:new, :create, :destroy]") route("match 'login' => 'user_sessions#new', :as => :login") route("match 'logout' => 'user_sessions#destroy', :as => :logout") end def create_models generate("model", "user email:string password_hash:string password_salt:string") inject_into_class("app/models/user.rb", User, IO.read("#{AuthenticationGenerator.source_root}/app/models/_user.rb")) end def create_controllers generate("controller", "user_sessions") generate("controller", "users") inject_into_class("app/controllers/users_controller.rb", UsersController, IO.read("#{AuthenticationGenerator.source_root}/app/controllers/_users_controller.rb")) inject_into_class("app/controllers/user_sessions_controller.rb", UserSessionsController, IO.read("#{AuthenticationGenerator.source_root}/app/controllers/_user_sessions_controller.rb")) end def create_views directory "app/views/users", "#{Rails.root}/app/views/users" directory "app/views/user_sessions", "#{Rails.root}/app/views/user_sessions" end def create_rake_tasks copy_file "lib/tasks/create_default_user.rake", "#{Rails.root}/lib/tasks/create_default_user.rake" end end end end