Permalink
Browse files

has_and_belongs_to_many roles generator verified working in Rails 2.0

git-svn-id: http://rolerequirement.googlecode.com/svn/trunk@82 0128ec72-9f35-0410-83f6-31648bef6b25
  • Loading branch information...
1 parent b0bac75 commit 3376d63781dbf1d36bb6795494ee7b29cdaeca84 timcharper committed Nov 13, 2007
@@ -44,7 +44,7 @@ def add_method_to_user_model(m)
end
def add_migration(m)
- m.migration_template 'add_role_to_users_migration.rb', 'db/migrate', :assigns => {
+ m.migration_template '001_add_role_to_users_migration.rb.erb', 'db/migrate', :assigns => {
:migration_name => "AddRoleTo#{users_model_name.pluralize.gsub(/::/, '')}"
}, :migration_file_name => "add_role_to_#{users_table_name}"
end
@@ -23,70 +23,82 @@ def initialize(runtime_args, runtime_options = {})
def manifest
record do |m|
- skip_fixtures = false
- # generate fixtures
- if (File.exists?(users_fixture_filename))
- users_fixtures_content = File.read users_fixture_filename
- users_fixtures = YAML.load(users_fixtures_content)
+ modify_or_add_user_fixtures(m)
+ add_roles_and_join_table_fixtures(m)
+
+ add_method_to_user_model(m)
+
+ add_role_model(m)
+ add_dependencies_to_application_rb
+ add_dependencies_to_test_helper_rb
+ add_role_requirement_system(m)
+ add_migration(m) unless options[:skip_migration]
+ end
+ end
+
+ def add_role_model(m)
+ # add the Role model
+ m.template 'role_model.rb.erb', roles_model_filename
+ end
+
+ def add_method_to_user_model(m)
+ content_for_insertion = render_template("_user_functions.erb")
+ # modify the User model unless it's already got RoleRequirement code in there
+ if insert_content_after(users_model_filename,
+ Regexp.new("class +#{users_model_name}"),
+ content_for_insertion,
+ :unless => lambda { |content| content.include? "def has_role?"; }
+ )
+ puts "Added the following to the top of #{users_model_filename}:\n#{content_for_insertion}"
+ else
+ puts "Not modifying #{users_model_filename} because it appears that the funtion has_role? already exists."
+ end
+ end
+
+ def modify_or_add_user_fixtures(m)
+ if (File.exists?(users_fixture_filename))
+ users_fixtures_content = File.read users_fixture_filename
+ users_fixtures = YAML.load(users_fixtures_content)
+
+ begin
+ throw "Can't understand whatever is in #{users_fixture_filename}" unless Hash===users_fixtures
- begin
- throw "Can't understand whatever is in #{users_fixture_filename}" unless Hash===users_fixtures
+ unless users_fixtures.has_key?("admin")
+ @next_user_id = (users_fixtures.collect{ |k, params| params["id"].to_i}.max||0) + 1
+ output = users_fixtures_content + "\n" + render_template("users_admin_fixture_with_roles.yml")
- unless users_fixtures.has_key?("admin")
- @next_user_id = (users_fixtures.collect{ |k, params| params["id"].to_i}.max||0) + 1
- output = users_fixtures_content + "\n" + render_template("users_admin_fixture_with_roles.yml")
-
- # prevent generator from truncating the whole file if something goes wrong.
- if output.length > users_fixtures_content.length
- File.open(users_fixture_filename, "w") {|f| f.write(output) }
- end
- else
- @next_user_id = users_fixtures["admin"]["id"].to_i
+ # prevent generator from truncating the whole file if something goes wrong.
+ if output.length > users_fixtures_content.length
+ File.open(users_fixture_filename, "w") {|f| f.write(output) }
end
- rescue e
- puts e.message
- skip_fixtures = true
+ else
+ @next_user_id = users_fixtures["admin"]["id"].to_i
end
- else
- # users.yml doesn't exist. Generate it from scratch
- @next_user_id = 1
-
- m.template 'users_admin_fixture_with_roles.yml',
- File.join('test/fixtures', "#{users_table_name}.yml")
+ rescue e
+ puts e.message
+ skip_fixtures = true
end
+ else
+ # users.yml doesn't exist. Generate it from scratch
+ @next_user_id = 1
- unless skip_fixtures
- # generate roles and users_roles
- m.template 'roles_users.yml',
- File.join('test/fixtures', "#{habtm_name}.yml")
- m.template 'roles.yml',
- File.join('test/fixtures', "#{roles_table_name}.yml")
- end
-
- content_for_insertion = render_template("_user_functions.erb")
- # modify the User model unless it's already got RoleRequirement code in there
- if insert_content_after(users_model_filename,
- Regexp.new("class +#{users_model_name}"),
- content_for_insertion,
- :unless => lambda { |content| content.include? "def has_role?"; }
- )
- puts "Added the following to the top of #{users_model_filename}:\n#{content_for_insertion}"
- else
- puts "Not modifying #{users_model_filename} because it appears that the funtion has_role? already exists."
- end
-
- # add the Role model
- m.template 'role_model.rb', roles_model_filename
- # generate migration
- unless options[:skip_migration]
- m.migration_template 'roles_migration.rb', 'db/migrate', :assigns => {
- :migration_name => "Create#{roles_model_name.pluralize.gsub(/::/, '')}"
- }, :migration_file_name => "create_#{roles_table_name}"
- end
-
- add_dependencies_to_application_rb
-
+ m.template 'users_admin_fixture_with_roles.yml',
+ File.join('test/fixtures', "#{users_table_name}.yml")
end
+
+ end
+
+ def add_migration(m)
+ m.migration_template '001_roles_migration.rb.erb', 'db/migrate', :assigns => {
+ :migration_name => "Create#{roles_model_name.pluralize.gsub(/::/, '')}"
+ }, :migration_file_name => "create_#{roles_table_name}"
+ end
+
+ def add_roles_and_join_table_fixtures(m)
+ m.template 'roles_users.yml',
+ File.join('test/fixtures', "#{habtm_name}.yml")
+ m.template 'roles.yml',
+ File.join('test/fixtures', "#{roles_table_name}.yml")
end
def render_template(name)
@@ -98,7 +110,7 @@ def habtm_name; [roles_table_name, users_table_name].sort * "_"; end
def roles_foreign_key; roles_table_name.singularize.foreign_key; end
def roles_model_filename; "app/models/#{roles_model_name.underscore}.rb"; end;
def users_foreign_key; users_table_name.singularize.foreign_key; end
- def users_fixture_filename; "test/fixtures/users.yml"; end;
+ def users_fixture_filename; "test/fixtures/#{users_table_name}.yml"; end;
protected
def banner
"Usage: #{$0} roles RoleModelName [TargetUserModelName]"

0 comments on commit 3376d63

Please sign in to comment.