arpa 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +42 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +138 -0
  8. data/Rakefile +2 -0
  9. data/arpa.gemspec +33 -0
  10. data/lib/arpa.rb +91 -0
  11. data/lib/arpa/additions/resource.rb +29 -0
  12. data/lib/arpa/assets/stylesheets/ar_accordion.scss +80 -0
  13. data/lib/arpa/controllers/profiles_controller.rb +106 -0
  14. data/lib/arpa/controllers/resources_controller.rb +47 -0
  15. data/lib/arpa/controllers/roles_controller.rb +106 -0
  16. data/lib/arpa/data_mappers/action_mapper.rb +13 -0
  17. data/lib/arpa/data_mappers/base.rb +117 -0
  18. data/lib/arpa/data_mappers/profile_mapper.rb +14 -0
  19. data/lib/arpa/data_mappers/resource_mapper.rb +11 -0
  20. data/lib/arpa/data_mappers/role_mapper.rb +14 -0
  21. data/lib/arpa/entities/action.rb +24 -0
  22. data/lib/arpa/entities/permissions.rb +35 -0
  23. data/lib/arpa/entities/profile.rb +22 -0
  24. data/lib/arpa/entities/resource.rb +58 -0
  25. data/lib/arpa/entities/role.rb +28 -0
  26. data/lib/arpa/exceptions/record_invalid.rb +13 -0
  27. data/lib/arpa/repositories/actions/creator.rb +18 -0
  28. data/lib/arpa/repositories/actions/finder.rb +23 -0
  29. data/lib/arpa/repositories/actions/remover.rb +22 -0
  30. data/lib/arpa/repositories/actions/repository_action.rb +15 -0
  31. data/lib/arpa/repositories/base.rb +15 -0
  32. data/lib/arpa/repositories/profiles/creator.rb +18 -0
  33. data/lib/arpa/repositories/profiles/finder.rb +29 -0
  34. data/lib/arpa/repositories/profiles/remover.rb +29 -0
  35. data/lib/arpa/repositories/profiles/repository_profile.rb +12 -0
  36. data/lib/arpa/repositories/profiles/updater.rb +23 -0
  37. data/lib/arpa/repositories/registrator.rb +44 -0
  38. data/lib/arpa/repositories/resources/creator.rb +18 -0
  39. data/lib/arpa/repositories/resources/finder.rb +45 -0
  40. data/lib/arpa/repositories/resources/remover.rb +22 -0
  41. data/lib/arpa/repositories/resources/repository_resource.rb +12 -0
  42. data/lib/arpa/repositories/roles/creator.rb +18 -0
  43. data/lib/arpa/repositories/roles/finder.rb +29 -0
  44. data/lib/arpa/repositories/roles/remover.rb +29 -0
  45. data/lib/arpa/repositories/roles/repository_role.rb +13 -0
  46. data/lib/arpa/repositories/roles/updater.rb +23 -0
  47. data/lib/arpa/services/actions/create/action_creator.rb +50 -0
  48. data/lib/arpa/services/actions/remove/action_remover.rb +24 -0
  49. data/lib/arpa/services/base.rb +41 -0
  50. data/lib/arpa/services/profiles/create/profile_creator.rb +32 -0
  51. data/lib/arpa/services/profiles/profile_manager_creator.rb +22 -0
  52. data/lib/arpa/services/profiles/profile_manager_remover.rb +24 -0
  53. data/lib/arpa/services/profiles/profile_manager_updater.rb +22 -0
  54. data/lib/arpa/services/profiles/remove/profile_remover.rb +25 -0
  55. data/lib/arpa/services/profiles/update/profile_updater.rb +32 -0
  56. data/lib/arpa/services/resources/create/resource_creator.rb +42 -0
  57. data/lib/arpa/services/resources/remove/resource_remover.rb +31 -0
  58. data/lib/arpa/services/resources/resource_manager_creator.rb +50 -0
  59. data/lib/arpa/services/roles/create/role_creator.rb +33 -0
  60. data/lib/arpa/services/roles/remove/role_remover.rb +25 -0
  61. data/lib/arpa/services/roles/role_manager_creator.rb +23 -0
  62. data/lib/arpa/services/roles/role_manager_remover.rb +23 -0
  63. data/lib/arpa/services/roles/role_manager_updater.rb +22 -0
  64. data/lib/arpa/services/roles/update/role_updater.rb +32 -0
  65. data/lib/arpa/services/verifier.rb +24 -0
  66. data/lib/arpa/validators/action_validator.rb +17 -0
  67. data/lib/arpa/validators/profile_validator.rb +19 -0
  68. data/lib/arpa/validators/resource_validator.rb +17 -0
  69. data/lib/arpa/validators/role_validator.rb +20 -0
  70. data/lib/arpa/version.rb +3 -0
  71. data/lib/arpa/views/profiles/_form.html.erb +41 -0
  72. data/lib/arpa/views/profiles/edit.html.erb +6 -0
  73. data/lib/arpa/views/profiles/index.html.erb +29 -0
  74. data/lib/arpa/views/profiles/new.html.erb +5 -0
  75. data/lib/arpa/views/profiles/show.html.erb +37 -0
  76. data/lib/arpa/views/resources/index.html.erb +27 -0
  77. data/lib/arpa/views/resources/show.html.erb +33 -0
  78. data/lib/arpa/views/roles/_form.html.erb +55 -0
  79. data/lib/arpa/views/roles/edit.html.erb +6 -0
  80. data/lib/arpa/views/roles/index.html.erb +29 -0
  81. data/lib/arpa/views/roles/new.html.erb +5 -0
  82. data/lib/arpa/views/roles/show.html.erb +37 -0
  83. data/lib/config/locales/arpa.en.yml +38 -0
  84. data/lib/generators/ar/install_generator.rb +74 -0
  85. data/lib/generators/ar/templates/migration.rb +48 -0
  86. data/spec/factories/repository_actions.rb +19 -0
  87. data/spec/factories/repository_profiles.rb +11 -0
  88. data/spec/factories/repository_resources.rb +15 -0
  89. data/spec/factories/repository_roles.rb +11 -0
  90. data/spec/lib/ar/additions/resource_spec.rb +34 -0
  91. data/spec/lib/ar/data_mappers/action_mapper_spec.rb +46 -0
  92. data/spec/lib/ar/data_mappers/base_spec.rb +47 -0
  93. data/spec/lib/ar/data_mappers/profile_mapper_spec.rb +43 -0
  94. data/spec/lib/ar/data_mappers/resource_mapper_spec.rb +38 -0
  95. data/spec/lib/ar/data_mappers/role_mapper_spec.rb +47 -0
  96. data/spec/lib/ar/entities/action_spec.rb +19 -0
  97. data/spec/lib/ar/entities/permissions_spec.rb +61 -0
  98. data/spec/lib/ar/entities/resource_spec.rb +35 -0
  99. data/spec/lib/ar/repositories/actions/finder_spec.rb +40 -0
  100. data/spec/lib/ar/repositories/base_spec.rb +37 -0
  101. data/spec/lib/ar/repositories/profiles/finder_spec.rb +43 -0
  102. data/spec/lib/ar/repositories/profiles/remover_spec.rb +31 -0
  103. data/spec/lib/ar/repositories/resources/finder_spec.rb +92 -0
  104. data/spec/lib/ar/repositories/resources/remover_spec.rb +38 -0
  105. data/spec/lib/ar/repositories/roles/finder_spec.rb +43 -0
  106. data/spec/lib/ar/repositories/roles/remover_spec.rb +31 -0
  107. data/spec/lib/ar/requests/profiles/create_request_spec.rb +51 -0
  108. data/spec/lib/ar/requests/profiles/remove_request_spec.rb +36 -0
  109. data/spec/lib/ar/requests/resources/create_request_spec.rb +107 -0
  110. data/spec/lib/ar/requests/roles/create_request_spec.rb +40 -0
  111. data/spec/lib/ar/requests/roles/remove_request_spec.rb +23 -0
  112. data/spec/lib/ar/requests/roles/update_request_spec.rb +50 -0
  113. data/spec/lib/ar/services/actions/create/action_creator_spec.rb +88 -0
  114. data/spec/lib/ar/services/actions/remove/action_remover_spec.rb +36 -0
  115. data/spec/lib/ar/services/base_spec.rb +94 -0
  116. data/spec/lib/ar/services/profiles/create/profile_creator_spec.rb +68 -0
  117. data/spec/lib/ar/services/profiles/profile_manager_creator_spec.rb +31 -0
  118. data/spec/lib/ar/services/profiles/profile_manager_remover_spec.rb +44 -0
  119. data/spec/lib/ar/services/profiles/profile_manager_updater_spec.rb +31 -0
  120. data/spec/lib/ar/services/profiles/remove/profile_remover_spec.rb +45 -0
  121. data/spec/lib/ar/services/profiles/update/profile_updater_spec.rb +68 -0
  122. data/spec/lib/ar/services/resources/create/resource_creator_spec.rb +87 -0
  123. data/spec/lib/ar/services/resources/remove/resource_remover_spec.rb +50 -0
  124. data/spec/lib/ar/services/resources/resource_manager_creator_spec.rb +71 -0
  125. data/spec/lib/ar/services/roles/create/role_creator_spec.rb +68 -0
  126. data/spec/lib/ar/services/roles/remove/role_remover_spec.rb +50 -0
  127. data/spec/lib/ar/services/roles/role_manager_creator_spec.rb +31 -0
  128. data/spec/lib/ar/services/roles/role_manager_remover_spec.rb +32 -0
  129. data/spec/lib/ar/services/roles/role_manager_updater_spec.rb +31 -0
  130. data/spec/lib/ar/services/roles/update/role_updater_spec.rb +68 -0
  131. data/spec/lib/ar/services/verifier_spec.rb +44 -0
  132. data/spec/lib/ar/validators/action_validator_spec.rb +15 -0
  133. data/spec/lib/ar/validators/profile_validator_spec.rb +16 -0
  134. data/spec/lib/ar/validators/resource_validator_spec.rb +15 -0
  135. data/spec/lib/ar/validators/role_validator_spec.rb +16 -0
  136. data/spec/spec_helper.rb +33 -0
  137. data/spec/support/repositories/test_repository.rb +2 -0
  138. data/spec/support/schema.rb +58 -0
  139. metadata +380 -0
@@ -0,0 +1,35 @@
1
+ module Arpa
2
+ module Entities
3
+ class Permissions
4
+
5
+ attr_reader :permissions
6
+
7
+ def initialize(resources)
8
+ @resources = resources
9
+ @permissions = Array.new
10
+ build_permissions
11
+ end
12
+
13
+ def has_permission?(resource_name, action_name)
14
+ hash_permission = build_hash_permission(resource_name, action_name)
15
+ permissions.include?(hash_permission)
16
+ end
17
+
18
+ private
19
+ def build_permissions
20
+ @resources.each do |resource|
21
+ resource.actions.collect do |action|
22
+ hash_permission = build_hash_permission(resource.name, action.name)
23
+ @permissions << hash_permission unless permissions.include?(hash_permission)
24
+ end
25
+ end
26
+ end
27
+
28
+ def build_hash_permission(resource_name, action_name)
29
+ {'resource' => resource_name, 'action' => action_name }
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,22 @@
1
+ module Arpa
2
+ module Entities
3
+ class Profile
4
+
5
+ attr_reader :id, :name, :description, :role_ids, :roles, :created_at, :updated_at, :removed
6
+
7
+ def initialize(attrs = {})
8
+ attrs = attrs.with_indifferent_access
9
+
10
+ @id = attrs[:id]
11
+ @name = attrs[:name]
12
+ @description = attrs[:name]
13
+ @role_ids = attrs[:role_ids] || []
14
+ @roles = attrs[:roles] || []
15
+ @created_at = attrs[:created_at]
16
+ @updated_at = attrs[:updated_at]
17
+ @removed = attrs[:removed] || false
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,58 @@
1
+ module Arpa
2
+ module Entities
3
+ class Resource
4
+
5
+ attr_reader :id, :full_name, :name, :created_at, :updated_at, :actions
6
+
7
+ def initialize(attrs = {})
8
+ attrs = attrs.with_indifferent_access
9
+
10
+ @id = attrs[:id]
11
+ @full_name = attrs[:full_name]
12
+ @name = attrs[:name]
13
+ @created_at = attrs[:created_at]
14
+ @updated_at = attrs[:updated_at]
15
+ @actions = attrs[:actions] || []
16
+ end
17
+
18
+ def build_correct_name
19
+ name = remove_word(full_name)
20
+ @name = adjust_name(name)
21
+ end
22
+
23
+ private
24
+
25
+ def adjust_name(name)
26
+ parts_of_name = name.split '::'
27
+ refactored_name = String.new
28
+
29
+ parts_of_name.each_with_index do |part, index|
30
+ refactored_name << '/' if index > 0
31
+ refactored_name << change_to_snake_case(part)
32
+ end
33
+
34
+ refactored_name
35
+ end
36
+
37
+ def remove_word(word, word_to_delete = 'Controller')
38
+ word = "#{word}del" if word.include?(word_to_delete)
39
+ word.slice!("#{word_to_delete}del")
40
+ word
41
+ end
42
+
43
+ def change_to_snake_case(name)
44
+ parts_of_name = name.split(/(?=[A-Z])/) # Split at CamelCase
45
+ refactored_name = String.new
46
+
47
+ parts_of_name.each_with_index do |part, index|
48
+ part.downcase!
49
+ refactored_name << '_' if index > 0
50
+ refactored_name << part
51
+ end
52
+
53
+ refactored_name
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,28 @@
1
+ module Arpa
2
+ module Entities
3
+ class Role
4
+
5
+ attr_reader :id, :name, :description, :created_at, :updated_at, :removed,
6
+ :action_ids, :actions, :profiles
7
+
8
+ def initialize(attrs = {})
9
+ attrs = attrs.with_indifferent_access
10
+
11
+ @id = attrs[:id]
12
+ @name = attrs[:name]
13
+ @description = attrs[:description]
14
+ @action_ids = attrs[:action_ids] || []
15
+ @actions = attrs[:actions] || []
16
+ @profiles = attrs[:profiles] || []
17
+ @created_at = attrs[:created_at]
18
+ @updated_at = attrs[:updated_at]
19
+ @removed = attrs[:removed] || false
20
+ end
21
+
22
+ def has_profile?
23
+ profiles.present?
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module Arpa
2
+ module Exceptions
3
+ class RecordInvalid < StandardError
4
+ attr_reader :errors
5
+
6
+ def initialize(args)
7
+ super(args[:message])
8
+ @errors = args[:errors]
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Actions
4
+ class Creator
5
+ include Arpa::Repositories::Registrator
6
+
7
+ def mapper_instance
8
+ Arpa::DataMappers::ActionMapper.instance
9
+ end
10
+
11
+ def repository_class
12
+ RepositoryAction
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Actions
4
+ class Finder
5
+ include Arpa::Repositories::Base
6
+
7
+ def by_name_and_resource(name, resource_id)
8
+ record = repository_class.where(name: name, repository_resource_id: resource_id).first
9
+ mapper_instance.map_to_entity(record) if record
10
+ end
11
+
12
+ def mapper_instance
13
+ Arpa::DataMappers::ActionMapper.instance
14
+ end
15
+
16
+ def repository_class
17
+ RepositoryAction
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Actions
4
+ class Remover
5
+ include Arpa::Repositories::Base
6
+
7
+ def destroy(entity)
8
+ repository_class.destroy(entity.id)
9
+ end
10
+
11
+ def mapper_instance
12
+ Arpa::DataMappers::ActionMapper.instance
13
+ end
14
+
15
+ def repository_class
16
+ RepositoryAction
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Actions
4
+ class RepositoryAction < ActiveRecord::Base
5
+ default_scope {order(name: :asc)}
6
+
7
+ alias_attribute :resource_id, :repository_resource_id
8
+ belongs_to :resource, foreign_key: 'repository_resource_id', class_name: 'Arpa::Repositories::Resources::RepositoryResource'
9
+
10
+ has_and_belongs_to_many :roles, class_name: 'Arpa::Repositories::Roles::RepositoryRole'
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Base
4
+
5
+ def mapper_instance
6
+ raise NotImplementedError, "This #{self.class} cannot respond :mapper_instance"
7
+ end
8
+
9
+ def repository_class
10
+ raise NotImplementedError, "This #{self.class} cannot respond :repository_class"
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Profiles
4
+ class Creator
5
+ include Arpa::Repositories::Registrator
6
+
7
+ def mapper_instance
8
+ Arpa::DataMappers::ProfileMapper.instance
9
+ end
10
+
11
+ def repository_class
12
+ RepositoryProfile
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Profiles
4
+ class Finder
5
+ include Arpa::Repositories::Base
6
+
7
+ def find(id)
8
+ record = repository_class.find(id)
9
+ mapper_instance.map_to_entity(record)
10
+ end
11
+
12
+ def all
13
+ repository_class.all.collect do |record|
14
+ mapper_instance.map_to_entity(record)
15
+ end
16
+ end
17
+
18
+ def mapper_instance
19
+ Arpa::DataMappers::ProfileMapper.instance
20
+ end
21
+
22
+ def repository_class
23
+ RepositoryProfile
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Profiles
4
+ class Remover
5
+ include Arpa::Repositories::Base
6
+
7
+ def destroy(entity)
8
+ repository_class.destroy(entity.id)
9
+ end
10
+
11
+ def disable(entity)
12
+ record = mapper_instance.map_to_record(entity)
13
+ repository_class.update(record.id, removed: true)
14
+ record.reload
15
+ mapper_instance.map_to_entity(record)
16
+ end
17
+
18
+ def mapper_instance
19
+ Arpa::DataMappers::ProfileMapper.instance
20
+ end
21
+
22
+ def repository_class
23
+ RepositoryProfile
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Profiles
4
+ class RepositoryProfile < ActiveRecord::Base
5
+ default_scope {where(removed: false).order(name: :asc)}
6
+
7
+ has_and_belongs_to_many :roles, class_name: 'Arpa::Repositories::Roles::RepositoryRole'
8
+
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Profiles
4
+ class Updater
5
+ include Arpa::Repositories::Registrator
6
+
7
+ def post_update(entity, record)
8
+ record.role_ids = entity.role_ids
9
+ record.save!
10
+ end
11
+
12
+ def mapper_instance
13
+ Arpa::DataMappers::ProfileMapper.instance
14
+ end
15
+
16
+ def repository_class
17
+ RepositoryProfile
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,44 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Registrator
4
+ include Base
5
+
6
+ def create(entity)
7
+ save(entity) do |record|
8
+ record.save!
9
+ end
10
+ end
11
+
12
+ def update(entity)
13
+ save(entity) do |record|
14
+ record.updated_at = Time.new.utc
15
+ attributes = record.attributes.except('id', 'created_at')
16
+
17
+ repository_class.update(entity.id, attributes)
18
+
19
+ record.reload
20
+ record.reload
21
+ post_update(entity, record)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def save(entity, &block)
28
+ record = mapper_instance.map_to_record( entity)
29
+ begin
30
+ block.call(record)
31
+ rescue ActiveRecord::RecordInvalid => invalid
32
+ raise Arpa::Exceptions::RecordInvalid.new(message: invalid.message, errors: invalid.record.errors)
33
+ end
34
+
35
+ mapper_instance.map_to_entity(record)
36
+ end
37
+
38
+ def post_update(entity, record)
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,18 @@
1
+ module Arpa
2
+ module Repositories
3
+ module Resources
4
+ class Creator
5
+ include Arpa::Repositories::Registrator
6
+
7
+ def mapper_instance
8
+ Arpa::DataMappers::ResourceMapper.instance
9
+ end
10
+
11
+ def repository_class
12
+ RepositoryResource
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end