wallaby-core 0.2.0 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +42 -1
  3. data/app/controllers/wallaby/resources_controller.rb +15 -375
  4. data/app/security/ability.rb +1 -1
  5. data/config/locales/wallaby.en.yml +92 -128
  6. data/config/locales/wallaby_class.en.yml +2 -23
  7. data/lib/adaptors/wallaby/custom/default_provider.rb +1 -1
  8. data/lib/adaptors/wallaby/custom/model_decorator.rb +8 -7
  9. data/lib/adaptors/wallaby/custom/model_finder.rb +3 -2
  10. data/lib/adaptors/wallaby/custom/model_pagination_provider.rb +1 -1
  11. data/lib/adaptors/wallaby/custom/model_service_provider.rb +1 -40
  12. data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +30 -24
  13. data/lib/authorizers/wallaby/default_authorization_provider.rb +6 -13
  14. data/lib/authorizers/wallaby/model_authorizer.rb +43 -67
  15. data/lib/authorizers/wallaby/pundit_authorization_provider.rb +21 -30
  16. data/lib/concerns/wallaby/application_concern.rb +110 -0
  17. data/lib/concerns/wallaby/authentication_concern.rb +162 -0
  18. data/lib/concerns/wallaby/authorizable.rb +8 -8
  19. data/lib/concerns/wallaby/baseable.rb +91 -10
  20. data/lib/concerns/wallaby/decoratable.rb +3 -3
  21. data/lib/concerns/wallaby/engineable.rb +1 -1
  22. data/lib/concerns/wallaby/fieldable.rb +4 -4
  23. data/lib/concerns/wallaby/paginatable.rb +3 -3
  24. data/lib/concerns/wallaby/resourcable.rb +4 -35
  25. data/lib/concerns/wallaby/resources_concern.rb +434 -0
  26. data/lib/concerns/wallaby/servicable.rb +4 -4
  27. data/lib/decorators/wallaby/resource_decorator.rb +53 -80
  28. data/lib/errors/wallaby/class_not_found.rb +6 -0
  29. data/lib/errors/wallaby/model_not_found.rb +3 -1
  30. data/lib/errors/wallaby/resource_not_found.rb +1 -1
  31. data/lib/helpers/wallaby/application_helper.rb +6 -0
  32. data/lib/helpers/wallaby/form_helper.rb +2 -3
  33. data/lib/helpers/wallaby/index_helper.rb +2 -2
  34. data/lib/helpers/wallaby/links_helper.rb +5 -5
  35. data/lib/helpers/wallaby/resources_helper.rb +3 -0
  36. data/lib/helpers/wallaby/secure_helper.rb +3 -3
  37. data/lib/helpers/wallaby/styling_helper.rb +17 -3
  38. data/lib/interfaces/wallaby/mode.rb +5 -5
  39. data/lib/interfaces/wallaby/model_authorization_provider.rb +15 -13
  40. data/lib/interfaces/wallaby/model_decorator.rb +15 -3
  41. data/lib/paginators/wallaby/model_paginator.rb +14 -45
  42. data/lib/routes/wallaby/resources_router.rb +1 -1
  43. data/lib/servicers/wallaby/model_servicer.rb +32 -62
  44. data/lib/services/wallaby/map/mode_mapper.rb +14 -14
  45. data/lib/services/wallaby/map/model_class_collector.rb +2 -2
  46. data/lib/services/wallaby/map/model_class_mapper.rb +7 -26
  47. data/lib/services/wallaby/type_renderer.rb +2 -12
  48. data/lib/utils/wallaby/locale.rb +53 -0
  49. data/lib/utils/wallaby/model_utils.rb +4 -3
  50. data/lib/utils/wallaby/module_utils.rb +1 -1
  51. data/lib/utils/wallaby/utils.rb +23 -9
  52. data/lib/wallaby/class_array.rb +75 -0
  53. data/lib/wallaby/class_hash.rb +94 -0
  54. data/lib/wallaby/classifier.rb +29 -0
  55. data/lib/wallaby/configuration/mapping.rb +33 -21
  56. data/lib/wallaby/configuration/metadata.rb +1 -1
  57. data/lib/wallaby/configuration/models.rb +5 -9
  58. data/lib/wallaby/configuration/security.rb +6 -3
  59. data/lib/wallaby/configuration/sorting.rb +1 -1
  60. data/lib/wallaby/configuration.rb +31 -2
  61. data/lib/wallaby/core/version.rb +1 -1
  62. data/lib/wallaby/core.rb +18 -6
  63. data/lib/wallaby/engine.rb +9 -20
  64. data/lib/wallaby/logger.rb +35 -0
  65. data/lib/wallaby/map.rb +20 -17
  66. data/lib/wallaby/preloader.rb +77 -0
  67. metadata +16 -9
  68. data/app/controllers/wallaby/application_controller.rb +0 -84
  69. data/app/controllers/wallaby/secure_controller.rb +0 -81
  70. data/lib/utils/wallaby/preload_utils.rb +0 -44
