Class: SimpleLogin::Generators::SimpleLoginGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/rails/generators/simple_login/simple_login_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_gemsObject



42
43
44
# File 'lib/rails/generators/simple_login/simple_login_generator.rb', line 42

def add_gems
  gem("bcrypt-ruby")
end

#create_userObject



37
38
39
40
# File 'lib/rails/generators/simple_login/simple_login_generator.rb', line 37

def create_user
  generate("model", "users email:string password_digest:string auth_token:string password_reset_token:string password_reset_sent_at:datetime")
  rake("db:migrate")
end

#generate_userObject



7
8
9
10
11
12
13
# File 'lib/rails/generators/simple_login/simple_login_generator.rb', line 7

def generate_user
  # Copy the controllers for user, sessions and password_reset
  directory "controllers", "app/controllers/"
  directory "mailers", "app/mailers/"
  directory "models", "app/models/"
  directory "views", "app/views/"
end

#insert_general_methodsObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rails/generators/simple_login/simple_login_generator.rb', line 15

def insert_general_methods
  inject_into_file "app/controllers/application_controller.rb", after: "protect_from_forgery"  do
    a = "\n\n  private\n\n  def current_user\n"
    b = "    @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]\n"
    c = "  end\n"
    d = "\n  helper_method :current_user\n\n"
    e = "  def authorize\n"
    f = "    redirect_to login_url, alert: 'Not authorized. Please login.' if current_user.nil?\n"
    g = "  end\n"
    a+b+c+d+e+f+g
  end
end

#insert_routesObject



28
29
30
31
32
33
34
35
# File 'lib/rails/generators/simple_login/simple_login_generator.rb', line 28

def insert_routes
  route("match 'signup', to: 'users#new', as: 'signup'")
  route("match 'login', to: 'sessions#new', as: 'login'")
  route("match 'logout', to: 'sessions#destroy', as: 'logout'")
  route("resources :users")
  route("resources :sessions")
  route("resources :password_resets")
end