Class: BootswatchRails::Generators::SorceryGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_controllersObject



67
68
69
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 67

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

#add_gravatarObject



56
57
58
59
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 56

def add_gravatar
  return unless options.gravatar?
  template "gravatar_helper.rb", "app/helpers/gravatar_helper.rb"
end

#add_initializerObject



115
116
117
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 115

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

#add_localesObject



119
120
121
122
123
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 119

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

#add_mailerObject



61
62
63
64
65
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 61

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



43
44
45
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 43

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

#add_modelsObject



47
48
49
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 47

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

#add_routesObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 79

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"
  ]
  lines << [
    "  root '#{table_name}#log_in'"
  ] if options.root_route_login?
  lines << [
    "  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_uploaderObject



51
52
53
54
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 51

def add_uploader
  return unless options.picture?
  template "picture_uploader.rb", "app/uploaders/picture_uploader.rb"
end

#add_viewsObject



71
72
73
74
75
76
77
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 71

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/generators/bootswatch_rails/sorcery/sorcery_generator.rb', line 125

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