@@ -3,33 +3,114 @@
3
3
  module Wallaby
4
4
  # Abstract related class methods
5
5
  module Baseable
6
- # Configurable attributes and class methods for marking a class the base class
7
- # and skipping {Wallaby::Map Wallaby mapping}
6
+ SUFFIX = /(Controller|Decorator|Servicer|Authorizer|Paginator)$/.freeze
7
+ ATTR_NAME = 'model_class'
8
+
9
+ # @param class_name [String]
10
+ # @param attr_name [String]
11
+ # @param suffix [String]
12
+ # @param plural [String]
13
+ # @return [Class] found associated class
14
+ # @raise [Wallaby::ClassNotFound] if associated class isn't found
15
+ def self.guess_associated_class_of(class_name, attr_name: ATTR_NAME, suffix: EMPTY_STRING, plural: false)
16
+ base_name = class_name.gsub(SUFFIX, EMPTY_STRING).try(plural ? :pluralize : :singularize) << suffix
17
+ parts = base_name.split(COLONS)
18
+ parts.each_with_index do |_, index|
19
+ begin
20
+ # NOTE: DO NOT try to use const_defined? and const_get EVER.
21
+ # This is Rails, use constantize
22
+ return parts[index..-1].join(COLONS).constantize
23
+ rescue NameError # rubocop:disable Lint/SuppressedException
24
+ end
25
+ end
26
+
27
+ raise ClassNotFound, <<~INSTRUCTION
28
+ The `#{attr_name}` hasn't been provided for Class `#{class_name}` and Wallaby cannot guess it right.
29
+ If `#{class_name}` is supposed to be a base class, add the following line to its class declaration:
30
+
31
+ class #{class_name}
32
+ base_class!
33
+ end
34
+
35
+ Otherwise, please specify the `#{attr_name}` in `#{class_name}`'s declaration as follows:
36
+
37
+ class #{class_name}
38
+ self.#{attr_name} = CorrectClass
39
+ end
40
+ INSTRUCTION
41
+ end
42
+
43
+ # Configurable attributes:
44
+ # 1. mark a class as a base class
45
+ # 2. guess the model class if model class isn't given
8
46
  module ClassMethods
9
47
  # @return [true] if class is a base class
10
48
  # @return [false] if class is not a base class
11
49
  def base_class?
12
- @base_class ||= false
50
+ @base_class == self
13
51
  end
14
52
 
15
- # Mark class a base class
16
- # @return [true]
53
+ # Mark the current class as the base class
17
54
  def base_class!
18
- @base_class = true
55
+ @base_class = self
56
+ end
57
+
58
+ # @!attribute [r] base_class
59
+ # @return [Class] The base class or the one from super class
60
+ def base_class
61
+ @base_class || superclass.try(:base_class)
62
+ end
63
+
64
+ # @!attribute [w] model_class
65
+ def model_class=(model_class)
66
+ raise ArgumentError 'Please provide a Class for `model_class`.' unless model_class.is_a? Class
67
+
68
+ @model_class = model_class
69
+ end
70
+
71
+ # @!attribute [r] model_class
72
+ # @example To configure the model class
73
+ # class Admin::ProductAuthorizer < Admin::ApplicationAuthorizer
74
+ # self.model_class = Product
75
+ # end
76
+ # @example To configure the model class for version below 5.2.0
77
+ # class Admin::ProductAuthorizer < Admin::ApplicationAuthorizer
78
+ # def self.model_class
79
+ # Product
80
+ # end
81
+ # end
82
+ # @return [Class] assigned model class or Wallaby will guess it
83
+ # (see {Wallaby::Baseable.guess_associated_class_of .guess_associated_class_of})
84
+ # @return [nil] if current class is marked as base class
85
+ # @raise [Wallaby::ModelNotFound] if model class isn't found
86
+ # @raise [ArgumentError] if base class is empty
87
+ def model_class
88
+ return if base_class?
89
+
90
+ @model_class ||= Baseable.guess_associated_class_of name
91
+ rescue TypeError
92
+ raise ArgumentError, <<~INSTRUCTION
93
+ Please specify the base class for class `#{name}`
94
+ by marking one of its parents `base_class!`, for example:
95
+
96
+ class ParentClass
97
+ base_class!
98
+ end
99
+ INSTRUCTION
19
100
  end
