can_can_dry 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f376cdbeb98a1137714aa74e1e0f9b84de1ecaf6b1d85f8bbcaa1902d6bc610
4
- data.tar.gz: 4e0272959e2e2ba9aebe2d1544884f293222aadb7385f4a3531522845633fb8b
3
+ metadata.gz: 818f91d0d410669d0dfa281a2f00a16483f0dc7105cb064adb22cd363a5b14a2
4
+ data.tar.gz: 88e252fb9f6b1ff964178cb7a6753cbac2d26a391b469687ac509be66bb20c91
5
5
  SHA512:
6
- metadata.gz: 525ecb1249278e808dab39fa4634a83f3ac78d9ed9aaf9537a1666627ba05f3a84c88af784093a6d0012da43e90adf8e28daf12738b486b5f1365b0a0bc9bc60
7
- data.tar.gz: 55d148c30ab64a8465967f01e2e5d35d50cbc600348b62d5721cc63436631a16966b243ce6a6ca6a49e407e53cf263910ede7494ff973b620975eec9d5746e3a
6
+ metadata.gz: 217d443821182f6e14a33c0eed5a3f2abc0c3389bfd1e386dac8efa97d88086ceca83027f1393c6f796fb6de4846a02a953818b99b44c59f5d4f94ef2b6c6962
7
+ data.tar.gz: '0529f2c5de053a4e73eaa8d4833f32c203179d4a14d63fab9de2c33efbe02f0fd5948adfd7796c0ced2f9973837dd2da2cd86a645a1968e7e1c3b0d7ef83c6d6'
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'bundler/setup'
3
5
  rescue LoadError
data/lib/can_can_dry.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/dependencies'
2
4
  require 'cancancan'
3
5
 
@@ -15,5 +17,5 @@ module CanCanDry
15
17
  require_dependency 'can_can_dry/path_recognizer'
16
18
 
17
19
  require_dependency 'action_view'
18
- ActionView::Base.send :include, CanCanDry::Helpers::CanCanDryHelper
20
+ ActionView::Base.include CanCanDry::Helpers::CanCanDryHelper
19
21
  end
@@ -1,11 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
1
5
  module CanCanDry
2
6
  module AbilityMapping
3
7
  RESOURCES_ACTION_MAPPING = {
4
- read: %w(index show),
5
- create: %w(new create),
6
- update: %w(update edit),
7
- destroy: %w(destroy)
8
- }
8
+ read: %w[index show],
9
+ create: %w[new create],
10
+ update: %w[update edit],
11
+ destroy: %w[destroy]
12
+ }.freeze
9
13
  ALL_ACTION = 'ALL'
10
14
 
11
15
  def mapping
@@ -38,6 +42,7 @@ module CanCanDry
38
42
  if can_args.count == 1
39
43
  raise "\"can_args\" deve ter 0 ou 2 ou mais elementos (can_args.count=#{can_args.count})"
40
44
  end
45
+
41
46
  mapping[controller] ||= {}
42
47
  mapping[controller][action] ||= []
43
48
  mapping[controller][action] << can_args
@@ -45,8 +50,8 @@ module CanCanDry
45
50
 
46
51
  def can_args_by_path(root_path, path, method)
47
52
  can_args_by_path_hash(recognize_path(root_path, path, method))
48
- rescue ActionMappingNotFound => ex
49
- raise PathMappingNotFound.new(path, method, ex)
53
+ rescue ActionMappingNotFound => e
54
+ raise PathMappingNotFound.new(path, method, e)
50
55
  end
51
56
 
52
57
  def can_args_by_path_hash(path_hash)
@@ -59,7 +64,7 @@ module CanCanDry
59
64
  def replace_model_by_record(can_args_args, id)
60
65
  can_args_args.map do |can_args|
61
66
  ca = can_args.dup
62
- ca[1] = ca[1].find_by_id(id) if id && ca[1].respond_to?(:find_by_id)
67
+ ca[1] = ca[1].find_by(id: id) if id && ca[1].respond_to?(:find_by_id)
63
68
  ca
64
69
  end
65
70
  end
@@ -70,15 +75,47 @@ module CanCanDry
70
75
  end
