wallaby-core 0.2.11 → 0.3.0.beta1

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 +4 -4
  2. data/app/controllers/wallaby/resources_controller.rb +20 -7
  3. data/app/security/ability.rb +1 -1
  4. data/config/routes.rb +21 -14
  5. data/lib/adaptors/wallaby/custom/default_provider.rb +1 -1
  6. data/lib/adaptors/wallaby/custom/model_decorator.rb +5 -17
  7. data/lib/adaptors/wallaby/custom/model_finder.rb +3 -3
  8. data/lib/adaptors/wallaby/custom/model_pagination_provider.rb +1 -1
  9. data/lib/adaptors/wallaby/custom/model_service_provider.rb +1 -1
  10. data/lib/authorizers/wallaby/cancancan_authorization_provider.rb +12 -5
  11. data/lib/authorizers/wallaby/default_authorization_provider.rb +10 -1
  12. data/lib/authorizers/wallaby/model_authorizer.rb +41 -16
  13. data/lib/authorizers/wallaby/pundit_authorization_provider.rb +22 -8
  14. data/lib/concerns/wallaby/application_concern.rb +41 -71
  15. data/lib/concerns/wallaby/authentication_concern.rb +29 -127
  16. data/lib/concerns/wallaby/authorizable.rb +14 -57
  17. data/lib/concerns/wallaby/baseable.rb +24 -57
  18. data/lib/concerns/wallaby/configurable.rb +416 -0
  19. data/lib/concerns/wallaby/decoratable.rb +24 -60
  20. data/lib/concerns/wallaby/engineable.rb +29 -46
  21. data/lib/concerns/wallaby/fieldable.rb +45 -56
  22. data/lib/concerns/wallaby/paginatable.rb +20 -51
  23. data/lib/concerns/wallaby/prefixable.rb +24 -4
  24. data/lib/concerns/wallaby/resourcable.rb +130 -72
  25. data/lib/concerns/wallaby/resources_concern.rb +205 -305
  26. data/lib/concerns/wallaby/servicable.rb +8 -48
  27. data/lib/concerns/wallaby/urlable.rb +69 -0
  28. data/lib/decorators/wallaby/resource_decorator.rb +72 -34
  29. data/lib/errors/wallaby/class_not_found.rb +1 -2
  30. data/lib/errors/wallaby/forbidden.rb +1 -2
  31. data/lib/errors/wallaby/general_error.rb +1 -1
  32. data/lib/errors/wallaby/invalid_error.rb +1 -2
  33. data/lib/errors/wallaby/method_removed.rb +5 -0
  34. data/lib/errors/wallaby/model_not_found.rb +1 -2
  35. data/lib/errors/wallaby/not_authenticated.rb +1 -2
  36. data/lib/errors/wallaby/not_found.rb +1 -2
  37. data/lib/errors/wallaby/not_implemented.rb +1 -2
  38. data/lib/errors/wallaby/resource_not_found.rb +1 -2
  39. data/lib/errors/wallaby/unprocessable_entity.rb +1 -2
  40. data/lib/fields/wallaby/all_fields.rb +63 -0
  41. data/lib/forms/wallaby/form_builder.rb +2 -2
  42. data/lib/generators/wallaby/engine/application_generator.rb +33 -0
  43. data/lib/generators/wallaby/engine/authorizer/USAGE +20 -0
  44. data/lib/generators/wallaby/engine/authorizer/authorizer_generator.rb +19 -0
  45. data/lib/generators/wallaby/engine/authorizer/templates/authorizer.rb.erb +35 -0
  46. data/lib/generators/wallaby/engine/controller/USAGE +20 -0
  47. data/lib/generators/wallaby/engine/controller/controller_generator.rb +23 -0
  48. data/lib/generators/wallaby/engine/controller/templates/controller.rb.erb +130 -0
  49. data/lib/generators/wallaby/engine/decorator/USAGE +20 -0
  50. data/lib/generators/wallaby/engine/decorator/decorator_generator.rb +19 -0
  51. data/lib/generators/wallaby/engine/decorator/templates/decorator.rb.erb +5 -0
  52. data/lib/generators/wallaby/engine/install/USAGE +19 -0
  53. data/lib/generators/wallaby/engine/install/install_generator.rb +91 -0
  54. data/lib/generators/wallaby/engine/install/templates/application_authorizer.rb.erb +37 -0
  55. data/lib/generators/wallaby/engine/install/templates/application_controller.rb.erb +173 -0
  56. data/lib/generators/wallaby/engine/install/templates/application_decorator.rb.erb +7 -0
  57. data/lib/generators/wallaby/engine/install/templates/application_paginator.rb.erb +27 -0
  58. data/lib/generators/wallaby/engine/install/templates/application_servicer.rb.erb +47 -0
  59. data/lib/generators/wallaby/engine/install/templates/initializer.rb.erb +16 -0
  60. data/lib/generators/wallaby/engine/paginator/USAGE +20 -0
  61. data/lib/generators/wallaby/engine/paginator/paginator_generator.rb +19 -0
  62. data/lib/generators/wallaby/engine/paginator/templates/paginator.rb.erb +25 -0
  63. data/lib/generators/wallaby/engine/servicer/USAGE +20 -0
  64. data/lib/generators/wallaby/engine/servicer/servicer_generator.rb +19 -0
  65. data/lib/generators/wallaby/engine/servicer/templates/servicer.rb.erb +45 -0
  66. data/lib/helpers/wallaby/application_helper.rb +10 -59
  67. data/lib/helpers/wallaby/base_helper.rb +11 -11
  68. data/lib/helpers/wallaby/configuration_helper.rb +36 -4
  69. data/lib/helpers/wallaby/form_helper.rb +1 -1
  70. data/lib/helpers/wallaby/index_helper.rb +19 -9
  71. data/lib/helpers/wallaby/links_helper.rb +13 -80
  72. data/lib/helpers/wallaby/resources_helper.rb +39 -7
  73. data/lib/helpers/wallaby/secure_helper.rb +20 -19
  74. data/lib/interfaces/wallaby/mode.rb +8 -8
  75. data/lib/interfaces/wallaby/model_authorization_provider.rb +23 -22
  76. data/lib/interfaces/wallaby/model_decorator.rb +36 -48
  77. data/lib/interfaces/wallaby/model_finder.rb +3 -3
  78. data/lib/interfaces/wallaby/model_pagination_provider.rb +2 -6
  79. data/lib/interfaces/wallaby/model_service_provider.rb +4 -4
  80. data/lib/paginators/wallaby/model_paginator.rb +1 -1
  81. data/lib/responders/wallaby/json_api_responder.rb +10 -5
  82. data/lib/responders/wallaby/resources_responder.rb +7 -2
  83. data/lib/routes/wallaby/engines/base_route.rb +78 -0
  84. data/lib/routes/wallaby/engines/custom_app_route.rb +92 -0
  85. data/lib/routes/wallaby/engines/engine_route.rb +77 -0
  86. data/lib/routes/wallaby/resources_router.rb +100 -45
  87. data/lib/servicers/wallaby/model_servicer.rb +13 -13
  88. data/lib/services/wallaby/authorizer_finder.rb +23 -0
  89. data/lib/services/wallaby/class_finder.rb +42 -0
  90. data/lib/services/wallaby/controller_finder.rb +29 -0
  91. data/lib/services/wallaby/decorator_finder.rb +34 -0
  92. data/lib/services/wallaby/default_models_excluder.rb +45 -0
  93. data/lib/services/wallaby/engine_name_finder.rb +14 -11
  94. data/lib/services/wallaby/engine_url_for.rb +82 -37
  95. data/lib/services/wallaby/fields_regulator.rb +34 -0
  96. data/lib/services/wallaby/map/mode_mapper.rb +4 -4
  97. data/lib/services/wallaby/map/model_class_mapper.rb +1 -1
  98. data/lib/services/wallaby/model_class_filter.rb +29 -0
  99. data/lib/services/wallaby/paginator_finder.rb +24 -0
  100. data/lib/services/wallaby/prefixes_builder.rb +49 -8
  101. data/lib/services/wallaby/servicer_finder.rb +31 -0
  102. data/lib/services/wallaby/sorting/hash_builder.rb +9 -0
  103. data/lib/services/wallaby/sorting/link_builder.rb +7 -10
  104. data/lib/services/wallaby/sorting/next_builder.rb +1 -12
  105. data/lib/services/wallaby/sorting/single_builder.rb +1 -1
  106. data/lib/support/action_dispatch/routing/mapper.rb +29 -4
  107. data/lib/utils/wallaby/field_utils.rb +9 -8
  108. data/lib/utils/wallaby/inflector.rb +94 -0
  109. data/lib/utils/wallaby/locale.rb +2 -2
  110. data/lib/utils/wallaby/module_utils.rb +3 -10
  111. data/lib/utils/wallaby/utils.rb +21 -14
  112. data/lib/wallaby/class_array.rb +18 -13
  113. data/lib/wallaby/class_hash.rb +16 -14
  114. data/lib/wallaby/classifier.rb +4 -2
  115. data/lib/wallaby/configuration/features.rb +8 -2
  116. data/lib/wallaby/configuration/mapping.rb +66 -112
  117. data/lib/wallaby/configuration/metadata.rb +15 -12
  118. data/lib/wallaby/configuration/models.rb +27 -25
  119. data/lib/wallaby/configuration/pagination.rb +15 -19
  120. data/lib/wallaby/configuration/security.rb +88 -80
  121. data/lib/wallaby/configuration/sorting.rb +15 -17
  122. data/lib/wallaby/configuration.rb +58 -23
  123. data/lib/wallaby/constants.rb +21 -13
  124. data/lib/wallaby/core/version.rb +1 -1
  125. data/lib/wallaby/core.rb +34 -10
  126. data/lib/wallaby/deprecator.rb +81 -0
  127. data/lib/wallaby/engine.rb +2 -19
  128. data/lib/wallaby/guesser.rb +45 -0
  129. data/lib/wallaby/logger.rb +35 -13
  130. data/lib/wallaby/map.rb +11 -88
  131. data/lib/wallaby/preloader.rb +9 -31
  132. metadata +120 -15
  133. data/config/locales/wallaby_class.en.yml +0 -9
  134. data/lib/concerns/wallaby/defaultable.rb +0 -38
  135. data/lib/concerns/wallaby/shared_helpers.rb +0 -22
  136. data/lib/services/wallaby/map/model_class_collector.rb +0 -49
  137. data/lib/services/wallaby/type_renderer.rb +0 -40
  138. data/lib/utils/wallaby/model_utils.rb +0 -52
  139. data/lib/utils/wallaby/test_utils.rb +0 -34
