Skip to content

Commit 3008994

Browse files
committed
Add support for generate scaffold password:digest
* adds password_digest attribute to the migration * adds has_secure_password to the model * adds password and password_confirmation password_fields to _form.html * omits password entirely from index.html and show.html * adds password and password_confirmation to the controller * adds unencrypted password and password_confirmation to the controller test * adds encrypted password_digest to the fixture
1 parent cd9f750 commit 3008994

File tree

11 files changed

+90
-14
lines changed

11 files changed

+90
-14
lines changed

activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ class <%= migration_class_name %> < ActiveRecord::Migration
22
def change
33
create_table :<%= table_name %> do |t|
44
<% attributes.each do |attribute| -%>
5+
<% if attribute.password_digest? -%>
6+
t.string :password_digest<%= attribute.inject_options %>
7+
<% else -%>
58
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
69
<% end -%>
10+
<% end -%>
711
<% if options[:timestamps] %>
812
t.timestamps
913
<% end -%>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<% module_namespacing do -%>
22
class <%= class_name %> < <%= parent_class_name.classify %>
3-
<% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
3+
<% attributes.select(&:reference?).each do |attribute| -%>
44
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
55
<% end -%>
6+
<% if attributes.any?(&:password_digest?) -%>
7+
has_secure_password
8+
<% end -%>
69
end
710
<% end -%>

railties/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* Add support for generate scaffold password:digest
4+
5+
* adds password_digest attribute to the migration
6+
* adds has_secure_password to the model
7+
* adds password and password_confirmation password_fields to _form.html
8+
* omits password from index.html and show.html
9+
* adds password and password_confirmation to the controller
10+
* adds unencrypted password and password_confirmation to the controller test
11+
* adds encrypted password_digest to the fixture
12+
13+
*Sam Ruby*
14+
315
* Rails now generates a `test/test_helper.rb` file with `fixtures :all` commented out by default,
416
since we don't want to force loading all fixtures for user when a single test is run. However,
517
fixtures are still going to be loaded automatically for test suites.

railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@
1313

1414
<% attributes.each do |attribute| -%>
1515
<div class="field">
16+
<% if attribute.password_digest? -%>
17+
<%%= f.label :password %><br />
18+
<%%= f.password_field :password %>
19+
</div>
20+
<div>
21+
<%%= f.label :password_confirmation %><br />
22+
<%%= f.password_field :password_confirmation %>
23+
<% else -%>
1624
<%%= f.label :<%= attribute.name %> %><br />
1725
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
26+
<% end -%>
1827
</div>
1928
<% end -%>
2029
<div class="actions">

railties/lib/rails/generators/erb/scaffold/templates/index.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<table>
44
<thead>
55
<tr>
6-
<% attributes.each do |attribute| -%>
6+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
77
<th><%= attribute.human_name %></th>
88
<% end -%>
99
<th></th>
@@ -15,7 +15,7 @@
1515
<tbody>
1616
<%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
1717
<tr>
18-
<% attributes.each do |attribute| -%>
18+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
1919
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
2020
<% end -%>
2121
<td><%%= link_to 'Show', <%= singular_table_name %> %></td>

railties/lib/rails/generators/erb/scaffold/templates/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<p id="notice"><%%= notice %></p>
22

3-
<% attributes.each do |attribute| -%>
3+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
44
<p>
55
<strong><%= attribute.human_name %>:</strong>
66
<%%= @<%= singular_table_name %>.<%= attribute.name %> %>

railties/lib/rails/generators/generated_attribute.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def has_uniq_index?
130130
@has_uniq_index
131131
end
132132

133+
def password_digest?
134+
name == 'password' && type == :digest
135+
end
136+
133137
def inject_options
134138
"".tap { |s| @attr_options.each { |k,v| s << ", #{k}: #{v.inspect}" } }
135139
end

railties/lib/rails/generators/named_base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def parse_attributes! #:nodoc:
163163
def attributes_names
164164
@attributes_names ||= attributes.each_with_object([]) do |a, names|
165165
names << a.column_name
166+
names << 'password_confirmation' if a.password_digest?
166167
names << "#{a.name}_type" if a.polymorphic?
167168
end
168169
end

railties/lib/rails/generators/test_unit/model/templates/fixtures.yml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
2-
32
<% unless attributes.empty? -%>
4-
one:
3+
<% %w(one two).each do |name| %>
4+
<%= name %>:
55
<% attributes.each do |attribute| -%>
6+
<%- if attribute.password_digest? -%>
7+
password_digest: <%%= BCrypt::Password.create('secret') %>
8+
<%- else -%>
69
<%= yaml_key_value(attribute.column_name, attribute.default) %>
7-
<%- if attribute.polymorphic? -%>
8-
<%= yaml_key_value("#{attribute.name}_type", attribute.human_name) %>
910
<%- end -%>
10-
<% end -%>
11-
12-
two:
13-
<% attributes.each do |attribute| -%>
14-
<%= yaml_key_value(attribute.column_name, attribute.default) %>
1511
<%- if attribute.polymorphic? -%>
1612
<%= yaml_key_value("#{attribute.name}_type", attribute.human_name) %>
1713
<%- end -%>
1814
<% end -%>
15+
<% end -%>
1916
<% else -%>
17+
2018
# This model initially had no columns defined. If you add columns to the
2119
# model remove the '{}' from the fixture names and add the columns immediately
2220
# below each fixture, per the syntax in the comments below

railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ def attributes_hash
2121
return if attributes_names.empty?
2222

2323
attributes_names.map do |name|
24-
"#{name}: @#{singular_table_name}.#{name}"
24+
if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?)
25+
"#{name}: 'secret'"
26+
else
27+
"#{name}: @#{singular_table_name}.#{name}"
28+
end
2529
end.sort.join(', ')
2630
end
2731
end

0 commit comments

Comments
 (0)