Class: Janus::Generators::ResourceGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/janus/resource_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_janus_routeObject



70
71
72
# File 'lib/generators/janus/resource_generator.rb', line 70

def add_janus_route
  route "janus :#{plural_name}, " + controllers.map { |ctrl| ":#{ctrl} => true" }.join(', ')
end

#create_controllers_and_viewsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/janus/resource_generator.rb', line 34

def create_controllers_and_views
  if strategies.include?('session')
    template 'sessions_controller.erb', "app/controllers/#{plural_name}/sessions_controller.rb"
    template 'sessions/new.html.erb',   "app/views/#{plural_name}/sessions/new.html.erb"
  end
  if strategies.include?('registration')
    template 'registrations_controller.erb', "app/controllers/#{plural_name}/registrations_controller.rb"
    template 'registrations/new.html.erb',   "app/views/#{plural_name}/registrations/new.html.erb"
    template 'registrations/edit.html.erb',  "app/views/#{plural_name}/registrations/edit.html.erb"
  end
  if strategies.include?('confirmation')
    template 'confirmations_controller.erb', "app/controllers/#{plural_name}/confirmations_controller.rb"
    template 'confirmations/new.html.erb',   "app/views/#{plural_name}/confirmations/new.html.erb"
  end
  if strategies.include?('password')
    template 'passwords_controller.erb', "app/controllers/#{plural_name}/passwords_controller.rb"
    template 'passwords/new.html.erb',   "app/views/#{plural_name}/passwords/new.html.erb"
    template 'passwords/edit.html.erb',  "app/views/#{plural_name}/passwords/edit.html.erb"
  end
end

#create_mailerObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/generators/janus/resource_generator.rb', line 55

def create_mailer
  return unless strategies.include?('registration') or strategies.include?('confirmation') or strategies.include?('password')
  template 'mailer.rb', "app/mailers/#{singular_name}_mailer.rb"

  if strategies.include?('confirmation')
    template 'mailer/confirmation_instructions.html.erb', "app/views/#{singular_name}_mailer/confirmation_instructions.html.erb"
    template 'mailer/confirmation_instructions.text.erb', "app/views/#{singular_name}_mailer/confirmation_instructions.text.erb"
  end

  if strategies.include?('password')
    template 'mailer/reset_password_instructions.html.erb', "app/views/#{singular_name}_mailer/reset_password_instructions.html.erb"
    template 'mailer/reset_password_instructions.text.erb', "app/views/#{singular_name}_mailer/reset_password_instructions.text.erb"
  end
end

#create_resourceObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/janus/resource_generator.rb', line 12

def create_resource
  attributes  = [singular_name]
  attributes += %w{email:string encrypted_password:string}
  attributes += %w{remember_token:string:uniq remember_created_at:datetime} if strategies.include?('remember')
  attributes += %w{confirmation_token:string:uniq confirmation_sent_at:datetime confirmed_at:datetime} if strategies.include?('confirmation')
  attributes += %w{reset_password_token:string:uniq reset_password_sent_at:datetime} if strategies.include?('password')
  attributes += %w{session_token:string:uniq} if strategies.include?('remote')
  attributes += %w{sign_in_count:integer last_sign_in_at:datetime last_sign_in_ip:string current_sign_in_at:datetime current_sign_in_ip:string} if strategies.include?('track')
  attributes += %w{authentication_token:string:uniq authentication_token_created_at:datetime} if strategies.include?('token')
  generate('model', attributes.join(' '))

  modules = [
    "  include Janus::Models::DatabaseAuthenticatable",
  ]
  modules << "  include Janus::Models::Rememberable" if strategies.include?('remember')
  modules << "  include Janus::Models::Confirmable" if strategies.include?('confirmation')
  modules << "  include Janus::Models::Trackable" if strategies.include?('track')
  modules << "  include Janus::Models::RemoteAuthenticatable" if strategies.include?('remote')
  modules << "  include Janus::Models::TokenAuthenticatable" if strategies.include?('token')
  inject_into_class "app/models/#{singular_name}.rb", class_name, modules.join("\n") + "\n"
end

#delivery_methodObject



74
75
76
# File 'lib/generators/janus/resource_generator.rb', line 74

def delivery_method
  Rails.version >= "4.2.0" ? "deliver_later" : "deliver"
end