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
@@ -0,0 +1,20 @@
1
+ Description:
2
+ Generate servicer for Wallaby Engine usage by given NAME.
3
+ It also takes the PARENT name and makes the generated servicer 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 AppleServicer that inherits from Wallaby Engine's application servicer.
7
+
8
+ Example:
9
+ rails generate wallaby:engine:servicer admin/user
10
+
11
+ This will create:
12
+ - app/servicers/admin/user_servicer.rb
13
+
14
+ Example:
15
+ rails generate wallaby:engine:servicer admin/user admin/application
16
+
17
+ This will create:
18
+ - app/servicers/admin/user_servicer.rb
19
+
20
+ And it makes Admin::UserServicer class inherit from Admin::ApplicationServicer
@@ -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:servicer` generator
8
+ # @see https://github.com/wallaby-rails/wallaby-core/blob/master/lib/generators/wallaby/engine/servicer/USAGE
9
+ class ServicerGenerator < ApplicationGenerator
10
+ source_root File.expand_path('templates', __dir__)
11
+
12
+ protected
13
+
14
+ def base_name
15
+ 'servicer'
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,45 @@
1
+ # @see https://github.com/wallaby-rails/wallaby/blob/master/docs/servicer.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
+ # Allowlist parameters for mass assignment
7
+ # def permit(params, action)
8
+ # super # do something
9
+ # end
10
+
11
+ # Fetch collection by given params
12
+ # def collection(params)
13
+ # super # do something
14
+ # end
15
+
16
+ # Paginate the collection
17
+ # def paginate(query, params)
18
+ # super # do something
19
+ # end
20
+
21
+ # Initialize the model class using given params
22
+ # def new(params)
23
+ # super # do something
24
+ # end
25
+
26
+ # Find a resource using id
27
+ # def find(id, params)
28
+ # super # do something
29
+ # end
30
+
31
+ # Save the newly initialized resource
32
+ # def create(resource, params)
33
+ # super # do something
34
+ # end
35
+
36
+ # Update the persisted resource
37
+ # def update(resource, params)
38
+ # super # do something
39
+ # end
40
+
41
+ # Destroy the given resource
42
+ # def destroy(resource, params)
43
+ # super # do something
44
+ # end
45
+ end
@@ -4,71 +4,22 @@ module Wallaby
4
4
  # Wallaby application helper
5
5
  module ApplicationHelper
6
6
  include ConfigurationHelper
7
+ include Configurable
7
8
  include Engineable
8
- include SharedHelpers
9
+ include Urlable
9
10
 
10
- # @!method try_to(subject, method_id, *args, &block)
11
- # (see Wallaby::ModuleUtils.try_to)
12
- # @see Wallaby::ModuleUtils.try_to
13
- delegate :try_to, to: ModuleUtils
14
-
15
- # Override origin method to handle URL for Wallaby engine.
16
- #
17
- # As Wallaby's routes are declared in a
18
- # {https://guides.rubyonrails.org/routing.html#routing-to-rack-applications Rack application} fashion, this will
19
- # lead to **ActionController::RoutingError** exception when using ordinary **url_for**
20
- # (e.g. `url_for action: :index`).
21
- #
22
- # Gotcha: Wallaby can't cope well with the following situation.
23
- # It's due to the limit of route declaration and matching:
24
- #
25
- # ```
26
- # scope path: '/prefix' do
27
- # wresources :products, controller: 'wallaby/resources'
28
- # end
29
- # ```
30
- # @param options [String, Hash, ActionController::Parameters]
31
- # @option options [Boolean]
32
- # :with_query to include `request.query_parameters` values for url generation.
33
- # @option options [String]
34
- # :engine_name to specify the engine_name to use, default to {Wallaby::Engineable#current_engine_name}
35
- # @return [String] URL string
36
- # @see Wallaby::EngineUrlFor.handle
37
- # @see https://api.rubyonrails.org/classes/ActionView/RoutingUrlFor.html#method-i-url_for
38
- # ActionView::RoutingUrlFor#url_for
39
- def url_for(options = nil)
40
- if options.is_a?(Hash) || try_to(options, :permitted?)
41
- # merge with all current query parameters
42
- options = request.query_parameters.merge(options) if options.delete(:with_query)
43
- options = ParamsUtils.presence url_options, options # remove blank values
44
- EngineUrlFor.handle(
45
- engine_name: options.fetch(:engine_name, current_engine_name), parameters: options
46
- )
47
- end || super(options)
48
- end
49
-
50
- # Override origin method to add turbolinks tracking when it's enabled
51
- # @param sources [Array<String>]
52
- # @return [String] stylesheet link tags HTML
53
- def stylesheet_link_tag(*sources)
54
- default_options =
55
- features.turbolinks_enabled ? { 'data-turbolinks-track' => true } : {}
56
- options = default_options.merge!(sources.extract_options!.stringify_keys)
57
- super(*sources, options)
58
- end
59
-
60
- # Override origin method to add turbolinks tracking when it's enabled
61
- # @param sources [Array<String>]
62
- # @return [String] javascript script tags HTML
63
- def javascript_include_tag(*sources)
64
- default_options =
65
- features.turbolinks_enabled ? { 'data-turbolinks-track' => true, 'data-turbolinks-eval' => false } : {}
66
- options = default_options.merge!(sources.extract_options!.stringify_keys)
67
- super(*sources, options)
11
+ # Override original #form_for method to provide default form builder
12
+ # @param record [ActiveRecord::Base, String, Symbol]
13
+ # @param options [Hash]
14
+ def form_for(record, options = {}, &block)
15
+ options[:builder] ||= FormBuilder
16
+ super
68
17
  end
69
18
 
19
+ # I18n transaltion just for Wallaby
70
20
  # @param key
71
21
  # @param options [Hash]
22
+ # @return [String] transaltion for given key
72
23
  def wt(key, options = {})
73
24
  Locale.t key, { translator: method(:t) }.merge(options)
74
25
  end
@@ -1,24 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wallaby
4
- # NOTE: Global helper methods should go in here
4
+ # Some basic helper methods
5
5
  module BaseHelper
6
6
  include StylingHelper
7
7
  include LinksHelper
8
8
 
9
- # @see ModelUtils.to_model_label
10
9
  # @return [String] label for given model class
10
+ # @see Inflector.to_model_label
11
11
  def to_model_label(model_class)
12
- ModelUtils.to_model_label model_class
12
+ Inflector.to_model_label(model_class)
13
13
  end
14
14
 
15
- # @see Map.resources_name_map
16
15
  # @return [String] resources name for given model class
16
+ # @see Map.resources_name_map
17
17
  def to_resources_name(model_class)
18
18
  Map.resources_name_map model_class
19
19
  end
20
20
 
21
21
  # Generate body class from the following sources:
22
+ #
22
23
  # - `:action` parameter
23
24
  # - converted current resources name (e.g. `order__item` from `Order::Item`)
24
25
  # - `:custom_body_class` content
@@ -32,11 +33,10 @@ module Wallaby
32
33
  ].compact.join SPACE
33
34
  end
34
35
 
35
- # Turn a list of classes into tree structure by inheritance.
36
+ # Turn a list of model classes into an inheritance tree.
36
37
  # @param classes [Array<Class>]
37
- # a list of all the classes that wallaby supports
38
- # @return [Array<Wallaby::Node>] a tree structure of given classes
39
- def model_classes(classes = Map.model_classes)
38
+ # @return [Array<Node>]
39
+ def model_classes(classes = wallaby_controller.all_models)
40
40
  nested_hash = classes.each_with_object({}) do |klass, hash|
41
41
  hash[klass] = Node.new(klass)
42
42
  end
@@ -47,11 +47,11 @@ module Wallaby
47
47
  nested_hash.values.select { |v| v.parent.nil? }
48
48
  end
49
49
 
50
- # Turn the tree of classes into a nested `ul` list.
51
- # @param array [Array<Wallaby::Node>] root classes
50
+ # Render the HTML for the given model class tree.
51
+ # @param array [Array<Node>] root classes
52
52
  # @return [String] HTML for the whole tree
53
53
  def model_tree(array, base_class = nil)
54
- return EMPTY_STRING if array.blank?
54
+ return EMPTY_STRING if array.blank? || current_engine_name.blank?
55
55
 
56
56
  options = { html_options: { class: 'dropdown-item' } }
57
57
  content_tag :ul, class: 'dropdown-menu', 'aria-labelledby': base_class do
@@ -3,16 +3,48 @@
3
3
  module Wallaby
4
4
  # Configuration helper module. Provide shortcut methods to configurations.
5
5
  module ConfigurationHelper
