Class: Katapult::Generators::ClearanceGenerator

Inherits:
Katapult::Generator
  • Object
show all
Defined in:
lib/generators/katapult/clearance/clearance_generator.rb

Constant Summary collapse

'app/views/layouts/_menu_bar.html.haml'

Instance Attribute Summary

Attributes inherited from Katapult::Generator

#element

Instance Method Summary collapse

Methods inherited from Katapult::Generator

#initialize

Methods included from Katapult::GeneratorGoodies

#file_contains?, #yarn

Constructor Details

This class inherits a constructor from Katapult::Generator

Instance Method Details

#add_authentication_pathsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 109

def add_authentication_paths
  inject_into_file 'features/support/paths.rb', "\n\n    # Authentication\n    when 'the sign-in form'\nsign_in_path\n    when 'the reset password page'\nnew_password_path\n    when 'the new password page for the user above'\nedit_user_password_path(User.last!)\n\n  CONTENT\nend\n", after: 'case page_name'

#add_current_user_to_layoutObject



78
79
80
81
82
83
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 78

def add_current_user_to_layout
  template 'app/views/layouts/_current_user.html.haml'
  inject_into_file MENU_BAR, "= render 'layouts/current_user' if signed_in?\n  CONTENT\nend\n", after: /^\s+#navbar.*\n/

#add_sign_in_background_to_all_featuresObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 89

def 
  Dir['features/*.feature'].each do |file|
    inject_into_file file, "\n\n  Background:\n    Given there is a user\nAnd I sign in as the user above\n    CONTENT\n  end\nend\n", after: /^Feature: .*$/

#add_user_factoryObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 124

def add_user_factory
  factories_file = 'spec/factories/factories.rb'

  # Remove empty factory
  gsub_file factories_file, "\n  factory :user\n", ''

  # In a second `transform` run, this might already be present
  unless file_contains? factories_file, 'factory :user'
    inject_into_file factories_file, "  factory :user do\n    sequence(:email) { |i| \"user-\#{ i }@example.com\" }\n    password 'password'\n  end\n\n    CONTENT\n  end\nend\n", before: /end\n\z/

#create_authentication_featureObject



101
102
103
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 101

def create_authentication_feature
  template 'features/authentication.feature'
end

#create_authentication_stepsObject



105
106
107
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 105

def create_authentication_steps
  template 'features/step_definitions/authentication_steps.rb'
end

#create_clearance_viewsObject



41
42
43
44
45
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 41

def create_clearance_views
  directory 'app/views/clearance_mailer'
  directory 'app/views/passwords'
  directory 'app/views/sessions'
end

#create_initializerObject



55
56
57
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 55

def create_initializer
  template 'config/initializers/clearance.rb', force: true
end

#create_routesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 63

def create_routes
  route "resources :users do\n    resource :password, controller: 'passwords',\nonly: %i[edit update]\n  end\n\n  # Clearance\n  get '/login', to: 'clearance/sessions#new', as: 'sign_in'\n  resource :session, controller: 'clearance/sessions', only: [:create]\n  resources :passwords, controller: 'passwords', only: [:create, :new]\n  delete '/logout', to: 'clearance/sessions#destroy', as: 'sign_out'\n  ROUTES\nend\n"

#create_translationsObject



59
60
61
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 59

def create_translations
  template 'config/locales/clearance.en.yml'
end

#hide_navigation_unless_signed_inObject



85
86
87
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 85

def hide_navigation_unless_signed_in
  gsub_file MENU_BAR, /(render.*navigation')/, '\1 if signed_in?'
end

#install_backdoorObject



47
48
49
50
51
52
53
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 47

def install_backdoor
  # This creepy indentation leads to correct formatting in the file
  application "# Enable quick-signin in tests: `visit homepage(as: User.last!)`\n  config.middleware.use Clearance::BackDoor\n  CONTENT\nend\n", env: 'test'

#install_clearanceObject



20
21
22
23
24
25
26
27
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 20

def install_clearance
  insert_into_file 'Gemfile', "gem 'clearance'\n\n  GEM\n  run 'bundle install --quiet'\n  generate 'clearance:install'\nend\n", before: "gem 'katapult'"

#migrateObject



16
17
18
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 16

def migrate
  rake 'db:migrate' # Clearance must see the users table in the db
end

#overwrite_clearance_controllersObject



37
38
39
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 37

def overwrite_clearance_controllers
  template 'app/controllers/passwords_controller.rb'
end

#require_loginObject



29
30
31
32
33
34
35
# File 'lib/generators/katapult/clearance/clearance_generator.rb', line 29

def 
  file = 'app/controllers/application_controller.rb'
  insert_into_file file, "\n  before_action :require_login\n  CONTENT\nend\n", after: "Clearance::Controller\n"