@@ -3,57 +3,17 @@
3
3
  module Wallaby
4
4
  # Servicer related attributes
5
5
  module Servicable
6
- # Configurable attribute
7
- module ClassMethods
8
- # @!attribute [w] model_servicer
9
- def model_servicer=(model_servicer)
10
- ModuleUtils.inheritance_check model_servicer, application_servicer
11
- @model_servicer = model_servicer
12
- end
13
-
14
- # @!attribute [r] model_servicer
15
- # If Wallaby doesn't get it right, please specify the **model_servicer**.
16
- # @example To set model servicer
17
- # class Admin::ProductionsController < Admin::ApplicationController
18
- # self.model_servicer = ProductServicer
19
- # end
20
- # @return [Class] model servicer
21
- # @raise [ArgumentError] when **model_servicer** doesn't inherit from **application_servicer**
22
- # @see Wallaby::ModelServicer
23
- # @since wallaby-5.2.0
24
- attr_reader :model_servicer
25
-
26
- # @!attribute [w] application_servicer
27
- def application_servicer=(application_servicer)
28
- ModuleUtils.inheritance_check model_servicer, application_servicer
29
- @application_servicer = application_servicer
30
- end
31
-
32
- # @!attribute [r] application_servicer
33
- # The **application_servicer** is as the base class of {#model_servicer}.
34
- # @example To set application decorator:
35
- # class Admin::ApplicationController < Wallaby::ResourcesController
36
- # self.application_servicer = AnotherApplicationServicer
37
- # end
38
- # @return [Class] application decorator
39
- # @raise [ArgumentError] when **model_servicer** doesn't inherit from **application_servicer**
40
- # @see Wallaby::ModelServicer
41
- # @since wallaby-5.2.0
42
- def application_servicer
43
- @application_servicer ||= ModuleUtils.try_to superclass, :application_servicer
44
- end
45
- end
46
-
47
- # Model servicer for current modal class. It comes from:
48
- #
49
- # - controller configuration {Wallaby::Servicable::ClassMethods#model_servicer .model_servicer}
50
- # - a generic servicer based on {Wallaby::Servicable::ClassMethods#application_servicer .application_servicer}
51
- # @return [Wallaby::ModelServicer] model servicer
6
+ # Model servicer for current modal class.
7
+ # @return [ModelServicer] model servicer
8
+ # @see ServicerFinder#execute
52
9
  # @since wallaby-5.2.0
