Class: RoleMaking::Generators::RoleMakingGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- RoleMaking::Generators::RoleMakingGenerator
- Defined in:
- lib/generators/role_making_generator.rb
Instance Method Summary collapse
- #copy_resource_file ⇒ Object
-
#create_ability_model ⇒ Object
add ability files.
- #create_role_model ⇒ Object
- #inject_user_class ⇒ Object
- #show_readme ⇒ Object
Instance Method Details
#copy_resource_file ⇒ Object
21 22 23 |
# File 'lib/generators/role_making_generator.rb', line 21 def copy_resource_file copy_file "resource.rb",File.join("app","models","resource.rb") end |
#create_ability_model ⇒ Object
add ability files
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/generators/role_making_generator.rb', line 34 def create_ability_model Rails::Generators.invoke "cancan:ability",["-s"],behavior: behavior model_path = File.join("app", "models", "ability.rb") insert_into_file(model_path, after: " # end\n") do #可以通过对该用户的每个角色来初始化权限,而不用通过轮循所有角色 <<-EOF ##{role_cname.classify}.all_without_reserved.each do |role| # next unless user.has_role?(role.name) user.roles.each do |role| role.role_resources.each do |res| resource = Resource.find_by_name(res.resource_name) rescue next if block = resource.behavior can resource.verb,resource.object do |obj| block.call(user,obj) end elsif resource.hashs eval_con = resource.hashs.delete(:con).try(:inject,{}) do |r,(k,v)| r[k] = eval(v) r end || {} can resource[:verb],resource[:object],resource[:hashs].except(:con).merge(eval_con) else can resource[:verb],resource[:object] end end end \n EOF end if behavior == :invoke end |
#create_role_model ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/generators/role_making_generator.rb', line 25 def create_role_model Rails::Generators.invoke "active_record:model",["RoleResource","--no-migration","--no-fixture"], behavior: behavior Rails::Generators.invoke "active_record:model",[role_cname,"--no-migration","--no-fixture"], behavior: behavior inject_role_class if behavior == :invoke inject_role_resource_class if behavior == :invoke create_migration end |
#inject_user_class ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/generators/role_making_generator.rb', line 12 def inject_user_class model_path = File.join("app", "models", "#{name.underscore}.rb") insert_into_file(model_path,after: "ApplicationRecord\n") do " role_making role_cname: '#{role_cname.classify}'\n" end end |
#show_readme ⇒ Object
67 68 69 70 71 |
# File 'lib/generators/role_making_generator.rb', line 67 def show_readme if behavior == :invoke readme "../USAGE" end end |