the_role 2.1.1 → 2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +27 -3
  3. data/.ruby-gemset.example +1 -0
  4. data/.ruby-version.example +1 -0
  5. data/.rvmrc.example +1 -1
  6. data/.travis.yml +2 -2
  7. data/README.md +46 -47
  8. data/app/assets/javascripts/the_role_editinplace.js.coffee +32 -0
  9. data/app/controllers/admin/role_sections_controller.rb +2 -2
  10. data/app/controllers/admin/roles_controller.rb +2 -2
  11. data/app/controllers/concerns/controller.rb +23 -0
  12. data/{lib/generators/the_role/templates → app/models/_templates_}/role.rb +1 -1
  13. data/app/models/concerns/base.rb +37 -0
  14. data/app/models/concerns/role.rb +134 -0
  15. data/app/models/concerns/user.rb +57 -0
  16. data/app/views/admin/roles/_role.html.haml +90 -69
  17. data/app/views/admin/roles/_sidebar.html.haml +16 -9
  18. data/app/views/admin/roles/new.html.haml +11 -10
  19. data/config/locales/en.yml +1 -0
  20. data/config/locales/es.yml +28 -28
  21. data/config/locales/ru.yml +1 -1
  22. data/db/migrate/20111025025129_create_roles.rb +7 -1
  23. data/docs/2.1.1.png +0 -0
  24. data/lib/generators/the_role/USAGE +1 -1
  25. data/lib/generators/the_role/templates/the_role.rb +7 -2
  26. data/lib/generators/the_role/the_role_generator.rb +8 -5
  27. data/lib/tasks/roles.rake +1 -1
  28. data/lib/the_role/activerecord.rb +11 -0
  29. data/lib/the_role/config.rb +7 -0
  30. data/lib/the_role/version.rb +1 -1
  31. data/lib/the_role.rb +19 -5
  32. data/spec/dummy_app/.gitignore +18 -0
  33. data/spec/dummy_app/.rspec +1 -0
  34. data/spec/dummy_app/Gemfile +32 -0
  35. data/spec/dummy_app/README.md +39 -0
  36. data/spec/dummy_app/Rakefile +6 -0
  37. data/spec/dummy_app/app/assets/images/.keep +0 -0
  38. data/spec/dummy_app/app/assets/javascripts/application.js +17 -0
  39. data/spec/dummy_app/app/assets/stylesheets/app/style.css.scss +23 -0
  40. data/spec/dummy_app/app/assets/stylesheets/application.css +3 -0
  41. data/spec/dummy_app/app/controllers/application_controller.rb +38 -0
  42. data/spec/dummy_app/app/controllers/concerns/.keep +0 -0
  43. data/spec/dummy_app/app/controllers/pages_controller.rb +75 -0
  44. data/spec/dummy_app/app/controllers/users_controller.rb +32 -0
  45. data/spec/dummy_app/app/controllers/welcome_controller.rb +13 -0
  46. data/spec/dummy_app/app/helpers/application_helper.rb +2 -0
  47. data/spec/dummy_app/app/mailers/.keep +0 -0
  48. data/spec/dummy_app/app/models/.keep +0 -0
  49. data/spec/dummy_app/app/models/concerns/.keep +0 -0
  50. data/spec/dummy_app/app/models/page.rb +24 -0
  51. data/spec/dummy_app/app/models/role.rb +4 -0
  52. data/spec/dummy_app/app/models/user.rb +21 -0
  53. data/spec/dummy_app/app/views/layouts/_current_user_info.html.haml +17 -0
  54. data/spec/dummy_app/app/views/layouts/application.html.haml +68 -0
  55. data/spec/dummy_app/app/views/pages/_form.html.haml +19 -0
  56. data/spec/dummy_app/app/views/pages/edit.html.haml +7 -0
  57. data/spec/dummy_app/app/views/pages/index.html.haml +27 -0
  58. data/spec/dummy_app/app/views/pages/manage.html.haml +23 -0
  59. data/spec/dummy_app/app/views/pages/my.html.haml +10 -0
  60. data/spec/dummy_app/app/views/pages/new.html.haml +5 -0
  61. data/spec/dummy_app/app/views/pages/show.html.haml +15 -0
  62. data/spec/dummy_app/app/views/users/edit.html.haml +34 -0
  63. data/spec/dummy_app/app/views/welcome/index.html.haml +38 -0
  64. data/spec/dummy_app/app/views/welcome/profile.html.haml +2 -0
  65. data/spec/dummy_app/bin/bundle +3 -0
  66. data/spec/dummy_app/bin/rails +4 -0
  67. data/spec/dummy_app/bin/rake +4 -0
  68. data/spec/dummy_app/config/application.rb +23 -0
  69. data/spec/dummy_app/config/boot.rb +4 -0
  70. data/spec/dummy_app/config/database.yml +17 -0
  71. data/spec/dummy_app/config/environment.rb +5 -0
  72. data/spec/dummy_app/config/environments/development.rb +29 -0
  73. data/spec/dummy_app/config/environments/production.rb +80 -0
  74. data/spec/dummy_app/config/environments/test.rb +36 -0
  75. data/spec/dummy_app/config/initializers/backtrace_silencers.rb +7 -0
  76. data/spec/dummy_app/config/initializers/devise.rb +227 -0
  77. data/spec/dummy_app/config/initializers/filter_parameter_logging.rb +4 -0
  78. data/spec/dummy_app/config/initializers/inflections.rb +16 -0
  79. data/spec/dummy_app/config/initializers/mime_types.rb +5 -0
  80. data/spec/dummy_app/config/initializers/secret_token.rb +12 -0
  81. data/spec/dummy_app/config/initializers/session_store.rb +3 -0
  82. data/spec/dummy_app/config/initializers/the_role.rb +6 -0
  83. data/spec/dummy_app/config/initializers/wrap_parameters.rb +14 -0
  84. data/spec/dummy_app/config/locales/devise.en.yml +57 -0
  85. data/spec/dummy_app/config/locales/en.yml +7 -0
  86. data/spec/dummy_app/config/locales/ru.yml +4 -0
  87. data/spec/dummy_app/config/routes.rb +20 -0
  88. data/spec/dummy_app/config.ru +4 -0
  89. data/spec/dummy_app/db/migrate/20120212061952_devise_create_users.rb +63 -0
  90. data/spec/dummy_app/db/migrate/20120212063249_create_roles.rb +18 -0
  91. data/spec/dummy_app/db/migrate/20120314061307_create_pages.rb +14 -0
  92. data/spec/dummy_app/db/schema.rb +59 -0
  93. data/spec/dummy_app/db/seeds.rb +85 -0
  94. data/spec/dummy_app/lib/assets/.keep +0 -0
  95. data/spec/dummy_app/lib/tasks/.keep +0 -0
  96. data/spec/dummy_app/lib/tasks/assets.rake +15 -0
  97. data/spec/dummy_app/lib/tasks/db_bootstrap.rake +16 -0
  98. data/spec/dummy_app/log/.keep +0 -0
  99. data/spec/dummy_app/public/404.html +58 -0
  100. data/spec/dummy_app/public/422.html +58 -0
  101. data/spec/dummy_app/public/500.html +57 -0
  102. data/spec/dummy_app/public/favicon.ico +0 -0
  103. data/spec/dummy_app/public/robots.txt +5 -0
  104. data/spec/dummy_app/spec/controllers/admin_roles_controller_spec.rb +52 -0
  105. data/spec/dummy_app/spec/controllers/pages_controller_spec.rb +141 -0
  106. data/spec/dummy_app/spec/controllers/welcome_controller_spec.rb +66 -0
  107. data/spec/dummy_app/spec/factories/page.rb +6 -0
  108. data/spec/dummy_app/spec/factories/role.rb +55 -0
  109. data/spec/dummy_app/spec/factories/user.rb +11 -0
  110. data/spec/dummy_app/spec/models/hash_spec.rb +272 -0
  111. data/spec/dummy_app/spec/models/param_process_spec.rb +44 -0
  112. data/spec/dummy_app/spec/models/role_spec.rb +218 -0
  113. data/spec/dummy_app/spec/models/user_spec.rb +164 -0
  114. data/spec/dummy_app/spec/routing/admin_roles_routing_spec.rb +41 -0
  115. data/spec/dummy_app/spec/routing/pages_routing_spec.rb +35 -0
  116. data/spec/dummy_app/spec/spec_helper.rb +68 -0
  117. data/spec/dummy_app/vendor/assets/javascripts/.keep +0 -0
  118. data/spec/dummy_app/vendor/assets/stylesheets/.keep +0 -0
  119. data/the_role.gemspec +2 -1
  120. data/the_role.yml.teamocil.example +11 -0
  121. metadata +213 -16
  122. data/app/assets/javascripts/the_role.js.coffee +0 -31
  123. data/app/assets/stylesheets/the_role/bootstrap_sass.css.scss +0 -20
  124. data/app/assets/stylesheets/the_role.css.scss +0 -47
  125. data/app/controllers/the_role_controller.rb +0 -18
  126. data/app/models/concerns/role_model.rb +0 -125
  127. data/app/models/concerns/the_role_base.rb +0 -35
  128. data/app/models/concerns/the_role_user_model.rb +0 -54
