ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2009年10月16日 星期五
clearance 的 generator
clearance
Class Rails::Generator::Commands::Create
#這邊是需要的library
require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
require File.expand_path(File.dirname(__FILE__) + "/lib/rake_commands.rb")
require 'factory_girl'
class ClearanceGenerator < Rails::Generator::Base
def manifest
record do |m|
# 原來可以這樣insert GOOD
m.insert_into "app/controllers/application_controller.rb",
"include Clearance::Authentication"
user_model = "app/models/user.rb"
if File.exists?(user_model)
m.insert_into user_model, "include Clearance::User"
else
#原來也可以生目錄
m.directory File.join("app", "models")
m.file "user.rb", user_model
end
m.directory File.join("test", "factories")
m.file "factories.rb", "test/factories/clearance.rb"
#還可以選擇用哪個file migrate, 叫啥名字
m.migration_template "migrations/#{migration_name}.rb",
'db/migrate',
:migration_file_name => "clearance_#{migration_name}"
m.readme "README"
end
private
def migration_name
#可以check table 在不在
if ActiveRecord::Base.connection.table_exists?(:users)
'update_users'
else
'create_users'
end
end
end
#--------------------------------------------------
Rails::Generator::Commands::Create.class_eval do
def insert_into(file, line)
logger.insert "#{line} into #{file}"
unless options[:pretend] || file_contains?(file, line)
gsub_file file, /^(class|module) .+$/ do |match|
"#{match}\n #{line}"
end
end
end
end
#/clearance/templates/migrations/create_users.rb
class ClearanceCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.string :email
t.string :encrypted_password, :limit => 128
t.string :salt, :limit => 128
t.string :confirmation_token, :limit => 128
t.string :remember_token, :limit => 128
t.boolean :email_confirmed, :default => false, :null => false
t.timestamps
end
add_index :users, [:id, :confirmation_token]
add_index :users, :email
add_index :users, :remember_token
end
def self.down
drop_table :users
end
end
#-------------------------------------
#/clearance/templates/migrations/update_users.rb
class ClearanceUpdateUsers < ActiveRecord::Migration
def self.up
<%
# ActiveRecord::Base.connection.columns(:users).collect { |each| each.name } = User.columns.map { |each| each.name }
existing_columns = ActiveRecord::Base.connection.columns(:users).collect { |each| each.name }
columns = [
[:email, 't.string :email'],
[:encrypted_password, 't.string :encrypted_password, :limit => 128'],
[:salt, 't.string :salt, :limit => 128'],
[:confirmation_token, 't.string :confirmation_token, :limit => 128'],
[:remember_token, 't.string :remember_token, :limit => 128'],
[:email_confirmed, 't.boolean :email_confirmed, :default => false, :null => false']
].delete_if {|c| existing_columns.include?(c.first.to_s)}
-%>
change_table(:users) do |t|
<% columns.each do |c| -%>
<%= c.last %>
<% end -%>
end
<%
#連有哪些index 也可以看
existing_indexes = ActiveRecord::Base.connection.indexes(:users)
index_names = existing_indexes.collect { |each| each.name }
new_indexes = [
[:index_users_on_id_and_confirmation_token, 'add_index :users, [:id, :confirmation_token]'],
[:index_users_on_email, 'add_index :users, :email'],
[:index_users_on_remember_token, 'add_index :users, :remember_token']
].delete_if { |each| index_names.include?(each.first.to_s) }
-%>
<% new_indexes.each do |each| -%>
<%= each.last %>
<% end -%>
end
def self.down
change_table(:users) do |t|
<% unless columns.empty? -%>
t.remove <%= columns.collect { |each| ":#{each.first}" }.join(',') %>
<% end -%>
end
end
end
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言