6
- delegate :models, :security, :mapping, :pagination, :features, :sorting, to: :configuration
6
+ delegate :max_text_length, to: :wallaby_controller
7
7
 
8
- # @return [Wallaby::Configuration] shortcut method of configuration
8
+ # @return [Configuration] shortcut method of configuration
9
9
  def configuration
10
10
  Wallaby.configuration
11
11
  end
12
12
 
13
- # @return [Wallaby::Configuration::Metadata] shortcut method of metadata
13
+ # @deprecated
14
14
  def default_metadata
15
- configuration.metadata
15
+ Deprecator.alert 'default_metadata.max', from: '0.3.0', alternative: <<~INSTRUCTION
16
+ Please use `max_text_length` instead
17
+ INSTRUCTION
18
+ end
19
+
20
+ # @deprecated
21
+ def mapping
22
+ Deprecator.alert method(__callee__), from: '0.3.0'
23
+ end
24
+
25
+ # @deprecated
26
+ def security
27
+ Deprecator.alert method(__callee__), from: '0.3.0'
28
+ end
29
+
30
+ # @deprecated
31
+ def models
32
+ Deprecator.alert method(__callee__), from: '0.3.0'
33
+ end
34
+
35
+ # @deprecated
36
+ def pagination
37
+ Deprecator.alert method(__callee__), from: '0.3.0'
38
+ end
39
+
40
+ # @deprecated
41
+ def features
42
+ Deprecator.alert method(__callee__), from: '0.3.0'
43
+ end
44
+
45
+ # @deprecated
46
+ def sorting
47
+ Deprecator.alert method(__callee__), from: '0.3.0'
16
48
  end
17
49
  end
18
50
  end
@@ -13,7 +13,7 @@ module Wallaby
13
13
  # the typed keyword
14
14
  # @return [String] URL for autocomplete
15
15
  def remote_url(url, model_class, wildcard = 'QUERY')
16
- url || index_path(model_class, url_params: { q: wildcard, per: pagination.page_size })
16
+ url || index_path(model_class, url_params: { q: wildcard, per: wallaby_controller.try(:page_size) })
17
17
  end
18
18
 
19
19
  # To generate dropdown options (class => url) for polymorphic class.
@@ -34,7 +34,7 @@ module Wallaby
34
34
  # @param filter_name [String, Symbol]
35
35
  # @param filters [Hash]
36
36
  # @return [String] filter name
37
- # @see Wallaby::FilterUtils.filter_name_by
37
+ # @see FilterUtils.filter_name_by
38
38
  def filter_name_by(filter_name, filters)
39
39
  FilterUtils.filter_name_by filter_name, filters
40
40
  end
@@ -45,28 +45,38 @@ module Wallaby
45
45
  # @param filters [Hash]
46
46
  # @param url_params [Hash, ActionController::Parameters]
47
47
  # @return [String] HTML anchor link
48
- def filter_link(model_class, filter_name, filters: {}, url_params: {})
48
+ def filter_link(model_class, filter_name, filters: {}, url_params: {}, html_options: {})
49
49
  is_all = filter_name == :all
50
50
  config = filters[filter_name] || {}
51
51
  label = is_all ? all_label : filter_label(filter_name, filters)
52
52
  url_params[:filter] = config[:default] ? nil : filter_name
53
- index_link(model_class, options: { url: url_for(url_params.merge(with_query: true)) }) { label }
53
+
54
+ index_link(
55
+ model_class,
56
+ url_params: url_params.merge(with_query: true), html_options: html_options
57
+ ) { label }
54
58
  end
55
59
 
56
60
  # @param model_class [Class]
57
61
  # @param url_params [Hash, ActionController::Parameters] extra URL params
58
62
  # @return [String] Export link for the given model_class.
59
- def export_link(model_class, url_params: {})
60
- index_link(model_class, url_params: { format: 'csv', page: nil, per: nil, with_query: true }.merge(url_params)) do
63
+ def export_link(model_class, url_params: {}, html_options: {})
64
+ index_link(
65
+ model_class,
66
+ url_params: url_params.merge(format: 'csv', page: nil, per: nil, with_query: true),
67
+ html_options: html_options
68
+ ) do
61
69
  wt 'links.export', ext: 'CSV'
62
70
  end
63
71
  end
64
72
 
