cirandas.net

ref: master

config/routes/50_plugins.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
paths              = {}
profile_format     = /#{Noosfero.identifier_format}/i
plugins_root       = if Rails.env.test? then 'plugins' else '{baseplugins,config/plugins}' end
plugins_paths      = "#{Rails.root}/#{plugins_root}/*/"
prefixes_by_folder = {
  public:    'plugin',
  profile:   'profile(/:profile)/plugin',
  myprofile: 'myprofile(/:profile)/plugin',
  admin:     'admin/plugin',
}

Dir[plugins_paths].each do |plugin_dir|
  plugin_name       = File.basename plugin_dir
  controllers_paths = "#{plugin_dir}/{app/,}controllers"

  Dir[controllers_paths].each do |controllers_dir|
    controllers_by_folder = prefixes_by_folder.keys.inject({}) do |hash, folder|
      path = "#{controllers_dir}/#{folder}/"
      hash[folder] = Dir.glob("#{path}{*.rb,#{plugin_name}_plugin/*.rb}").map do |filename|
        filename.gsub(path, '').gsub(/[_\/]controller.rb$/, '')
      end
      hash
    end

    controllers_by_folder.each do |folder, controllers|
      controllers.each do |controller|
        controller_name = controller.gsub(/#{plugin_name}_plugin[_\/]?/, '')
        controller_path = if controller_name.present? then "/#{controller_name}" else '' end

        as  = controller.tr '/','_'
        url = "#{prefixes_by_folder[folder]}/#{plugin_name}#{controller_path}(/:action(/:id))"

        paths[url] = {
          controller: controller,
          via:        :all,
          as:         as,
        }
        paths[url][:profile] = profile_format if folder.to_s.in? %w[profile myprofile]
      end
    end

    # DEPRECATED default controllers
    paths.reverse_merge!(
      "plugin/#{plugin_name}(/:action(/:id))" => {
        controller: "#{plugin_name}_plugin",
          via:        :all,
      },
      "admin/plugin/#{plugin_name}(/:action(/:id))" => {
        controller: "#{plugin_name}_plugin_admin",
          via:        :all,
      },

      "profile(/:profile)/plugin/#{plugin_name}(/:action(/:id))" => {
        controller: "#{plugin_name}_plugin_profile",
          via:        :all,
          profile:    profile_format,
      },
      "myprofile(/:profile)/plugin/#{plugin_name}(/:action(/:id))" => {
        controller: "#{plugin_name}_plugin_myprofile",
          via:        :all,
          profile:    profile_format,
      },
    )
  end
end

Noosfero::Application.routes.draw do

  paths.each do |url, opts|
    controller_klass = "#{opts[:controller]}_controller".camelize.constantize rescue nil
    next unless controller_klass

    match url, opts
  end

end