Skip to content

Commit

Permalink
update generator to support multiple mapping modes and specification …
Browse files Browse the repository at this point in the history
…of the user model
  • Loading branch information
tardate committed Dec 29, 2009
1 parent 211a777 commit 87499ca
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
18 changes: 16 additions & 2 deletions generators/add_authlogic_rpx_migration/USAGE
@@ -1,4 +1,18 @@
Description:
run ./script/generate add_authlogic_rpx_migration
ruby script/generate add_authlogic_rpx_migration [mapping:mapping_mode] [user_model:model_name]

the add_authlogic_rpx_migration file will be created in db/migrate
Creates an add_authlogic_rpx_migration file in db/migrate.

The mapping_mode parameter indicates which style of Authlogic_RPX-supported identity
mapping should be used. Allowed values for mapping_mode are:
none
internal
Default mapping_mode is 'internal'

The user_model parameter specifies the name of the user/member model in your application.
Default model_name is 'User'

e.g. to generate the RPX migration where the user model is called 'Member' and you do not
want to support identity mapping:

ruby script/generate add_authlogic_rpx_migration mapping:none user_model:member
@@ -1,11 +1,44 @@
class AddAuthlogicRpxMigrationGenerator < Rails::Generator::Base
def manifest

record do |m|
m.migration_template 'migration.rb', 'db/migrate'

m.migration_template template_name, 'db/migrate', :assigns => {
:user_model_base => user_model_base,
:user_model => user_model,
:user_model_collection => user_model_collection
}
end
end

def file_name
"add_authlogic_rpx_migration"
end

protected
# Override with your own usage banner.
def banner
"Usage: #{$0} #{spec.name} [options] [mapping:mapping_mode] [user_model:model_name]"
end

attr_writer :params
def params
@params ||= {"mapping" => "internal", "user_model" => "User"}.merge( Hash[*(@args.collect { |arg| arg.split(":") }.flatten)] )
end

def user_model_base
params['user_model'].singularize.downcase
end
def user_model
params['user_model'].singularize.capitalize
end
def user_model_collection
params['user_model'].pluralize.downcase
end
def mapping
params['mapping']
end
def template_name
mapping == 'none' ? 'migration_no_mapping.rb' : 'migration_internal_mapping.rb'
end
end
Expand Up @@ -3,19 +3,19 @@ def self.up
create_table :rpx_identifiers do |t|
t.string :identifier
t.string :provider_name
t.integer :user_id
t.integer :<%= user_model_base %>_id
t.timestamps
end
add_index :rpx_identifiers, :identifier, :unique => true, :null => false
add_index :rpx_identifiers, :user_id, :unique => false, :null => false
add_index :rpx_identifiers, :<%= user_model_base %>_id, :unique => false, :null => false

# == Customisation may be required here ==
# You may need to remove database constraints on other fields if they will be unused in the RPX case
# (e.g. crypted_password and password_salt to make password authentication optional).
# If you are using auto-registration, you must also remove any database constraints for fields that will be automatically mapped
# e.g.:
#change_column :users, :crypted_password, :string, :default => nil, :null => true
#change_column :users, :password_salt, :string, :default => nil, :null => true
#change_column :<%= user_model_collection %>, :crypted_password, :string, :default => nil, :null => true
#change_column :<%= user_model_collection %>, :password_salt, :string, :default => nil, :null => true

end

Expand All @@ -26,8 +26,8 @@ def self.down
# Restore user model database constraints as appropriate
# e.g.:
#[:crypted_password, :password_salt].each do |field|
# User.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
# change_column :users, field, :string, :default => "", :null => false
# <%= user_model %>.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
# change_column :<%= user_model_collection %>, field, :string, :default => "", :null => false
#end

end
Expand Down
@@ -0,0 +1,29 @@
class AddAuthlogicRpxMigration < ActiveRecord::Migration

def self.up
add_column :<%= user_model_collection %>, :rpx_identifier, :string
add_index :<%= user_model_collection %>, :rpx_identifier

# == Customisation may be required here ==
# You may need to remove database constraints on other fields if they will be unused in the RPX case
# (e.g. crypted_password and password_salt to make password authentication optional).
# If you are using auto-registration, you must also remove any database constraints for fields that will be automatically mapped
# e.g.:
#change_column :<%= user_model_collection %>, :crypted_password, :string, :default => nil, :null => true
#change_column :<%= user_model_collection %>, :password_salt, :string, :default => nil, :null => true

end

def self.down
remove_column :<%= user_model_collection %>, :rpx_identifier

# == Customisation may be required here ==
# Restore user model database constraints as appropriate
# e.g.:
#[:crypted_password, :password_salt].each do |field|
# <%= user_model %>.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
# change_column :<%= user_model_collection %>, field, :string, :default => "", :null => false
#end

end
end

0 comments on commit 87499ca

Please sign in to comment.