71
76
 
72
77
  def find_can_args_list(controller, action)
73
- controller = ActiveSupport::Inflector.camelize(controller)
74
- raise ActionMappingNotFound.new(controller, action) unless mapping[controller]
75
- return mapping[controller][action] if mapping[controller][action]
76
- return mapping[controller][ALL_ACTION] if mapping[controller][ALL_ACTION]
77
- raise ActionMappingNotFound.new(controller, action)
78
+ FindCanArgsList.new(mapping, controller, action).find
78
79
  end
79
80
 
80
81
  def recognize_path(root_path, path, method)
81
82
  ::CanCanDry::PathRecognizer.recognize(root_path, path, method: method)
82
83
  end
84
+
85
+ class FindCanArgsList
86
+ ALL_ACTION = ::CanCanDry::AbilityMapping::ALL_ACTION
87
+ common_constructor :mapping, :controller, :action
88
+
89
+ set_callback :initialize, :after do
90
+ @controller = ::ActiveSupport::Inflector.camelize(controller)
91
+ end
92
+
93
+ def find
94
+ validate
95
+ find_by_action || find_by_all_action || raise_mapping_not_found
96
+ end
97
+
98
+ private
99
+
100
+ def find_by_action
101
+ return mapping[controller][action] if mapping[controller][action]
102
+ end
103
+
104
+ def find_by_all_action
105
+ return mapping[controller][ALL_ACTION] if mapping[controller][ALL_ACTION]
106
+ end
107
+
108
+ def mapping_has_controller?
109
+ mapping[controller]
110
+ end
111
+
112
+ def raise_mapping_not_found
113
+ raise(ActionMappingNotFound.new(controller, action))
114
+ end
115
+
116
+ def validate
117
+ raise ActionMappingNotFound.new(controller, action) unless mapping_has_controller?
118
+ end
119
+ end
83
120
  end
84
121
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CanCanDry
2
4
  module AbilityMapping
3
- class ActionMappingNotFound < Exception
5
+ class ActionMappingNotFound < RuntimeError
4
6
  def initialize(controller, action)
5
7
  super('Nenhum mapeamento de controle de acesso encontrado ' \
6
8
  "para a action \"#{controller}##{action}\"")
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CanCanDry
2
4
  module AbilityMapping
3
- class PathMappingNotFound < Exception
4
- def initialize(path, method, ex)
5
- super("Falha ao tentar recuperar mapeamento \"#{path}\", método=#{method}: #{ex}")
5
+ class PathMappingNotFound < RuntimeError
6
+ def initialize(path, method, error)
7
+ super("Falha ao tentar recuperar mapeamento \"#{path}\", método=#{method}: #{error}")
6
8
  end
7
9
  end
8
10
  end
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module CanCanDry
3
4
  module AbilityMappingSets
4
5
  module ActiveScaffold
5
6
  ACTIVE_SCAFFOLD_MAPPING = {
6
- read: [:browse, :index, :mark, :render_field, :show, :show_search],
7
- create: [:create, :new],
8
- update: [:add_existing, :edit, :edit_associated, :new_existing, :update, :update_column],
9
- destroy: [:destroy, :destroy_existing]
7
+ read: %i[browse index mark render_field show show_search],
8
+ create: %i[create new],
9
+ update: %i[add_existing edit edit_associated new_existing update update_column],
10
+ destroy: %i[destroy destroy_existing]
10
11
  }.freeze
11
12
 
12
13
  def map_active_scaffold(controller, entity)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CanCanDry
2
4
  module AbilityMappingSets
3
5
  module Devise
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CanCanDry
2
4
  module AbilityMappingSets
3
5
  module DeviseInvitable
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_dependency 'can_can_dry/helpers/can_can_dry_helper'
2
4
 
3
5
  module CanCanDry
@@ -5,14 +7,14 @@ module CanCanDry
5
7
  include CanCanDry::Helpers::CanCanDryHelper
6
8
 
7
9
  def authorize_by_ability_mapping
8
- fail CanCan::AccessDenied, "Falhou ao tentar acessar #{path_hash}" unless
10
+ raise CanCan::AccessDenied, "Falhou ao tentar acessar #{path_hash}" unless
9
11
  can_by_path_hash?(path_hash)
10
12
  end
11
13
 
12
14
  private
13
15
 
14
16
  def path_hash
15
- params.select { |k, _v| %w(controller action id).include?(k) }
17
+ params.select { |k, _v| %w[controller action id].include?(k) }
16
18
  end
17
19
 
18
20
  def ability_mapping
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CanCanDry
2
4
  module Helpers
3
5
  module CanCanDryHelper
@@ -53,6 +55,7 @@ module CanCanDry
53
55
 
54
56
  def assert_can_method
55
57
  return if respond_to?('can?')
58
+
56
59
  singleton_class.include(::CanCanDry::NoControllerCanCanAdditions)
57
60
  end
58
61
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module CanCanDry
3
4
  # A substitute for CanCan::ControllerAdditions when no controller is present.
4
5
  module NoControllerCanCanAdditions
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CanCanDry
2
4
  # Copiado de https://github.com/appirits/awesome_admin_layout
3
5
  # /lib/awesome_admin_layout/recognize_path.rb
@@ -5,7 +7,7 @@ module CanCanDry
5
7
  class << self
6
8
  def recognize(root_path, path, options = {})
7
9
  path = remove_root_path(root_path, path)
8
- return Rails.application.routes.recognize_path(path, options)
10
+ Rails.application.routes.recognize_path(path, options)
9
11
  rescue ActionController::RoutingError
10
12
  Rails::Engine.subclasses.each do |engine|
11
13
  recognized_path = engine_recognize(engine, path, options)
@@ -24,6 +26,7 @@ module CanCanDry
24
26
  def engine_recognize(engine, path, options)
25
27
  engine_path = path_for_engine(engine.instance.class, path)
26
28
  return unless engine_path
29
+
27
30
  begin
28
31
  return engine.instance.routes.recognize_path(engine_path, options)
29
32
  rescue ActionController::RoutingError => e
@@ -35,6 +38,7 @@ module CanCanDry
35
38
  def path_for_engine(engine_class, path)
36
39
  engine_route = Rails.application.routes.routes.find { |r| app_class_for(r) == engine_class }
37
40
  return unless engine_route
41
+
38
42
  path.gsub(/^#{engine_route.path.spec}/, '')
39
43
  end
40
44
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CanCanDry
2
4
  require 'rails'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CanCanDry
2
- VERSION = '0.3.1'
4
+ VERSION = '0.4.0'
3
5
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  namespace :can_can_dry do
3
4
  desc 'Mostra o mapeamento de CanCanDry'
4
5
  task ability_mapping: :environment do
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
 
4
5
  class CanCanDryTest < ActiveSupport::TestCase
@@ -1,14 +1,15 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
 
4
5
  module CanCanDry
5
6
  class PathRecognizerTest < ActiveSupport::TestCase
6
7
  test 'remove_root_path' do
7
- [%w(/path/to/action /prefix/ /prefix/path/to/action),
8
- %w(/path/to/action /prefix /prefix/path/to/action),
9
- %w(/path / /path),
8
+ [%w[/path/to/action /prefix/ /prefix/path/to/action],
9
+ %w[/path/to/action /prefix /prefix/path/to/action],
10
+ %w[/path / /path],
10
11
  ['/path', '', '/path'],
11
- %w(/unknown/path /abc /unknown/path)].each do |s|
12
+ %w[/unknown/path /abc /unknown/path]].each do |s|
12
13
  assert_equal s[0], ::CanCanDry::PathRecognizer.send(:remove_root_path, s[1], s[2]), s
13
14
  end
14
15
  end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  ENV['RAILS_ENV'] = 'test'
3
4
  require 'minitest/autorun'
4
5
  require 'can_can_dry'
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: can_can_dry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-18 00:00:00.000000000 Z
11
+ date: 2020-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
14
+ name: cancancan
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.1
19
+ version: 1.13.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.1
26
+ version: 1.13.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: cancancan
28
+ name: eac_ruby_utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.21'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.21'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: 1.13.0
47
+ version: 4.2.1
34
48
  type: :runtime
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: 1.13.0
54
+ version: 4.2.1
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement