Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update generator to support multiple mapping modes and specification …
…of the user model
- Loading branch information
Showing
4 changed files
with
85 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
35 changes: 34 additions & 1 deletion
35
generators/add_authlogic_rpx_migration/add_authlogic_rpx_migration_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
generators/add_authlogic_rpx_migration/templates/migration_no_mapping.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |