plutonium 0.18.4 → 0.18.5

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: 3fb21e14c1e59fae04a219cec33b1a5e4cb70f45bd65b225a1a03eae4bef2a22
4
- data.tar.gz: a85a28e236f423102d8c980c71c41817ee8b69dd25a966e001b5172774afdcf4
3
+ metadata.gz: d1f2533a233b5da2a308e4561e48269ffd70f116143b60a4c67b53c5488b3b4e
4
+ data.tar.gz: cf4f9a1758eed8cead02556e88f7f860a2e4922542ac6f41c937ef01a69cf308
5
5
  SHA512:
6
- metadata.gz: 6e59eb75688654e50018aae1fc931175f17200168522537cab05480f0a317ea3e64bf04db9f556775efb92cedf532b9fa1fd91ec8be57503d892cd68eb40b225
7
- data.tar.gz: c61a6183c890e6d47dd35ab54d055d54ecf31d48c09642a144757f9032c945465306db11c91f6b1535188d92d8e2de1cf0f79e071bd14c71b09aff7c15576f15
6
+ metadata.gz: ee3eb7c3309b7037d40bb19f23d1e6d01f3b362e5224bea386c3316ed786276061557a6615387f3285bce82aa8aec26e2ea93eba56a5041e33b28e31df61cd77
7
+ data.tar.gz: 6c911c0b178a4060cc5f8ae01c6653a3c856df8a8937612a87914fdf7baaf8e89ef2ed0d3aa60b43f0453268822ce9ad0af4311743d6f0515e6e8b618572b0ed
@@ -70,7 +70,8 @@ module Plutonium
70
70
  else
71
71
  controller_chain << element.class.to_s.pluralize
72
72
  if index == args.length - 1
73
- url_args[:id] = element.to_param
73
+ resource_route_config = current_engine.routes.resource_route_config_for(element.model_name.plural)[0]
74
+ url_args[:id] = element.to_param unless resource_route_config[:route_type] == :resource
74
75
  url_args[:action] ||= :show
75
76
  else
76
77
  url_args[element.model_name.singular_route_key.to_sym] = element.to_param
@@ -49,11 +49,25 @@ module Plutonium
49
49
  self.class.resource_class
50
50
  end
51
51
 
52
- # Returns the resource record based on path parameters
53
- # @return [ActiveRecord::Base, nil] The resource record
52
+ def resource_record_relation
53
+ @resource_record_relation ||= begin
54
+ resource_route_config = current_engine.routes.resource_route_config_for(resource_class.model_name.plural)[0]
55
+ if resource_route_config[:route_type] == :resource
56
+ current_authorized_scope
57
+ elsif params[:id]
58
+ current_authorized_scope.from_path_param(params[:id])
59
+ else
60
+ current_authorized_scope.none
61
+ end
62
+ end
63
+ end
64
+
54
65
  def resource_record
55
- @resource_record ||= current_authorized_scope.from_path_param(params[:id]).first! if params[:id]
56
- @resource_record
66
+ @resource_record ||= resource_record_relation.first!
67
+ end
68
+
69
+ def resource_record?
70
+ @resource_record ||= resource_record_relation.first
57
71
  end
58
72
 
59
73
  # Returns the submitted resource parameters
@@ -144,7 +144,13 @@ module Plutonium
144
144
  #
145
145
  # @return [Object] the subject for the policy (either resource_record or resource_class)
146
146
  def current_policy_subject
147
- resource_record || resource_class
147
+ # We have an "inconsistency" here where resource_record?
148
+ # will return an actual record instead of nil for routes such as :new when dealing with singular resources.
149
+ # It impacts mainly attribute policies, such as when getting the allowed attributes for forms.
150
+ # But while it is an an inconsistency, I believe it is expected behaviour.
151
+ # You did mark the resource as singular after all.
152
+ # So you should disable :create? in your policy when a record exists.
153
+ resource_record? || resource_class
148
154
  end
149
155
  end
150
156
  end
@@ -19,7 +19,7 @@ module Plutonium
19
19
  # @yield An optional block for additional resource configuration.
20
20
  # @return [void]
21
21
  def register_resource(resource, options = {}, &)
22
- route_config = route_set.register_resource(resource, &)
22
+ route_config = route_set.register_resource(resource, options, &)
23
23
  define_resource_routes(route_config, resource)
24
24
  resource_route_concern_names << route_config[:concern_name]
25
25
  end
@@ -65,7 +65,7 @@ module Plutonium
65
65
  # @return [void]
66
66
  def define_resource_routes(route_config, resource)
67
67
  concern route_config[:concern_name] do
68
- resources route_config[:route_name], **route_config[:route_options] do
68
+ send route_config[:route_type], route_config[:route_name], **route_config[:route_options] do
69
69
  instance_exec(&route_config[:block]) if route_config[:block]
70
70
  define_nested_resource_routes(resource)
71
71
  end
@@ -49,17 +49,18 @@ module Plutonium
49
49
  # Registers a resource for routing.
50
50
  #
51
51
  # @param resource [Class] The resource class to be registered.
52
+ # @param options [Hash] Additional options for resource registration.
52
53
  # @yield An optional block for additional resource configuration.
53
54
  # @return [Hash] The configuration for the registered resource.
54
55
  # @raise [ArgumentError] If the engine is not supported.
55
- def register_resource(resource, &)
56
+ def register_resource(resource, options = {}, &)
56
57
  self.class.validate_engine! engine
57
58
  engine.resource_register.register(resource)
58
59
 
59
60
  route_name = resource.model_name.plural
60
61
  concern_name = :"#{route_name}_routes"
61
62
 
62
- config = create_resource_config(resource, route_name, concern_name, &)
63
+ config = create_resource_config(resource, route_name, concern_name, options, &)
63
64
  resource_route_config_lookup[route_name] = config
64
65
 
65
66
  config
@@ -101,15 +102,17 @@ module Plutonium
101
102
  # @param resource_name [String] The name of the resource.
102
103
  # @param route_name [String] The pluralized name for routes.
103
104
  # @param concern_name [Symbol] The name of the concern for this resource.
105
+ # @param options [Hash] Additional options for resource registration.
104
106
  # @yield An optional block for additional resource configuration.
105
107
  # @return [Hash] The complete resource configuration.
106
- def create_resource_config(resource, route_name, concern_name, &block)
108
+ def create_resource_config(resource, route_name, concern_name, options = {}, &block)
107
109
  {
110
+ route_type: options[:singular] ? :resource : :resources,
108
111
  route_name: route_name,
109
112
  concern_name: concern_name,
110
113
  route_options: {
111
114
  controller: resource.to_s.pluralize.underscore,
112
- path: resource.model_name.collection,
115
+ path: options[:singular] ? resource.model_name.singular : resource.model_name.collection,
113
116
  concerns: %i[interactive_resource_actions]
114
117
  },
115
118
  block: block
@@ -1,5 +1,5 @@
1
1
  module Plutonium
2
- VERSION = "0.18.4"
2
+ VERSION = "0.18.5"
3
3
  NEXT_MAJOR_VERSION = VERSION.split(".").tap { |v|
4
4
  v[1] = v[1].to_i + 1
5
5
  v[2] = 0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plutonium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.4
4
+ version: 0.18.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Froelich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-31 00:00:00.000000000 Z
11
+ date: 2025-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk