Project

General

Profile

Defect #39862 » 0001-dynamic-object_type-routing-constraint-39862.patch

Jens Krämer, 2023-12-18 06:40

View differences:

config/routes.rb
317 317
  get 'attachments/download/:id', :to => 'attachments#download', :id => /\d+/
318 318
  get 'attachments/thumbnail/:id(/:size)', :to => 'attachments#thumbnail', :id => /\d+/, :size => /\d+/, :as => 'thumbnail'
319 319
  resources :attachments, :only => [:show, :update, :destroy]
320
  constraints object_type: /(issues|versions|news|messages|wiki_pages|projects|documents|journals)/ do
320

  
321
  # register plugin object types with ObjectTypeConstraint.register_object_type(PluginModel.name.underscore.pluralize')
322
  constraints Redmine::Acts::Attachable::ObjectTypeConstraint do
321 323
    get 'attachments/:object_type/:object_id/edit', :to => 'attachments#edit_all', :as => :object_attachments_edit
322 324
    patch 'attachments/:object_type/:object_id', :to => 'attachments#update_all', :as => :object_attachments
323 325
    get 'attachments/:object_type/:object_id/download', :to => 'attachments#download_all', :as => :object_attachments_download
lib/plugins/acts_as_attachable/lib/acts_as_attachable.rb
20 20
module Redmine
21 21
  module Acts
22 22
    module Attachable
23

  
24
      class ObjectTypeConstraint
25
        cattr_accessor :object_types
26

  
27
        self.object_types = Concurrent::Set.new(%w[
28
          issues versions news messages wiki_pages projects documents journals
29
        ])
30

  
31
        class << self
32
          def matches?(request)
33
            request.path_parameters[:object_type] =~ param_expression
34
          end
35

  
36
          def register_object_type(type)
37
            object_types << type
38
            @param_expression = nil
39
          end
40

  
41
          def param_expression
42
            @param_expression ||= Regexp.new("^(#{object_types.join("|")})$")
43
          end
44
        end
45
      end
46

  
23 47
      def self.included(base)
24 48
        base.extend ClassMethods
25 49
      end
test/fixtures/plugins/redmine_test_plugin_foo/init.rb
5 5
  version '0.0.1'
6 6
end
7 7

  
8
Redmine::Acts::Attachable::ObjectTypeConstraint.register_object_type('plugin_articles')
9

  
8 10
Pathname(__dir__).glob("app/**/*.rb").sort.each do |path|
9 11
  require path
10 12
end
test/integration/routing/plugins_test.rb
43 43
    should_route 'GET /plugin_articles' => 'plugin_articles#index'
44 44
    should_route 'GET /bar_plugin_articles' => 'bar_plugin_articles#index'
45 45
    assert_equal("/bar_plugin_articles", plugin_articles_path)
46
    should_route(
47
      'GET /attachments/plugin_articles/12/edit' => 'attachments#edit_all',
48
      object_id: '12',
49
      object_type: 'plugin_articles'
50
    )
46 51
  end
47 52
end
    (1-1/1)