20
101
 
21
102
  # @!attribute [w] namespace
22
103
  # Used by `model_class`
23
- # @since 5.2.0
104
+ # @since wallaby-5.2.0
24
105
  attr_writer :namespace
25
106
 
26
107
  # @!attribute [r] namespace
27
108
  # @return [String] namespace
28
- # @since 5.2.0
109
+ # @since wallaby-5.2.0
29
110
  def namespace
30
111
  @namespace ||=
31
- ModuleUtils.try_to(superclass, :namespace) \
32
- || name.deconstantize.gsub(/Wallaby(::)?/, EMPTY_STRING).presence
112
+ superclass.try(:namespace) \
113
+ || name.deconstantize.gsub(/Wallaby(::)?/, EMPTY_STRING).presence
33
114
  end
34
115
  end
35
116
  end
@@ -22,7 +22,7 @@ module Wallaby
22
22
  # @return [Class] resource decorator
23
23
  # @raise [ArgumentError] when **resource_decorator** doesn't inherit from **application_decorator**
24
24
  # @see Wallaby::ResourceDecorator
25
- # @since 5.2.0
25
+ # @since wallaby-5.2.0
26
26
  attr_reader :resource_decorator
27
27
 
28
28
  # @!attribute [w] application_decorator
@@ -40,7 +40,7 @@ module Wallaby
40
40
  # @raise [ArgumentError] when **resource_decorator** doesn't inherit from **application_decorator**
41
41
  # @return [Class] application decorator
42
42
  # @see Wallaby::ResourceDecorator
43
- # @since 5.2.0
43
+ # @since wallaby-5.2.0
44
44
  def application_decorator
45
45
  @application_decorator ||= ModuleUtils.try_to superclass, :application_decorator
46
46
  end
@@ -68,7 +68,7 @@ module Wallaby
68
68
  @current_decorator ||=
69
69
  (controller_to_get(:resource_decorator) || \
70
70
  Map.resource_decorator_map(current_model_class, controller_to_get(:application_decorator))).tap do |decorator|
71
- Rails.logger.info %( - Current decorator: #{decorator})
71
+ Logger.debug %(Current decorator: #{decorator}), sourcing: false
72
72
  end
73
73
  end
74
74
 
@@ -17,7 +17,7 @@ module Wallaby
17
17
  # self.engine_name = 'admin_engine'
18
18
  # end
19
19
  # @return [String, Symbol, nil] engine name
20
- # @since 5.2.0
20
+ # @since wallaby-5.2.0
21
21
  def engine_name
22
22
  @engine_name ||= ModuleUtils.try_to superclass, :engine_name
23
23
  end
@@ -18,7 +18,7 @@ module Wallaby
18
18
  # @param field_name [String, Symbol] field name
19
19
  # @return [String, Symbol] type for a given field
20
20
  def type_of(field_name)
21
- validate_presence_of field_name, metadata_of(field_name)[:type]
21
+ ensure_type_is_present field_name, metadata_of(field_name)[:type]
22
22
  end
23
23
 
24
24
  # @param field_name [String, Symbol] field name
@@ -36,7 +36,7 @@ module Wallaby
36
36
  # @param field_name [String, Symbol] field name
37
37
  # @return [String, Symbol] index type for a given field
38
38
  def index_type_of(field_name)
39
- validate_presence_of field_name, index_metadata_of(field_name)[:type]
39
+ ensure_type_is_present field_name, index_metadata_of(field_name)[:type], 'index_'
40
40
  end
41
41
 
42
42
  # @param field_name [String, Symbol] field name
@@ -54,7 +54,7 @@ module Wallaby
54
54
  # @param field_name [String, Symbol] field name
55
55
  # @return [String, Symbol] show type for a given field
56
56
  def show_type_of(field_name)
57
- validate_presence_of field_name, show_metadata_of(field_name)[:type]
57
+ ensure_type_is_present field_name, show_metadata_of(field_name)[:type], 'show_'
58
58
  end
59
59
 
60
60
  # @param field_name [String, Symbol] field name
@@ -72,7 +72,7 @@ module Wallaby
72
72
  # @param field_name [String, Symbol] field name
73
73
  # @return [String, Symbol] form type for a given field
74
74
  def form_type_of(field_name)
75
- validate_presence_of field_name, form_metadata_of(field_name)[:type]
75
+ ensure_type_is_present field_name, form_metadata_of(field_name)[:type], 'form_'
76
76
  end
77
77
  end
78
78
  end
@@ -20,7 +20,7 @@ module Wallaby
20
20
  # @return [Class] model paginator
21
21
  # @raise [ArgumentError] when **model_paginator** doesn't inherit from **application_paginator**
22
22
  # @see Wallaby::ModelPaginator
23
- # @since 5.2.0
23
+ # @since wallaby-5.2.0
24
24
  attr_reader :model_paginator
25
25
 
26
26
  # @!attribute [w] application_paginator
@@ -38,7 +38,7 @@ module Wallaby
38
38
  # @return [Class] application decorator
39
39
  # @raise [ArgumentError] when **model_paginator** doesn't inherit from **application_paginator**
40
40
  # @see Wallaby::ModelPaginator
41
- # @since 5.2.0
41
+ # @since wallaby-5.2.0
42
42
  def application_paginator
43
43
  @application_paginator ||= ModuleUtils.try_to superclass, :application_paginator
44
44
  end
@@ -53,7 +53,7 @@ module Wallaby
53
53
  @current_paginator ||=
54
54
  (controller_to_get(:model_paginator) \
55
55
  || Map.paginator_map(current_model_class, controller_to_get(:application_paginator))).try do |klass|
56
- Rails.logger.info %( - Current paginator: #{klass})
56
+ Logger.debug %(Current paginator: #{klass}), sourcing: false
57
57
  klass.new current_model_class, collection, params
58
58
  end
59
59
  end
@@ -3,41 +3,6 @@
3
3
  module Wallaby
4
4
  # Resources related attributes
5
5
  module Resourcable
6
- # Configurable attribute
7
- module ClassMethods
8
- # @!attribute [w] model_class
9
- attr_writer :model_class
10
-
11
- # @!attribute [r] model_class
12
- # This attribute will be used by the dynamic router to find out which
13
- # controller to dispatch to. For example:
14
- #
15
- # `/admin/products` will be dispatched to the controller that has the
16
- # model class `Product`.
17
- # @return [Class] the model class for controller.
18
- # @example It can be customized as below:
19
- # ```
20
- # self.model_class = Product
21
- # ```
22
- # Or
23
- # ```
24
- # def self.model_class; Product; end
25
- # ```
26
- # @example To set model paginator
27
- # class Admin::ProductionsController < Admin::ApplicationController
28
- # self.model_class = ProductResources
29
- # end
30
- # @return [Class] model paginator
31
- # @raise [ArgumentError] when **model_class** doesn't inherit from **application_paginator**
32
- # @see Wallaby::ModelResources
33
- def model_class
34
- return unless self < ResourcesController
35
- return if base_class? || self == Wallaby.configuration.mapping.resources_controller
36
-
37
- @model_class ||= Map.model_class_map(name.gsub(/(^#{namespace}::)|(Controller$)/, EMPTY_STRING))
38
- end
39
- end
40
-
41
6
  # @return [String] resources name for current request
42
7
  def current_resources_name
43
8
  @current_resources_name ||= params[:resources]
@@ -97,6 +62,8 @@ module Wallaby
97
62
  )
98
63
  end
99
64
 
65
+ alias collection! collection
66
+
100
67
  # @note This is a template method that can be overridden by subclasses.
101
68
  # This is a method to return resource for pages except `index`.
102
69
  #
@@ -145,5 +112,7 @@ module Wallaby
145
112
  &block
146
113
  )
147
114
  end
115
+
116
+ alias resource! resource
148
117
  end
149
118
  end