Class: S6xAuthScaffoldGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- S6xAuthScaffoldGenerator
- Extended by:
- ActiveRecord::Generators::Migration
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/s6x_auth_scaffold_generator.rb
Instance Method Summary collapse
- #add_routes ⇒ Object
- #create_sessions_controller ⇒ Object
- #create_user_model ⇒ Object
- #create_users_migration ⇒ Object
- #edit_application_controller ⇒ Object
- #parse_options ⇒ Object
Instance Method Details
#add_routes ⇒ Object
48 49 50 51 52 |
# File 'lib/s6x_auth_scaffold_generator.rb', line 48 def add_routes route('match "login" => "sessions#login"') route('match "logout" => "sessions#logout"') route(%Q'root :to => "#{[:root_to]}"') end |
#create_sessions_controller ⇒ Object
41 42 43 44 45 46 |
# File 'lib/s6x_auth_scaffold_generator.rb', line 41 def create_sessions_controller template "app/controllers/sessions_controller.rb" template "test/functional/sessions_controller_test.rb" copy_file "app/views/sessions/login.html.erb" template "app/helpers/sessions_helper.rb" end |
#create_user_model ⇒ Object
35 36 37 38 39 |
# File 'lib/s6x_auth_scaffold_generator.rb', line 35 def create_user_model template "app/models/user.rb", "app/models/#{@user_model_file_name}.rb" template "test/unit/user_test.rb", "test/unit/#{@user_model_file_name}_test.rb" template "test/fixtures/users.yml", "test/fixtures/#{@users_table_name}.yml" end |
#create_users_migration ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/s6x_auth_scaffold_generator.rb', line 25 def create_users_migration migration_template "db/migrate/create_users.rb", "db/migrate/create_#{@users_table_name}.rb" rescue Rails::Generators::Error => e if [:skip] puts e. else raise end end |
#edit_application_controller ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/s6x_auth_scaffold_generator.rb', line 54 def edit_application_controller method_name = "current_#{@user_instance_name}" dest_path = "app/controllers/application_controller.rb" if ApplicationController.public_method_defined?(method_name.to_sym) msg = "ApplicationController is already defined #{method_name} method: #{dest_path}" if [:skip] puts msg else raise Rails::Generators::Error, msg end else prepend_to_file(dest_path, "# coding: utf-8\n") inject_into_class(dest_path, ApplicationController, %Q' before_filter :login_required # ログイン中のユーザを返す。 def #{method_name} if session[:#{@user_instance_name}_id] @current_#{@user_instance_name} ||= #{@user_model_name}.find(session[:#{@user_instance_name}_id]) end return @current_#{@user_instance_name} end helper_method :current_#{@user_instance_name} # ログインしているかどうかを返す。 def logged_in? return #{method_name} ? true : false end helper_method :logged_in? # ログインが必要なコントローラでフィルタとして使う。 # # 使用例) # class SomeController < ActionController::Base # before_filter :login_required # skip_before_filter :login_required, only: :login # def login_required if !logged_in? redirect_to(login_path, alert: "ログインしてください。") return false end end '.force_encoding("ASCII-8BIT")) # NOTE: force_encoding("ASCII-8BIT")はThorのバグを回避するため。 end end |
#parse_options ⇒ Object
18 19 20 21 22 23 |
# File 'lib/s6x_auth_scaffold_generator.rb', line 18 def @user_model_name = file_name.camelize @user_model_file_name = file_name.underscore @user_instance_name = file_name.underscore @users_table_name = file_name.underscore.pluralize end |