65
- # @return [Wallaby::Sorting::LinkBuilder]
66
- # @see Wallaby::Sorting::LinkBuilder
67
- def sort_link_builder
73
+ # @return [Sorting::LinkBuilder]
74
+ # @see Sorting::LinkBuilder
75
+ def sort_link_builder(sorting_strategy = wallaby_controller.sorting_strategy)
68
76
  @sort_link_builder ||=
69
- Sorting::LinkBuilder.new current_model_decorator, params.slice(:sort).permit!, self, sorting.strategy
77
+ Sorting::LinkBuilder.new(
78
+ current_model_decorator, params.slice(:sort).permit!, self, sorting_strategy
79
+ )
70
80
  end
71
81
  end
72
82
  end
@@ -7,7 +7,7 @@ module Wallaby
7
7
  # @param model_class [Class]
8
8
  # @param options [Hash]
9
9
  # @option options [String] :url url/path for the link
10
- # @param url_params [ActionController::Parameters, Hash]
10
+ # @param url_params [Hash, ActionController::Parameters]
11
11
  # @param html_options [Hash] (see
12
12
  # {https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
13
13
  # ActionView::Helpers::UrlHelper#link_to})
@@ -22,7 +22,6 @@ module Wallaby
22
22
  block: -> { to_model_label model_class }
23
23
  )
24
24
 
25
- url_params = request.query_parameters.merge(url_params) if url_params.delete(:with_query)
26
25
  url = options[:url] || index_path(model_class, url_params: url_params)
27
26
  link_to url, html_options, &block
28
27
  end
@@ -31,7 +30,7 @@ module Wallaby
31
30
  # @param model_class [Class]
32
31
  # @param options [Hash]
33
32
  # @option options [String] :url url/path for the link
34
- # @param url_params [ActionController::Parameters, Hash]
33
+ # @param url_params [Hash, ActionController::Parameters]
35
34
  # @param html_options [Hash] (see
36
35
  # {https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
37
36
  # ActionView::Helpers::UrlHelper#link_to})
@@ -56,8 +55,8 @@ module Wallaby
56
55
  # @param options [Hash]
57
56
  # @option options [String] :url url/path for the link
58
57
  # @option options [Boolean] :readonly readonly and therefore output the label
59
- # @option options [Boolean] :is_resource to tell {#show_path} if it is a resource
60
- # @param url_params [ActionController::Parameters, Hash]
58
+ # @option options [Boolean] :is_resource to tell {Urlable#show_path} if it is a resource
59
+ # @param url_params [Hash, ActionController::Parameters]
61
60
  # @param html_options [Hash] (see
62
61
  # {https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
63
62
  # ActionView::Helpers::UrlHelper#link_to})
@@ -73,10 +72,10 @@ module Wallaby
73
72
  block: -> { decorate(resource).to_label.to_s }
74
73
  )
75
74
 
76
- default = options[:readonly] && block.call || nil
75
+ default = (options[:readonly] && block.call) || nil
77
76
  return default if unauthorized? :show, extract(resource)
78
77
 
79
- url = options[:url] || show_path(resource, is_resource: options[:is_resource], url_params: url_params)
78
+ url = options[:url] || show_path(resource, url_params: url_params)
80
79
  link_to url, html_options, &block
81
80
  end
82
81
 
@@ -85,8 +84,8 @@ module Wallaby
85
84
  # @param options [Hash]
86
85
  # @option options [String] :url url/path for the link
87
86
  # @option options [Boolean] :readonly readonly and therefore output the label
88
- # @option options [Boolean] :is_resource to tell {#edit_path} if it is a resource
89
- # @param url_params [ActionController::Parameters, Hash]
87
+ # @option options [Boolean] :is_resource to tell {Urlable#edit_path} if it is a resource
88
+ # @param url_params [Hash, ActionController::Parameters]
90
89
  # @param html_options [Hash] (see
91
90
  # {https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
92
91
  # ActionView::Helpers::UrlHelper#link_to})
@@ -100,10 +99,10 @@ module Wallaby
100
99
  block: -> { "#{wt 'links.edit'} #{decorate(resource).to_label}" }
101
100
  )
102
101
 
103
- default = options[:readonly] && block.call || nil
102
+ default = (options[:readonly] && block.call) || nil
104
103
  return default if unauthorized? :edit, extract(resource)
105
104
 
106
- url = options[:url] || edit_path(resource, is_resource: options[:is_resource], url_params: url_params)
105
+ url = options[:url] || edit_path(resource, url_params: url_params)
107
106
  link_to url, html_options, &block
108
107
  end
109
108
 
@@ -111,8 +110,8 @@ module Wallaby
111
110
  # @param resource [Object, Wallaby::ResourceDecorator] model class
112
111
  # @param options [Hash]
113
112
  # @option options [String] :url url/path for the link
114
- # @option options [Boolean] :is_resource to tell {#edit_path} if it is a resource
115
- # @param url_params [ActionController::Parameters, Hash]
113
+ # @option options [Boolean] :is_resource to tell {Urlable#edit_path} if it is a resource
114
+ # @param url_params [Hash, ActionController::Parameters]
116
115
  # @param html_options [Hash] (see
117
116
  # {https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
118
117
  # ActionView::Helpers::UrlHelper#link_to})
@@ -129,7 +128,7 @@ module Wallaby
129
128
  html_options[:method] ||= :delete
130
129
  (html_options[:data] ||= {})[:confirm] ||= wt 'links.confirm.delete'
131
130
 
132
- url = options[:url] || show_path(resource, is_resource: options[:is_resource], url_params: url_params)
131
+ url = options[:url] || show_path(resource, url_params: url_params)
133
132
  link_to url, html_options, &block
134
133
  end
135
134
 
@@ -143,71 +142,5 @@ module Wallaby
143
142
  block ||= -> { wt 'links.cancel' }
144
143
  link_to 'javascript:history.back()', html_options, &block
145
144
  end
146
-
147
- # @param model_class [Class]
148
- # @param url_params [Hash]
149
- # @return [String] index page path
150
- def index_path(model_class, url_params: {})
151
- hash = ParamsUtils.presence(
152
- { action: :index },
153
- default_path_params(resources: to_resources_name(model_class)),
154
- url_params.to_h
155
- )
156
- current_engine.try(:resources_path, hash) || url_for(hash)
157
- end
158
-
159
- # @param model_class [Class]
160
- # @param url_params [Hash]
161
- # @return [String] new page path
162
- def new_path(model_class, url_params: {})
163
- hash = ParamsUtils.presence(
164
- { action: :new },
165
- default_path_params(resources: to_resources_name(model_class)),
166
- url_params.to_h
167
- )
168
- current_engine.try(:new_resource_path, hash) || url_for(hash)
169
- end
170
-
171
- # @param resource [Object]
172
- # @param is_resource [Boolean]
173
- # @param url_params [Hash]
174
- # @return [String] show page path
175
- def show_path(resource, is_resource: false, url_params: {})
176
- decorated = decorate resource
177
- return unless is_resource || decorated.primary_key_value
178
-
179
- hash = ParamsUtils.presence(
180
- { action: :show, id: decorated.primary_key_value },
181
- default_path_params(resources: decorated.resources_name),
182
- url_params.to_h
183
- )
184
-
185
- current_engine.try(:resource_path, hash) || url_for(hash)
186
- end
187
-
188
- # @param resource [Object]
189
- # @param is_resource [Boolean]
190
- # @param url_params [Hash]
191
- # @return [String] edit page path
192
- def edit_path(resource, is_resource: false, url_params: {})
193
- decorated = decorate resource
194
- return unless is_resource || decorated.primary_key_value
195
-
196
- hash = ParamsUtils.presence(
197
- { action: :edit, id: decorated.primary_key_value },
198
- default_path_params(resources: decorated.resources_name),
199
- url_params.to_h
200
- )
201
-
202
- current_engine.try(:edit_resource_path, hash) || url_for(hash)
203
- end
204
-
205
- # @return [Hash] default path params
206
- def default_path_params(resources: nil)
207
- { script_name: request.env[SCRIPT_NAME] }.tap do |default|
208
- default[:resources] = resources if current_engine || resources
209
- default[:only_path] = true unless default.key?(:only_path)
210
- end
211
- end
212
145
  end
213
146
  end
@@ -16,18 +16,50 @@ module Wallaby
16
16
  include Resourcable
17
17
  include Servicable
18
18
 
19
- # Render type cell/partial
20
- # @param partial_name [String]
21
- # @param locals [Hash]
22
- def type_render(partial_name = '', locals = {}, &block)
23
- TypeRenderer.render self, partial_name, locals, &block
19
+ # @deprecated
20
+ def type_render(_partial_name = '', _locals = {})
21
+ Deprecator.alert method(__callee__), from: '0.3.0', alternative: <<~INSTRUCTION
22
+ If it's for index type partils, please follow below example:
23
+
24
+ render(
25
+ field_name,
26
+ object: decorated,
27
+ field_name: field_name,
28
+ value: decorated.try(field_name),
29
+ metadata: decorated.index_metadata_of(field_name)
30
+ )
31
+
32
+ If it's for show type partils, please follow below example:
33
+
34
+ render(
35
+ field_name,
36
+ object: decorated,
37
+ field_name: field_name,
38
+ value: decorated.try(field_name),
39
+ metadata: decorated.show_metadata_of(field_name)
40
+ )
41
+
42
+ If it's for new/create/edit/update/destroy type partils, please follow below example:
43
+
44
+ render(
45
+ field_name,
46
+ form: form,
47
+ object: decorated,
48
+ field_name: field_name,
49
+ value: decorated.try(field_name),
50
+ metadata: decorated.form_metadata_of(field_name)
51
+ )
52
+
53
+ INSTRUCTION
24
54
  end
25
55
 
26
56
  # Title for show page of given resource
27
- # @param decorated [Wallaby::ResourceDecorator]
57
+ # @param decorated [ResourceDecorator]
28
58
  # @return [String]
29
59
  def show_title(decorated)
30
- raise ::ArgumentError unless decorated.is_a? ResourceDecorator
60
+ unless decorated.is_a? ResourceDecorator
61
+ raise ::ArgumentError, 'Please provide a resource wrapped by a decorator.'
62
+ end
31
63
 
32
64
  [
33
65
  to_model_label(decorated.model_class), decorated.to_label
@@ -8,42 +8,43 @@ module Wallaby
8
8
  # - if email is present, a gravatar image tag will be returned
9
9
  # - otherwise, an user icon will be returned
10
10
  # @param user [Object]
11
+ # @param method_name [Symbol, String]
11
12
  # @return [String] IMG or I element
12
- def user_portrait(user = wallaby_user)
13
- email_method = security.email_method || :email
14
- email = try_to user, email_method
15
- if email.present?
16
- https = "http#{request.ssl? ? 's' : EMPTY_STRING}"
17
- email_md5 = ::Digest::MD5.hexdigest email.downcase
18
- image_source = "#{https}://www.gravatar.com/avatar/#{email_md5}"
19
- image_tag image_source, class: 'user'
20
- else
21
- fa_icon 'user'
22
- end
13
+ def user_portrait(user: wallaby_user, method_name: wallaby_controller.email_method)
14
+ method_name ||= user.methods.grep(/email/i).min || :email
15
+ email = user.try method_name
16
+ return fa_icon 'user' if email.blank?
17
+
18
+ image_tag(
19
+ "#{request.protocol}www.gravatar.com/avatar/#{::Digest::MD5.hexdigest email.downcase}", # rubocop:disable Fips/MD5
20
+ class: 'user'
21
+ )
23
22
  end
24
23
 
25
24
  # Logout path for given user
26
- # @see Wallaby::Configuration::Security#logout_path
25
+ # @see Configuration::Security#logout_path
27
26
  # @param user [Object]
28
27
  # @param app [Object]
28
+ # @param method_name [Symbol, String]
29
29
  # @return [String] URL to log out
30
- def logout_path(user = wallaby_user, app = main_app)
31
- path = security.logout_path
32
- path ||=
30
+ def logout_path(user: wallaby_user, app: main_app, method_name: wallaby_controller.logout_path)
31
+ method_name ||=
33
32
  if defined? ::Devise
34
33
  scope = ::Devise::Mapping.find_scope! user
35
34
  "destroy_#{scope}_session_path"
36
35
  end
37
- try_to app, path
36
+
37
+ app.try method_name if method_name
38
38
  end
39
39
 
40
40
  # Logout method for given user
41
- # @see Wallaby::Configuration::Security#logout_method
41
+ # @see Configuration::Security#logout_method
42
42
  # @param user [Object]
43
+ # @param http_method [Symbol, String]
43
44
  # @return [String, Symbol] http method to log out
44
- def logout_method(user = wallaby_user)
45
- http_method = security.logout_method
45
+ def logout_method(user: wallaby_user, http_method: wallaby_controller.logout_method)
46
46
  http_method ||
47
+
47
48
  if defined? ::Devise
48
49
  scope = ::Devise::Mapping.find_scope! user
49
50
  mapping = ::Devise.mappings[scope]