Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

roles generator works

  • Loading branch information...
commit 4b6f008ca32bd9c071c704b465763c6c7fdfb367 1 parent ecab47a
@kristianmandrup authored
Showing with 131 additions and 30 deletions.
  1. 0  .document
  2. 0  .gitignore
  3. 0  .rspec
  4. 0  Gemfile
  5. 0  LICENSE
  6. 0  README.markdown
  7. 0  Rakefile
  8. 0  VERSION
  9. 0  development.sqlite3
  10. +55 −29 lib/generators/active_record/roles/roles_generator.rb
  11. 0  lib/generators/active_record/roles_migration/roles_migration_generator.rb
  12. 0  lib/generators/active_record/roles_migration/templates/add_admin_flag_strategy.erb
  13. 0  lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb
  14. 0  lib/generators/active_record/roles_migration/templates/add_one_role_strategy.erb
  15. 0  lib/generators/active_record/roles_migration/templates/add_role_string_strategy.erb
  16. 0  lib/generators/active_record/roles_migration/templates/add_roles_mask_to_users_migration.erb
  17. 0  lib/generators/active_record/setup/setup_generator.rb
  18. 0  lib/roles_active_record/base.rb
  19. 0  lib/roles_active_record/namespaces.rb
  20. +1 −1  lib/roles_active_record/role.rb
  21. 0  lib/roles_active_record/strategy.rb
  22. 0  lib/roles_active_record/strategy/multi/many_roles.rb
  23. 0  lib/roles_active_record/strategy/multi/roles_mask.rb
  24. 0  lib/roles_active_record/strategy/single/admin_flag.rb
  25. 0  lib/roles_active_record/strategy/single/one_role.rb
  26. 0  lib/roles_active_record/strategy/single/role_string.rb
  27. 0  lib/roles_for_active_record.rb
  28. 0  lib/views/_multi_role_selector.erb.html
  29. 0  lib/views/_single_role_selector.erb.html
  30. +4 −0 logging.log
  31. 0  roles_active_record.gemspec
  32. 0  roles_for_ar.gemspec
  33. 0  sandbox/Rakefile
  34. 0  sandbox/add_role_to_users_migration.erb
  35. 0  sandbox/create_roles_migration.erb
  36. 0  sandbox/create_user_roles_migration.erb
  37. 0  sandbox/database.log
  38. 0  sandbox/database.yml
  39. 0  sandbox/development.sqlite3
  40. 0  sandbox/migrate/201002508_create_roles.rb
  41. 0  sandbox/migrate/20102507_create_users.rb
  42. 0  sandbox/model_base.rb
  43. 0  sandbox/test.rb
  44. 0  spec/db/database.yml
  45. 0  spec/fixtures/many_roles_setup.rb
  46. 0  spec/fixtures/one_role_setup.rb
  47. +13 −0 spec/generator_spec_helper.rb
  48. 0  spec/migrations/admin_flag/004_add_admin_flag_to_users.rb
  49. 0  spec/migrations/many_roles/002_create_roles.rb
  50. 0  spec/migrations/many_roles/003_create_user_roles.rb
  51. 0  spec/migrations/one_role/002_create_roles.rb
  52. 0  spec/migrations/one_role/003_add_role_to_users.rb
  53. 0  spec/migrations/role_string/002_add_role_string_to_users.rb
  54. 0  spec/migrations/roles_mask/005_add_roles_mask_to_users.rb
  55. 0  spec/migrations/users/001_create_users.rb
  56. 0  spec/roles_active_record/admin_flag_spec.rb
  57. 0  spec/roles_active_record/generators/migration_spec_helper.rb
  58. +56 −0 spec/roles_active_record/generators/roles_generator_spec.rb
  59. 0  spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb
  60. 0  spec/roles_active_record/generators/roles_migration/many_roles_spec.rb
  61. 0  spec/roles_active_record/generators/roles_migration/one_role_spec.rb
  62. 0  spec/roles_active_record/generators/roles_migration/role_string_spec.rb
  63. 0  spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb
  64. 0  spec/roles_active_record/generators/setup_generator_spec.rb
  65. 0  spec/roles_active_record/many_roles_spec.rb
  66. 0  spec/roles_active_record/one_role_spec.rb
  67. 0  spec/roles_active_record/role_string_spec.rb
  68. 0  spec/roles_active_record/roles_mask_spec.rb
  69. 0  spec/spec_helper.rb
  70. +2 −0  tmp/rails/config/routes.rb
View
0  .document 100644 → 100755
File mode changed
View
0  .gitignore 100644 → 100755
File mode changed
View
0  .rspec 100644 → 100755
File mode changed
View
0  Gemfile 100644 → 100755
File mode changed
View
0  LICENSE 100644 → 100755
File mode changed
View
0  README.markdown 100644 → 100755
File mode changed
View
0  Rakefile 100644 → 100755
File mode changed
View
0  VERSION 100644 → 100755
File mode changed
View
0  development.sqlite3 100644 → 100755
File mode changed
View
84 lib/generators/active_record/roles/roles_generator.rb 100644 → 100755
@@ -1,46 +1,72 @@
+require 'rails3_artifactor'
+require 'logging_assist'
+
module ActiveRecord
module Generators
- class RolesGenerator < Rails::Generators::NamedBase
- desc "Generate roles model for User"
+ class RolesGenerator < Rails::Generators::NamedBase
+ desc "Add role strategy to a model"
- argument :role_strategy, :type => :string, :aliases => "-r", :default => 'inline_role', :desc => "Create roles model for user"
+ class_option :strategy, :type => :string, :aliases => "-s", :default => 'role_string',
+ :desc => "Role strategy to use (admin_flag, role_string, roles_string, role_strings, one_role, many_roles, roles_mask)"
- hook_for :orm
-
- def self.source_root
- @source_root ||= File.expand_path("../../templates", __FILE__)
- end
- def apply_role_strategy
- insert_into_model('user', role_strategy_statement)
+ class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
+
+ def apply_role_strategy
+ log.add_logfile
+ log.debug "apply_role_strategy for : #{strategy} in model #{name}"
+ begin
+ insert_into_model name do
+ insertion_text
+ end
+ rescue
+ say "Model #{name} not found"
+ end
end
protected
- def match_expr
- /< (.+?)\w+\s/
- end
+ extend Rails3::Assist::UseMacro
+ use_orm :active_record
+ include Rails::Assist::BasicLogging
+ def orm
+ :active_record
+ end
+
+ def default_roles
+ [:admin, :guest]
+ end
+
+ def roles_to_add
+ @roles_to_add ||= default_roles.concat(options[:roles]).to_symbols.uniq
+ end
+
+ def roles
+ roles_to_add.map{|r| ":#{r}" }
+ end
+
def role_strategy_statement
- "role_strategy #{role_strategy}"
+ "strategy :#{strategy}, :default\n#{role_class_stmt}"
end
- def role_strategy
- options[:role_strategy]
+ def role_class_stmt
+ " role_class :role" if [:one_role, :many_roles].include? (strategy.to_sym)
+ end
+
+ def roles_statement
+ roles ? "valid_roles_are #{roles.join(', ')}" : ''
+ end
+
+ def insertion_text
+ %Q{include Roles::#{orm.to_s.camelize}
+ #{role_strategy_statement}
+ #{roles_statement}}
+ end
+
+ def strategy
+ options[:strategy]
end
-
- def model_file(name)
- File.join(Rails.root, "app/models/#{name}.rb")
- end
-
- def insert_into_model(model_name, insert_text)
- model_name = model_name.to_s
- file = File.new(model_file(model_name))
- return if (file.read =~ /#{insert_text}/)
- gsub_file model_file(model_name), match_expr do |match|
- match << insert_text
- end
- end
end
end
end
View
0  lib/generators/active_record/roles_migration/roles_migration_generator.rb 100644 → 100755
File mode changed
View
0  lib/generators/active_record/roles_migration/templates/add_admin_flag_strategy.erb 100644 → 100755
File mode changed
View
0  lib/generators/active_record/roles_migration/templates/add_many_roles_strategy.erb 100644 → 100755
File mode changed
View
0  lib/generators/active_record/roles_migration/templates/add_one_role_strategy.erb 100644 → 100755
File mode changed
View
0  lib/generators/active_record/roles_migration/templates/add_role_string_strategy.erb 100644 → 100755
File mode changed
View
0  lib/generators/active_record/roles_migration/templates/add_roles_mask_to_users_migration.erb 100644 → 100755
File mode changed
View
0  lib/generators/active_record/setup/setup_generator.rb 100644 → 100755
File mode changed
View
0  lib/roles_active_record/base.rb 100644 → 100755
File mode changed
View
0  lib/roles_active_record/namespaces.rb 100644 → 100755
File mode changed
View
2  lib/roles_active_record/role.rb 100644 → 100755
@@ -3,7 +3,7 @@ def valid_roles_are(*role_list)
strategy_class.valid_roles = role_list.to_symbols
if role_class_name
role_list.each do |name|
- res = role_class_name.create(:name => name.to_s)
+ role_class_name.create(:name => name.to_s)
end
end
end
View
0  lib/roles_active_record/strategy.rb 100644 → 100755
File mode changed
View
0  lib/roles_active_record/strategy/multi/many_roles.rb 100644 → 100755
File mode changed
View
0  lib/roles_active_record/strategy/multi/roles_mask.rb 100644 → 100755
File mode changed
View
0  lib/roles_active_record/strategy/single/admin_flag.rb 100644 → 100755
File mode changed
View
0  lib/roles_active_record/strategy/single/one_role.rb 100644 → 100755
File mode changed
View
0  lib/roles_active_record/strategy/single/role_string.rb 100644 → 100755
File mode changed
View
0  lib/roles_for_active_record.rb 100644 → 100755
File mode changed
View
0  lib/views/_multi_role_selector.erb.html 100644 → 100755
File mode changed
View
0  lib/views/_single_role_selector.erb.html 100644 → 100755
File mode changed
View
4 logging.log
@@ -0,0 +1,4 @@
+[Rails::Assist::Logging]
+apply_role_strategy for : admin_flag in model User
+[Rails::Assist::Logging]
+apply_role_strategy for : one_role in model User
View
0  roles_active_record.gemspec 100644 → 100755
File mode changed
View
0  roles_for_ar.gemspec 100644 → 100755
File mode changed
View
0  sandbox/Rakefile 100644 → 100755
File mode changed
View
0  sandbox/add_role_to_users_migration.erb 100644 → 100755
File mode changed
View
0  sandbox/create_roles_migration.erb 100644 → 100755
File mode changed
View
0  sandbox/create_user_roles_migration.erb 100644 → 100755
File mode changed
View
0  sandbox/database.log 100644 → 100755
File mode changed
View
0  sandbox/database.yml 100644 → 100755
File mode changed
View
0  sandbox/development.sqlite3 100644 → 100755
File mode changed
View
0  sandbox/migrate/201002508_create_roles.rb 100644 → 100755
File mode changed
View
0  sandbox/migrate/20102507_create_users.rb 100644 → 100755
File mode changed
View
0  sandbox/model_base.rb 100644 → 100755
File mode changed
View
0  sandbox/test.rb 100644 → 100755
File mode changed
View
0  spec/db/database.yml 100644 → 100755
File mode changed
View
0  spec/fixtures/many_roles_setup.rb 100644 → 100755
File mode changed
View
0  spec/fixtures/one_role_setup.rb 100644 → 100755
File mode changed
View
13 spec/generator_spec_helper.rb
@@ -0,0 +1,13 @@
+require 'rspec'
+require 'rspec/autorun'
+require 'rails3_artifactor'
+require 'generator-spec'
+require 'roles_generic'
+
+RSpec::Generator.configure do |config|
+ config.debug = true #false
+ config.remove_temp_dir = false # true
+ config.default_rails_root(__FILE__)
+ config.lib = File.dirname(__FILE__) + '/../lib'
+ config.logger = :stdout
+end
View
0  spec/migrations/admin_flag/004_add_admin_flag_to_users.rb 100644 → 100755
File mode changed
View
0  spec/migrations/many_roles/002_create_roles.rb 100644 → 100755
File mode changed
View
0  spec/migrations/many_roles/003_create_user_roles.rb 100644 → 100755
File mode changed
View
0  spec/migrations/one_role/002_create_roles.rb 100644 → 100755
File mode changed
View
0  spec/migrations/one_role/003_add_role_to_users.rb 100644 → 100755
File mode changed
View
0  spec/migrations/role_string/002_add_role_string_to_users.rb 100644 → 100755
File mode changed
View
0  spec/migrations/roles_mask/005_add_roles_mask_to_users.rb 100644 → 100755
File mode changed
View
0  spec/migrations/users/001_create_users.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/admin_flag_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/generators/migration_spec_helper.rb 100644 → 100755
File mode changed
View
56 spec/roles_active_record/generators/roles_generator_spec.rb
@@ -0,0 +1,56 @@
+require 'generator_spec_helper'
+require_generator :active_record => :roles
+
+# root_dir = Rails3::Assist::Directory.rails_root
+root_dir = File.join(Rails.application.config.root_dir, 'rails')
+
+describe 'role strategy generator: admin_flag' do
+ describe 'ORM: active_record' do
+ use_orm :active_record
+
+ before :each do
+ setup_generator 'datamapper_roles_generator' do
+ tests ActiveRecord::Generators::RolesGenerator
+ end
+ remove_model :user
+ end
+
+ after :each do
+ remove_model :user
+ end
+
+ it "should configure 'admin_flag' strategy" do
+ create_model :user do
+ '# content'
+ end
+ with_generator do |g|
+ arguments = "User --strategy admin_flag --roles admin user"
+ puts "arguments: #{arguments}"
+ g.run_generator arguments.args
+ root_dir.should have_model :user do |clazz|
+ clazz.should include_module 'Roles::ActiveRecord'
+ clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
+ clazz.should have_call :strategy, :args => ":admin_flag"
+ end
+ end
+ end
+
+ it "should configure 'one_role' strategy" do
+ create_model :user do
+ '# content'
+ end
+ with_generator do |g|
+ arguments = "User --strategy one_role --roles admin user"
+ puts "arguments: #{arguments}"
+ g.run_generator arguments.args
+ root_dir.should have_model :user do |clazz|
+ clazz.should include_module 'Roles::ActiveRecord'
+ clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
+ clazz.should have_call :role_class, :args => ":role"
+ clazz.should have_call :strategy, :args => ":one_role"
+ end
+ end
+ end
+ end
+end
+
View
0  spec/roles_active_record/generators/roles_migration/admin_flag_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/generators/roles_migration/many_roles_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/generators/roles_migration/one_role_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/generators/roles_migration/role_string_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/generators/roles_migration/roles_mask_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/generators/setup_generator_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/many_roles_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/one_role_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/role_string_spec.rb 100644 → 100755
File mode changed
View
0  spec/roles_active_record/roles_mask_spec.rb 100644 → 100755
File mode changed
View
0  spec/spec_helper.rb 100644 → 100755
File mode changed
View
2  tmp/rails/config/routes.rb
@@ -0,0 +1,2 @@
+Rails.application.routes.draw do
+end
Please sign in to comment.
Something went wrong with that request. Please try again.