Class: AuthenticatedGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/authenticated/authenticated_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ AuthenticatedGenerator

Returns a new instance of AuthenticatedGenerator.



22
23
24
25
26
27
# File 'lib/generators/authenticated/authenticated_generator.rb', line 22

def initialize(*args, &block)
  super
  controller_base_name
  model_controller_base_name
  load_or_initialize_site_keys()
end

Class Method Details

.source_rootObject



18
19
20
# File 'lib/generators/authenticated/authenticated_generator.rb', line 18

def self.source_root
  @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
end

Instance Method Details

#create_controller_filesObject



53
54
55
56
# File 'lib/generators/authenticated/authenticated_generator.rb', line 53

def create_controller_files
  template 'controller.rb', File.join('app/controllers', controller_class_path, "#{ controller_file_name }_controller.rb")
  template 'model_controller.rb', File.join('app/controllers', model_controller_class_path, "#{ model_controller_file_name }_controller.rb")
end

#create_lib_filesObject



58
59
60
61
# File 'lib/generators/authenticated/authenticated_generator.rb', line 58

def create_lib_files
  template 'authenticated_system.rb', File.join('lib', 'authenticated_system.rb')
  template 'authenticated_test_helper.rb', File.join('lib', 'authenticated_test_helper.rb')
end

#create_migrationObject



120
121
122
123
124
# File 'lib/generators/authenticated/authenticated_generator.rb', line 120

def create_migration
  unless options.skip_migration?
    migration_template 'migration.rb', "db/migrate/create_#{ migration_file_name }.rb"
  end
end

#create_model_filesObject

def check_class_naming_collisions

  # Check for class naming collisions.
  class_collisions controller_class_path, "#{ controller_class_name }Controller",
                                          "#{ controller_class_name }Helper"                    # Sessions Controller

  class_collisions model_controller_class_path, "#{ model_controller_class_name }Controller",
                                                "#{ model_controller_class_name }Helper"        # Model Controller

  class_collisions class_path, "#{ class_name }",
                               "#{ class_name }Mailer",
                               "#{ class_name }MailerTest",
                               "#{ class_name }Observer"

  class_collisions [], 'AuthenticatedSystem', 'AuthenticatedTestHelper'
end


45
46
47
48
49
50
51
# File 'lib/generators/authenticated/authenticated_generator.rb', line 45

def create_model_files
  template 'model.rb', File.join('app/models', class_path, "#{ file_name }.rb")
  if options.include_activation?
    template "mailer.rb", File.join('app/mailers', class_path, "#{ file_name }_mailer.rb")
    template "observer.rb", File.join('app/models', class_path, "#{ file_name }_observer.rb")
  end
end

#create_notesObject

Post-install notes



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/generators/authenticated/authenticated_generator.rb', line 140

def create_notes
  case behavior
  when :invoke
    puts "Ready to generate."
    puts ("-" * 70)
    puts "Once finished, don't forget to:"
    puts
    puts "- Install the dynamic_form plugin(error_messages_for was removed from Rails and is now available as a plugin):"
    puts "    Install it with rails plugin install git://github.com/rails/dynamic_form.git"
    if options.include_activation?
      puts "- Add an observer to config/environment.rb"
      puts "    config.active_record.observers = :#{ file_name }_observer"
    end
    if options.aasm?
      puts "- Install the acts_as_state_machine gem:"
      puts "    sudo gem sources -a http://gems.github.com (If you haven't already)"
      puts "    sudo gem install rubyist-aasm"
    elsif options.stateful?
      puts "- Install the acts_as_state_machine plugin:"
      puts "    svn export http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk vendor/plugins/acts_as_state_machine"
    end
    puts "- Add routes to these resources. In config/routes.rb, insert routes like:"
    puts %(    match 'login' => '#{ controller_file_name }#new', :as => :login)
    puts %(    match 'logout' => '#{ controller_file_name }#destroy', :as => :logout)
    puts %(    match 'signup' => '#{ model_controller_file_name }#new', :as => :signup)
    if options.include_activation?
      puts %(    match 'activate/:activation_code' => '#{ model_controller_file_name }#activate', :as => :activate, :activation_code => nil)
    end
    if options.stateful?
      puts  "  and modify the resources :#{ model_controller_file_name } line to include these actions:"
      puts  "    resources :#{ model_controller_file_name } do"
      puts  "      member do"
      puts  "        put :suspend"
      puts  "        put :unsuspend"
      puts  "        delete :purge"
      puts  "      end"
      puts  "    end"
    end
    puts
    puts ("-" * 70)
    puts
    if $rest_auth_site_key_from_generator.blank?
      puts "You've set a nil site key. This preserves existing users' passwords,"
      puts "but allows dictionary attacks in the unlikely event your database is"
      puts "compromised and your site code is not.  See the README for more."
    elsif $rest_auth_keys_are_new
      puts "We've create a new site key in #{ site_keys_file }.  If you have existing"
      puts "user accounts their passwords will no longer work (see README). As always,"
      puts "keep this file safe but don't post it in public."
    else
      puts "We've reused the existing site key in #{ site_keys_file }.  As always,"
      puts "keep this file safe but don't post it in public."
    end
    puts
    puts ("-" * 70)
  when :revoke
    puts
    puts ("-" * 70)
    puts
    puts "Thanks for using restful_authentication"
    puts
    puts "Don't forget to comment out the observer line in environment.rb"
    puts "  (This was optional so it may not even be there)"
    puts "  # config.active_record.observers = :#{ file_name }_observer"
    puts
    puts ("-" * 70)
    puts
  else
    puts "Didn't understand the action '#{ action }' -- you might have missed the 'after running me' instructions."
  end