53
10
  def current_servicer
54
11
  @current_servicer ||=
55
- (controller_to_get(:model_servicer) \
56
- || Map.servicer_map(current_model_class, controller_to_get(:application_servicer))).try do |klass|
12
+ ServicerFinder.new(
13
+ script_name: script_name,
14
+ model_class: current_model_class,
15
+ current_controller_class: wallaby_controller
16
+ ).execute.try do |klass|
57
17
  Logger.debug %(Current servicer: #{klass}), sourcing: false
58
18
  klass.new current_model_class, current_authorizer, current_model_decorator
59
19
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ # All URL helpers for {Engine}
5
+ module Urlable
6
+ # Override original method to handle URL generation **when Wallaby is mounted as Rails Engine**.
7
+ # @param params [String, Hash, ActionController::Parameters, nil]
8
+ # @param options [Hash] options only used by {EngineUrlFor}
9
+ # @return [String] URL string
10
+ # @see EngineUrlFor.execute
11
+ # @see https://api.rubyonrails.org/classes/ActionView/RoutingUrlFor.html#method-i-url_for
12
+ # ActionView::RoutingUrlFor#url_for
13
+ def url_for(params = nil, options = {})
14
+ (!options[:super] &&
15
+ EngineUrlFor.execute(context: self, params: params, options: options)) ||
16
+ super(params)
17
+ end
18
+
19
+ # Generate the resourceful index path for given model class.
20
+ # @param model_class [Class]
21
+ # @param url_params [Hash]
22
+ # @return [String] index page path
23
+ def index_path(model_class, url_params: {})
24
+ with_query = url_params.delete :with_query
25
+ url_for(
26
+ { action: :index }.merge(url_params),
27
+ { model_class: model_class, with_query: with_query }
28
+ )
29
+ end
30
+
31
+ # Generate the resourceful new path for given model class.
32
+ # @param model_class [Class]
33
+ # @param url_params [Hash]
34
+ # @return [String] new page path
35
+ def new_path(model_class, url_params: {})
36
+ with_query = url_params.delete :with_query
37
+ url_for(
38
+ { action: :new }.merge(url_params),
39
+ { model_class: model_class, with_query: with_query }
40
+ )
41
+ end
42
+
43
+ # Generate the resourceful show path for given resource.
44
+ # @param resource [Object]
45
+ # @param url_params [Hash]
46
+ # @return [String] show page path
47
+ def show_path(resource, url_params: {})
48
+ decorated = decorate resource
49
+ with_query = url_params.delete :with_query
50
+ url_for(
51
+ ParamsUtils.presence(action: :show, id: decorated.try(:primary_key_value)).merge(url_params),
52
+ { model_class: decorated.model_class, with_query: with_query }
53
+ )
54
+ end
55
+
56
+ # Generate the resourceful edit path for given resource.
57
+ # @param resource [Object]
58
+ # @param url_params [Hash]
59
+ # @return [String] edit page path
60
+ def edit_path(resource, url_params: {})
61
+ decorated = decorate resource
62
+ with_query = url_params.delete :with_query
63
+ url_for(
64
+ { action: :edit, id: decorated.primary_key_value }.merge(url_params),
65
+ { model_class: decorated.model_class, with_query: with_query }
66
+ )
67
+ end
68
+ end
69
+ end
@@ -4,19 +4,19 @@ module Wallaby
4
4
  # Decorator base class. It's designed to be used as the decorator (AKA presenter/view object)
5
5
  # for the associated model instance (which means it should be used in the views only).
6
6
  #
7
- # And it holds the following metadata information for associated model class:
7
+ # And it holds the following field metadata information for associated model class:
8
8
  #
9
- # - {#fields}
10
- # - {#field_names}
11
- # - {#index_fields}
12
- # - {#index_field_names}
13
- # - {#show_fields}
14
- # - {#show_field_names}
15
- # - {#form_fields}
16
- # - {#form_field_names}
9
+ # - {#fields} - all other `*_fields` methods origin from it. and it's frozen.
10
+ # - {#field_names} - all other `*_field_names` methods origin from it. and it's frozen.
11
+ # - {#index_fields} - field metdata used on index page
12
+ # - {#index_field_names} - field name list used on index page
13
+ # - {#show_fields} - field metdata used on show page
14
+ # - {#show_field_names} - field name list used on show page
15
+ # - {#form_fields} - field metdata used on new/create/edit/update page
16
+ # - {#form_field_names} - field name list used on new/create/edit/update page
17
17
  #
18
18
  # For better practice, please create an application decorator class (see example)
19
- # to better control the functions shared between different resource decorators.
19
+ # to better control the functions shared between different decorators.
20
20
  # @example Create an application class for Admin Interface usage
21
21
  # class Admin::ApplicationDecorator < Wallaby::ResourceDecorator
22
22
  # base_class!
@@ -50,30 +50,66 @@ module Wallaby
50
50
  # (see Wallaby::ModelDecorator#form_field_names)
51
51
 
52
52
  DELEGATE_METHODS =
53
- ModelDecorator.public_instance_methods(false) + Fieldable.public_instance_methods(false) - %i(model_class)
53
+ ModelDecorator.public_instance_methods(false) + Fieldable.public_instance_methods(false) - %i[model_class]
54
54
 
55
55
  class << self
56
+ # @!attribute fields
57
+ # (see Wallaby::ModelDecorator#fields)
58
+
59
+ # @!attribute field_names
60
+ # (see Wallaby::ModelDecorator#field_names)
61
+
62
+ # @!attribute index_fields
63
+ # (see Wallaby::ModelDecorator#index_fields)
64
+
65
+ # @!attribute index_field_names
66
+ # (see Wallaby::ModelDecorator#index_field_names)
67
+
68
+ # @!attribute show_fields
69
+ # (see Wallaby::ModelDecorator#show_fields)
70
+
71
+ # @!attribute show_field_names
72
+ # (see Wallaby::ModelDecorator#show_field_names)
73
+
74
+ # @!attribute form_fields
75
+ # (see Wallaby::ModelDecorator#form_fields)
76
+
77
+ # @!attribute form_field_names
78
+ # (see Wallaby::ModelDecorator#form_field_names)
79
+
56
80
  delegate(*DELEGATE_METHODS, to: :model_decorator, allow_nil: true)
57
81
 
58
- # Return associated model decorator. It is the instance that pull out all the metadata
59
- # information for the associated model.
82
+ # Return associated {ModelDecorator model decorator}. It is the instance that pull out all the metadata
83
+ # information from the associated model.
60
84
  # @param model_class [Class]
61
- # @return [Wallaby::ModelDecorator]
85
+ # @return [ModelDecorator]
62
86
  # @return [nil] if itself is a base class or the given model_class is blank
63
87
  def model_decorator(model_class = self.model_class)
64
88
  return if model_class.blank?
65
89
 
66
- Map.model_decorator_map model_class, base_class
90
+ Map.model_decorator_map(model_class, base_class)
67
91
  end
68
92
 
69
93
  # @!attribute [w] h
70
94
  attr_writer :h
71
95
 
72
96
  # @!attribute [r] h
73
- # @return [ActionView::Base]
74
- # {Wallaby::Configuration::Mapping#resources_controller resources controller}'s helpers
97
+ # @return [ActionView::Base] {ResourcesController}'s helpers
75
98
  def h
76
- @h ||= Wallaby.configuration.mapping.resources_controller.helpers
99
+ @h ||= superclass.try(:h) || ResourcesController.helpers
100
+ end
101
+
102
+ # Delegate missing method to {.model_decorator}
103
+ def method_missing(method_id, *args, &block)
104
+ return if ModelDecorator::MISSING_METHODS_RELATED_TO_FIELDS.match?(method_id.to_s) && model_decorator.blank?
105
+ return super unless model_decorator.try(:respond_to?, method_id)
106
+
107
+ model_decorator.try(method_id, *args, &block)
108
+ end
109
+
110
+ # Delegate missing method check to {.model_decorator}
111
+ def respond_to_missing?(method_id, _include_private)
112
+ model_decorator.try(:respond_to?, method_id) || super
77
113
  end
78
114
  end
79
115
 
@@ -82,18 +118,17 @@ module Wallaby
82
118
  attr_reader :resource
83
119
 
84
120
  # @!attribute [r] model_decorator
85
- # @return [Wallaby::ModelDecorator]
121
+ # @return [ModelDecorator]
86
122
  attr_reader :model_decorator
87
123
 
88
- # @return [ActionView::Base]
89
- # {Wallaby::Configuration::Mapping#resources_controller resources controller}'s helpers
124
+ # @return [ActionView::Base] {ResourcesController}'s helpers
90
125
  # @see .h
91
126
  def h
92
127
  self.class.h
93
128
  end
94
129
 
95
130
  delegate(*DELEGATE_METHODS, to: :model_decorator)
96
- # NOTE: this delegation is to make url helper method working properly with resource decorator instance
131
+ # NOTE: this delegation is to make Rails URL helper methods working properly with decorator instance
97
132
  delegate :to_s, :to_param, to: :resource
98
133
 
99
134
  # @param resource [Object]
@@ -120,8 +155,9 @@ module Wallaby
120
155
  # It falls back to primary key value when no text field is found.
121
156
  # @return [String] a label
122
157
  def to_label
123
- # NOTE: `.to_s` at the end is to ensure String is returned that won't cause any
124
- # issue when `#to_label` is used in a link_to block. Coz integer is ignored.
158
+ # NOTE: `.to_s` at the end is to ensure String is returned.
159
+ # There is an issue when {#to_label} returns an integer value, and it is used in a #link_to block,
160
+ # the #link_to will generate empty link text when integer value is given in the block.
125
161
  (model_decorator.guess_title(resource) || primary_key_value).to_s
126
162
  end
127
163
 
@@ -132,14 +168,18 @@ module Wallaby
132
168
 
133
169
  # @return [Object] primary key value
134
170
  def primary_key_value
171
+ return if primary_key.blank?
172
+
135
173
  resource.try primary_key
136
174
  end
137
175
 
176
+ # @note this method is for the Rails URL helper methods to recognize non-ActiveModel models
138
177
  # @return [ActiveModel::Name]
139
178
  def model_name
140
179
  resource.try(:model_name) || ActiveModel::Name.new(model_class)
141
180
  end
142
181
 
182
+ # @note this method is for the Rails form helper methods to recognize non-ActiveModel models
143
183
  # @return [nil] if no primary key
144
184
  # @return [Array<String>] primary key
145
185
  def to_key
@@ -149,20 +189,18 @@ module Wallaby
149
189
 
150
190
  # Delegate missing method to {#resource}
151
191
  def method_missing(method_id, *args, &block)
152
- return super unless resource.respond_to? method_id
153
-
154
- resource.try method_id, *args, &block
192
+ if resource.respond_to?(method_id)
193
+ resource.try(method_id, *args, &block)
194
+ elsif model_decorator.respond_to?(method_id)
195
+ model_decorator.try(method_id, *args, &block)
196
+ else
197
+ super
198
+ end
155
199
  end
156
200
 
157
201
  # Delegate missing method check to {#resource}
158
202
  def respond_to_missing?(method_id, _include_private)
159
- resource.respond_to?(method_id) || super
160
- end
161
-
162
- # @see https://github.com/rails/rails/compare/v7.0.2.4..7-0-stable#diff-44b94eca66c7497711821a8e6bcdfde4684bb7b8efa15e64da6532449f03ef0bR441
163
- # @note This overwritten method is a response to the above change
164
- def to_model
165
- self
203
+ [resource, model_decorator].any? { |v| v.respond_to?(method_id) } || super
166
204
  end
167
205
  end
168
206
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- class ClassNotFound < GeneralError
5
- end
4
+ ClassNotFound = Class.new(GeneralError)
6
5
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- class Forbidden < GeneralError
5
- end
4
+ Forbidden = Class.new(GeneralError)
6
5
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- class GeneralError < ::StandardError
4
+ class GeneralError < ::StandardError # :nodoc:
5
5
  end
6
6
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- class InvalidError < GeneralError
5
- end
4
+ InvalidError = Class.new(GeneralError)
6
5
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ MethodRemoved = Class.new(GeneralError)
5
+ end
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Model not found error
5
- class ModelNotFound < NotFound
4
+ class ModelNotFound < NotFound # :nodoc:
6
5
  # @return [String] not found error message
7
6
  def message
8
7
  return super if super.include? "\n"
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- class NotAuthenticated < GeneralError
5
- end
4
+ NotAuthenticated = Class.new(GeneralError)
6
5
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- class NotFound < GeneralError
5
- end
4
+ NotFound = Class.new(GeneralError)
6
5
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- class NotImplemented < GeneralError
5
- end
4
+ NotImplemented = Class.new(GeneralError)
6
5
  end
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # Resource not found error
5
- class ResourceNotFound < NotFound
4
+ class ResourceNotFound < NotFound # :nodoc:
6
5
  # @return [String] resource not found error message
7
6
  def message
8
7
  Locale.t 'errors.not_found.resource', resource: super
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- class UnprocessableEntity < GeneralError
5
- end
4
+ UnprocessableEntity = Class.new(GeneralError)
6
5
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ # A simple wrapper so that all fields (index/show/form and other fields appear before itself)
5
+ # can be set in one-line.
6
+ #
7
+ # @example set type for name to `'string'` in decorator
8
+ # class ProductDecorator < ApplicationDecorator
9
+ # some_fields[:id][:sort_disabled] = true
10
+ # all_fields[:name][:type] = 'string'
11
+ # # index_fields[:name][:type] => 'string'
12
+ # # show_fields[:name][:type] => 'string'
13
+ # # form_fields[:name][:type] => 'string'
14
+ # # some_fields[:name][:type] => 'string'
15
+ # end
16
+ class AllFields
17
+ def initialize(decorator)
18
+ @decorator = decorator
19
+ @keys = []
20
+ end
21
+
22
+ # @param key [Symbol, String]
23
+ # @return [AllFields] self
24
+ def [](key)
25
+ @keys << key
26
+ self
27
+ end
28
+
29
+ # Set value for given keys
30
+ # @param last_key [Symbol, String]
31
+ # @param value [Object]
32
+ # @return [Object] value
33
+ def []=(last_key, value)
34
+ all_fields.each do |fields_method|
35
+ last = @keys.reduce(@decorator.try(fields_method)) do |metadata, key|
36
+ metadata.try :[], key
37
+ end
38
+ last.try :[]=, last_key, value
39
+ end
40
+
41
+ @keys = []
42
+ value # rubocop:disable Lint/Void
43
+ end
44
+
45
+ protected
46
+
47
+ def all_fields
48
+ existing_fields + possible_fields_from_instance_variables
49
+ end
50
+
51
+ def existing_fields
52
+ %w[index_fields show_fields form_fields]
53
+ end
54
+
55
+ def possible_fields_from_instance_variables
56
+ @decorator
57
+ .model_decorator.instance_variables
58
+ .map(&:to_s)
59
+ .grep(/\A@[a-zA-Z]\w*_fields\Z/)
60
+ .map { |s| s[1..] }
61
+ end
62
+ end
63
+ end
@@ -20,7 +20,7 @@ module Wallaby
20
20
  content_tag :ul, class: 'errors' do
21
21
  errors.each do |message|
22
22
  concat content_tag :li, content_tag(
23
- :small, raw(message) # rubocop:disable Rails/OutputSafety
23
+ :small, raw(message)
24
24
  )
25
25
  end
26
26
  end
@@ -49,7 +49,7 @@ module Wallaby
49
49
  # Delegate the method so that we don't come in here the next time
50
50
  # when same method is called
51
51
  self.class.delegate method, to: :@template
52
- @template.public_send method, *args, &block
52
+ @template.try method, *args, &block
53
53
  end
54
54
 
55
55
  # Delegate missing method check to `@template`
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ # base generator
5
+ class ApplicationGenerator < Rails::Generators::NamedBase
6
+ argument :name, type: :string
7
+ argument :parent_name, type: :string, default: nil, required: false
8
+
9
+ def install
10
+ template "#{base_name}.rb.erb", "app/#{base_name}s/#{name}_#{base_name}.rb"
11
+ end
12
+
13
+ protected
14
+
15
+ def base_class
16
+ "#{class_name}#{base_name.classify}"
17
+ end
18
+
19
+ def parent_base_class
20
+ return "#{parent_name.classify}#{base_name.classify}" if parent_name
21
+
22
+ application_class
23
+ end
24
+
25
+ def application_class
26
+ Wallaby.configuration.resources_controller.try "application_#{base_name}"
27
+ end
28
+
29
+ def model_name
30
+ class_name.singularize
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,20 @@
1
+ Description:
2
+ Generate authorizer for Wallaby Engine usage by given NAME.
3
+ It also takes the PARENT name and makes the generated authorizer inherit from the PARENT.
4
+
5
+ For example, if 'apple' is provided as the NAME, this generator will carry out the following items:
6
+ - Generate AppleAuthorizer that inherits from Wallaby Engine's application authorizer.
7
+
8
+ Example:
9
+ rails generate wallaby:engine:authorizer admin/user
10
+
11
+ This will create:
12
+ - app/authorizers/admin/user_authorizer.rb
13
+
14
+ Example:
15
+ rails generate wallaby:engine:authorizer admin/user admin/application
16
+
17
+ This will create:
18
+ - app/authorizers/admin/user_authorizer.rb
19
+
20
+ And it makes Admin::UserAuthorizer class inherit from Admin::ApplicationAuthorizer
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../application_generator'
4
+
5
+ module Wallaby
6
+ class Engine
7
+ # `wallaby:engine:authorizer` generator
8
+ # @see https://github.com/wallaby-rails/wallaby-core/blob/master/lib/generators/wallaby/engine/authorizer/USAGE
9
+ class AuthorizerGenerator < ApplicationGenerator
10
+ source_root File.expand_path('templates', __dir__)
11
+
12
+ protected
13
+
14
+ def base_name
15
+ 'authorizer'
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ # @see https://github.com/wallaby-rails/wallaby/blob/master/docs/authorizer.md
2
+ class <%= base_class %> < <%= parent_base_class %>
3
+ # Specify the model class if Wallaby doesn't get it right
4
+ # self.model_class = <%= model_name %>
5
+
6
+ # Specify which provider (:cancancan, :pundit or :default) to use.
7
+ # :default means no authorization will be in use.
8
+ # self.provider_name = :cancancan
9
+
10
+ # Check user's permission for given action on given subject.
11
+ # Raise Wallaby::Forbidden if user doesn't have permission
12
+ # def authorize(action, subject)
13
+ # super # do something
14
+ # end
15
+
16
+ # Check and see if user is allowed to perform an action on given subject.
17
+ # def authorized?(action, subject)
18
+ # super # do something
19
+ # end
20
+
21
+ # Filter the scope based on user's permission.
22
+ # def accessible_for(action, scope)
23
+ # super # do something
24
+ # end
25
+
26
+ # Assign the values based on user's permission.
27
+ # def attributes_for(action, subject)
28
+ # super # do something
29
+ # end
30
+
31
+ # Allowlist the parameters based on user's permission.
32
+ # def permit_params(action, subject)
33
+ # super # do something
34
+ # end
35
+ end
@@ -0,0 +1,20 @@
1
+ Description:
2
+ Generate controller for Wallaby Engine usage by given NAME.
3
+ It also takes the PARENT name and makes the generated controller inherit from the PARENT.
4
+
5
+ For example, if 'apples' is provided as the NAME, this generator will carry out the following items:
6
+ - Generate ApplesController that inherits from Wallaby Engine's application controller.
7
+
8
+ Example:
9
+ rails generate wallaby:engine:controller admin/users
10
+
11
+ This will create:
12
+ - app/controllers/admin/users_controller.rb
13
+
14
+ Example:
15
+ rails generate wallaby:engine:controller admin/users admin/application
16
+
17
+ This will create:
18
+ - app/controllers/admin/users_controller.rb
19
+
20
+ And it makes Admin::UsersController class inherit from Admin::ApplicationController
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../application_generator'
4
+
5
+ module Wallaby
6
+ class Engine
7
+ # `wallaby:engine:controller` generator
8
+ # @see https://github.com/wallaby-rails/wallaby-core/blob/master/lib/generators/wallaby/engine/controller/USAGE
9
+ class ControllerGenerator < ApplicationGenerator
10
+ source_root File.expand_path('templates', __dir__)
11
+
12
+ protected
13
+
14
+ def application_class
15
+ Wallaby.configuration.resources_controller
16
+ end
17
+
18
+ def base_name
19
+ 'controller'
20
+ end
21
+ end
22
+ end
23
+ end