Permalink
Browse files

Merge pull request #18448 from robertomiranda/has_secure_token_generator

Add has secure token to migration generator
  • Loading branch information...
guilleiguaran committed Jan 11, 2015
2 parents 0e316ea + 5cca205 commit 72139d8d310d896db78eaec98582c7a638135102
@@ -4,6 +4,8 @@ def change
<% attributes.each do |attribute| -%>
<% if attribute.password_digest? -%>
t.string :password_digest<%= attribute.inject_options %>
<% elsif attribute.token? -%>
t.string :<%= attribute.name %><%= attribute.inject_options %>
<% else -%>
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
<% end -%>
@@ -12,6 +14,9 @@ def change
t.timestamps
<% end -%>
end
<% attributes.select(&:token?).each do |attribute| -%>
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
<% end -%>
<% attributes_with_index.each do |attribute| -%>
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
<% end -%>
@@ -4,6 +4,9 @@ def change
<% attributes.each do |attribute| -%>
<%- if attribute.reference? -%>
add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
<%- elsif attribute.token? -%>
add_column :<%= table_name %>, :<%= attribute.name %>, :string<%= attribute.inject_options %>
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>, unique: true
<%- else -%>
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
<%- if attribute.has_index? -%>
@@ -3,6 +3,9 @@ class <%= class_name %> < <%= parent_class_name.classify %>
<% attributes.select(&:reference?).each do |attribute| -%>
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %><%= ', required: true' if attribute.required? %>
<% end -%>
<% attributes.select(&:token?).each do |attribute| -%>
has_secure_token<% if attribute.name != "token" %> :<%= attribute.name %><% end %>
<% end -%>
<% if attributes.any?(&:password_digest?) -%>
has_secure_password
<% end -%>
@@ -142,7 +142,11 @@ def has_uniq_index?
end
def password_digest?
name == 'password' && type == :digest
name == 'password' && type == :digest
end
def token?
type == :token
end
def inject_options
@@ -276,6 +276,30 @@ def test_create_table_migration_with_singular_table_name
end
end
def test_create_table_migration_with_token_option
run_generator ["create_users", "token:token", "auth_token:token"]
assert_migration "db/migrate/create_users.rb" do |content|
assert_method :change, content do |change|
assert_match(/create_table :users/, change)
assert_match(/ t\.string :token/, change)
assert_match(/ t\.string :auth_token/, change)
assert_match(/add_index :users, :token, unique: true/, change)
assert_match(/add_index :users, :auth_token, unique: true/, change)
end
end
end
def test_add_migration_with_token_option
migration = "add_token_to_users"
run_generator [migration, "auth_token:token"]
assert_migration "db/migrate/#{migration}.rb" do |content|
assert_method :change, content do |change|
assert_match(/add_column :users, :auth_token, :string/, change)
assert_match(/add_index :users, :auth_token, unique: true/, change)
end
end
end
private
def with_singular_table_name
@@ -438,6 +438,17 @@ def test_foreign_key_is_skipped_for_polymorphic_references
end
end
def test_token_option_adds_has_secure_token
run_generator ["user", "token:token", "auth_token:token"]
expected_file = <<-FILE.strip_heredoc
class User < ActiveRecord::Base
has_secure_token
has_secure_token :auth_token
end
FILE
assert_file "app/models/user.rb", expected_file
end
private
def assert_generated_fixture(path, parsed_contents)
fixture_file = File.new File.expand_path(path, destination_root)

0 comments on commit 72139d8

Please sign in to comment.