end

#create_routesObject



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/generators/authenticated/authenticated_generator.rb', line 126

def create_routes
  unless options.skip_routes?
    # Note that this fails for nested classes -- you're on your own with setting up the routes.
    route "match '/activate/:activation_code' => '#{ model_controller_plural_name }#activate', :as => :activate, :activation_code => nil"
    route "match 'logout' => '#{ controller_controller_name }#destroy', :as => :logout"
    route "match 'login' => '#{ controller_controller_name }#new', :as => :login"
    route "match 'register' => '#{ model_controller_plural_name }#create', :as => :register"
    route "match 'signup' => '#{ model_controller_plural_name }#new', :as => :signup"
    route "resource #{ controller_singular_name.to_sym.inspect }, :only => [:new, :create, :destroy]"
    route "resources #{ model_controller_plural_name.to_sym.inspect }"
  end
end

#create_site_keyObject



63
64
65
# File 'lib/generators/authenticated/authenticated_generator.rb', line 63

def create_site_key
  template 'site_keys.rb', site_keys_file
end

#create_test_filesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/authenticated/authenticated_generator.rb', line 67

def create_test_files
  if has_rspec?
    # RSpec Specs
    template 'spec/controllers/users_controller_spec.rb', File.join('spec/controllers', model_controller_class_path, "#{ model_controller_file_name }_controller_spec.rb")
    template 'spec/controllers/sessions_controller_spec.rb', File.join('spec/controllers', controller_class_path, "#{ controller_file_name }_controller_spec.rb")
    template 'spec/controllers/access_control_spec.rb', File.join('spec/controllers', controller_class_path, "access_control_spec.rb") 
    template 'spec/controllers/authenticated_system_spec.rb', File.join('spec/controllers', controller_class_path, "authenticated_system_spec.rb")
    template 'spec/helpers/users_helper_spec.rb', File.join('spec/helpers', model_controller_class_path, "#{ table_name }_helper_spec.rb")
    template 'spec/models/user_spec.rb', File.join('spec/models' , class_path, "#{ file_name }_spec.rb")
    #if fixtures_required?
      template 'spec/fixtures/users.yml', File.join('spec/fixtures', class_path, "#{ table_name }.yml")
    #end
    # Cucumber features
    template 'features/step_definitions/ra_navigation_steps.rb', File.join('features/step_definitions/ra_navigation_steps.rb')
    template 'features/step_definitions/ra_response_steps.rb', File.join('features/step_definitions/ra_response_steps.rb')
    template 'features/step_definitions/ra_resource_steps.rb', File.join('features/step_definitions/ra_resource_steps.rb')
    template 'features/step_definitions/user_steps.rb', File.join('features/step_definitions/', "#{ file_name }_steps.rb")
    template 'features/accounts.feature', File.join('features', 'accounts.feature')
    template 'features/sessions.feature', File.join('features', 'sessions.feature')
    template 'features/step_definitions/rest_auth_features_helper.rb', File.join('features', 'step_definitions', 'rest_auth_features_helper.rb')
    template 'features/step_definitions/ra_env.rb', File.join('features', 'step_definitions', 'ra_env.rb')
  else
    template 'test/functional_test.rb', File.join('test/functional', controller_class_path, "#{ controller_file_name }_controller_test.rb")
    template 'test/model_functional_test.rb', File.join('test/functional', model_controller_class_path, "#{ model_controller_file_name }_controller_test.rb")
    template 'test/unit_test.rb', File.join('test/unit', class_path, "#{ file_name }_test.rb") 
    if options.include_activation?
      template 'test/mailer_test.rb', File.join('test/functional', class_path, "#{ file_name }_mailer_test.rb")
    end
    #if fixtures_required?
      template 'spec/fixtures/users.yml', File.join('test/fixtures', class_path, "#{ table_name }.yml")
    #end
  end
end

#create_view_filesObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/generators/authenticated/authenticated_generator.rb', line 106

def create_view_files
  # Controller templates
  template 'login.html.erb',  File.join('app/views', controller_class_path, controller_file_name, "new.html.erb")
  template 'signup.html.erb', File.join('app/views', model_controller_class_path, model_controller_file_name, "new.html.erb")
  template '_model_partial.html.erb', File.join('app/views', model_controller_class_path, model_controller_file_name, "_#{ file_name }_bar.html.erb")

  if options.include_activation?
    # Mailer templates
    %w( activation signup_notification ).each do |action|
      template "#{ action }.erb", File.join('app/views', "#{ file_name }_mailer", "#{ action }.html.erb")
    end
  end
end

#crete_helper_filesObject



101
102
103
104
# File 'lib/generators/authenticated/authenticated_generator.rb', line 101

def crete_helper_files
  template 'helper.rb', File.join('app/helpers', controller_class_path, "#{ controller_file_name }_helper.rb")
  template 'model_helper.rb', File.join('app/helpers', model_controller_class_path, "#{ model_controller_file_name }_helper.rb")
end


212
213
214
215
216
# File 'lib/generators/authenticated/authenticated_generator.rb', line 212

def print_generator_attribute_names
  if options.dump_generator_attrs?
    dump_generator_attribute_names
  end
end