Class: ScaffoldPlus::Generators::SorceryGenerator

Inherits:
ActiveRecord::Generators::Base
  • Object
show all
Defined in:
lib/generators/scaffold_plus/sorcery/sorcery_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_controllersObject



45
46
47
# File 'lib/generators/scaffold_plus/sorcery/sorcery_generator.rb', line 45

def add_controllers
  template "users_controller.rb", "app/controllers/#{table_name}_controller.rb"
end

#add_initializerObject



88
89
90
# File 'lib/generators/scaffold_plus/sorcery/sorcery_generator.rb', line 88

def add_initializer
  template "initializer.rb", "config/initializers/sorcery.rb"
end

#add_localesObject



92
93
94
95
96
# File 'lib/generators/scaffold_plus/sorcery/sorcery_generator.rb', line 92

def add_locales
  %w[en de].each do |locale|
    template "sorcery.#{locale}.yml", "config/locales/sorcery.#{locale}.yml"
  end
end

#add_mailerObject



39
40
41
42
43
# File 'lib/generators/scaffold_plus/sorcery/sorcery_generator.rb', line 39

def add_mailer
  return unless options.reset_password?
  template "user_mailer.rb", "app/mailers/#{mailer_name}.rb"
  template "reset_password_email.html.erb", "app/views/#{mailer_name}/reset_password_email.html.erb"
end

#add_migrationsObject



31
32
33
# File 'lib/generators/scaffold_plus/sorcery/sorcery_generator.rb', line 31

def add_migrations
  migration_template "user_migration.rb", "db/migrate/create_#{table_name}.rb"
end

#add_modelsObject



35
36
37
# File 'lib/generators/scaffold_plus/sorcery/sorcery_generator.rb', line 35

def add_models
  template "user_model.rb", "app/models/#{name}.rb"
end

#add_routesObject



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
# File 'lib/generators/scaffold_plus/sorcery/sorcery_generator.rb', line 57

def add_routes
  lines = [
    "resources :#{table_name} do",
    "    collection do",
    "      get   'log_in'",
    "      post  'access'",
    "      get   'log_out'"
  ]
  lines << [
    "      get   'password'",
    "      post  'reset'"
  ] if options.reset_password?
  lines << [
    "    end"
  ]
  lines << [
    "    member do",
    "      get   'change'",
    "      patch 'refresh'",
    "      put   'refresh'",
    "    end"
  ] if options.reset_password?
  lines << [
    "  end",
    "  get '/login'  => '#{table_name}#log_in',  as: :login,  format: false",
    "  get '/logout' => '#{table_name}#log_out', as: :logout, format: false",
    ""
  ]
  route lines.join("\n")
end

#add_viewsObject



49
50
51
52
53
54
55
# File 'lib/generators/scaffold_plus/sorcery/sorcery_generator.rb', line 49

def add_views
  views  = %w[edit _form index log_in new show]
  views += %w[password change] if options.reset_password?
  views.each do |view|
    template "#{view}.html.erb", "app/views/#{table_name}/#{view}.html.erb"
  end
end

#update_application_controllerObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/generators/scaffold_plus/sorcery/sorcery_generator.rb', line 98

def update_application_controller
  file = "app/controllers/application_controller.rb"
  inject_into_class file, "ApplicationController", "  before_action :require_login\n\n"
  inject_into_file file, "\n\n  private", after: /protect_from_forgery.*$/
  lines = [
    "",
    "  def not_authenticated",
    "    redirect_to login_path, alert: t('sorcery.required')",
    "  end",
    "",
    "  def current_sysadm?",
    "    logged_in? and current_#{name}.sysadm",
    "  end",
    "  helper_method :current_sysadm?",
    ""
  ]
  inject_into_file file, lines.join("\n"), before: /^end$/
end