@@ -1,125 +0,0 @@
1
- module RoleModel
2
- extend ActiveSupport::Concern
3
-
4
- include TheRoleBase
5
-
6
- def role_hash; to_hash; end
7
- alias_method :has?, :has_role?
8
- alias_method :any?, :any_role?
9
-
10
- def has_section? section_name
11
- to_hash.key? TheRoleParam.process(section_name)
12
- end
13
-
14
- included do
15
- has_many :users
16
- validates :name, presence: true, uniqueness: true
17
- validates :title, presence: true, uniqueness: true
18
- validates :description, presence: true
19
-
20
- before_save do
21
- self.name = TheRoleParam.process(name)
22
-
23
- rules_set = self.the_role
24
- self.the_role = {}.to_json if rules_set.blank? # blank
25
- self.the_role = rules_set.to_json if rules_set.is_a?(Hash) # Hash
26
- end
27
- end
28
-
29
- module ClassMethods
30
- def with_name name
31
- where(name: name).first
32
- end
33
- end
34
-
35
- # C
36
-
37
- def create_section section_name = nil
38
- return false unless section_name
39
- role = to_hash
40
- section_name = TheRoleParam.process(section_name)
41
- return false if section_name.blank?
42
- return true if role[section_name]
43
- role[section_name] = {}
44
- update(the_role: role)
45
- end
46
-
47
- def create_rule section_name, rule_name
48
- return false if rule_name.blank?
49
- return false unless create_section(section_name)
50
- role = to_hash
51
- rule_name = TheRoleParam.process(rule_name)
52
- section_name = TheRoleParam.process(section_name)
53
- return true if role[section_name][rule_name]
54
- role[section_name][rule_name] = false
55
- update(the_role: role)
56
- end
57
-
58
- # R
59
-
60
- def to_hash
61
- begin JSON.load(the_role) rescue {} end
62
- end
63
-
64
- def to_json
65
- the_role
66
- end
67
-
68
- # U
69
-
70
- # source_hash will be reset to false
71
- # except true items from new_role_hash
72
- # all keys will become 'strings'
73
- # look at lib/the_role/hash.rb to find definition of *underscorify_keys* method
74
- def update_role new_role_hash
75
- new_role_hash = new_role_hash.try(:to_hash) || {}
76
- new_role = new_role_hash.underscorify_keys
77
- role = to_hash.underscorify_keys.deep_reset(false)
78
- role.deep_merge! new_role
79
- update(the_role: role)
80
- end
81
-
82
- def rule_on section_name, rule_name
83
- role = to_hash
84
- rule_name = TheRoleParam.process(rule_name)
85
- section_name = TheRoleParam.process(section_name)
86
- return false unless role[section_name]
87
- return false unless role[section_name].key? rule_name
88
- return true if role[section_name][rule_name]
89
- role[section_name][rule_name] = true
90
- update(the_role: role)
91
- end
92
-
93
- def rule_off section_name, rule_name
94
- role = to_hash
95
- rule_name = TheRoleParam.process(rule_name)
96
- section_name = TheRoleParam.process(section_name)
97
- return false unless role[section_name]
98
- return false unless role[section_name].key? rule_name
99
- return true unless role[section_name][rule_name]
100
- role[section_name][rule_name] = false
101
- update(the_role: role)
102
- end
103
-
104
- # D
105
-
106
- def delete_section section_name = nil
107
- return false unless section_name
108
- role = to_hash
109
- section_name = TheRoleParam.process(section_name)
110
- return false if section_name.blank?
111
- return false unless role[section_name]
112
- role.delete section_name
113
- update(the_role: role)
114
- end
115
-
116
- def delete_rule section_name, rule_name
117
- role = to_hash
118
- rule_name = TheRoleParam.process(rule_name)
119
- section_name = TheRoleParam.process(section_name)
120
- return false unless role[section_name]
121
- return false unless role[section_name].key? rule_name
122
- role[section_name].delete rule_name
123
- update(the_role: role)
124
- end
125
- end
@@ -1,35 +0,0 @@
1
- module TheRoleBase
2
- def has_section? section_name
3
- hash = role_hash
4
- section_name = TheRoleParam.process section_name
5
- return true if hash[section_name]
6
- false
7
- end
8
-
9
- def has_role? section_name, rule_name
10
- hash = role_hash
11
- section_name = TheRoleParam.process(section_name)
12
- rule_name = TheRoleParam.process(rule_name)
13
-
14
- return true if hash.try(:[], 'system').try(:[], 'administrator')
15
- return true if hash.try(:[], 'moderator').try(:[], section_name)
16
-
17
- return false unless hash[section_name]
18
- return false unless hash[section_name].key? rule_name
19
- hash[section_name][rule_name]
20
- end
21
-
22
- def any_role? roles_hash = {}
23
- roles_hash.each_pair{|section, action| return true if has_role?(section, action)}
24
- false
25
- end
26
-
27
- def moderator? section_name
28
- section_name = TheRoleParam.process(section_name)
29
- has_role? section_name, 'any_crazy_name'
30
- end
31
-
32
- def admin?
33
- has_role? 'any_crazy_name', 'any_crazy_name'
34
- end
35
- end
@@ -1,54 +0,0 @@
1
- module TheRoleUserModel
2
- extend ActiveSupport::Concern
3
-
4
- include TheRoleBase
5
-
6
- included do
7
- belongs_to :role
8
- before_validation :set_default_role, on: :create
9
- after_save { |user| user.instance_variable_set(:@role_hash, nil) }
10
- end
11
-
12
- module ClassMethods
13
- def with_role name
14
- Role.where(name: name).first.users
15
- end
16
- end
17
-
18
- def role_hash; @role_hash ||= role.try(:to_hash) || {} end
19
-
20
- # FALSE if object is nil
21
- # If object is a USER - check for youself
22
- # Check for owner field - :user_id
23
- # Check for owner _object_ if owner field is not :user_id
24
- def owner? obj
25
- return false unless obj
26
- return true if admin?
27
-
28
- section_name = obj.class.to_s.tableize
29
- return true if moderator?(section_name)
30
-
31
- # obj is User, simple way to define user_id
32
- return id == obj.id if obj.is_a?(User)
33
-
34
- # few ways to define user_id
35
- return id == obj.user_id if obj.respond_to? :user_id
36
- return id == obj[:user_id] if obj[:user_id]
37
- return id == obj[:user][:id] if obj[:user]
38
-
39
- false
40
- end
41
-
42
- private
43
-
44
- def set_default_role
45
- unless role
46
- default_role = Role.where(name: TheRole.config.default_user_role).first
47
- self.role = default_role if default_role
48
- end
49
-
50
- if User.count.zero? && TheRole.config.first_user_should_be_admin
51
- self.role = TheRole.create_admin
52
- end
53
- end
54
- end