aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/plugin.rb
blob: 4c34eb331ed8c130b1d2eacf7f3a737bd486c375 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'rails/engine'
require 'active_support/core_ext/array/conversions'

module Rails
  # Rails::Plugin is nothing more than a Rails::Engine, but since it's loaded too late
  # in the boot process, it does not have the same configuration powers as a bare
  # Rails::Engine.
  #
  # Opposite to Rails::Railtie and Rails::Engine, you are not supposed to inherit from
  # Rails::Plugin. Rails::Plugin is automatically configured to be an engine by simply
  # placing inside vendor/plugins. Since this is done automatically, you actually cannot
  # declare a Rails::Engine inside your Plugin, otherwise it would cause the same files
  # to be loaded twice. This means that if you want to ship an Engine as gem it cannot
  # be used as plugin and vice-versa.
  #
  # Besides this conceptual difference, the only difference between Rails::Engine and
  # Rails::Plugin is that plugins automatically load the file "init.rb" at the plugin
  # root during the boot process.
  #
  class Plugin < Engine
    def self.global_plugins
      @global_plugins ||= []
    end

    def self.inherited(base)
      raise "You cannot inherit from Rails::Plugin"
    end

    def self.all(list, paths)
      plugins = []
      paths.each do |path|
        Dir["#{path}/*"].each do |plugin_path|
          plugin = new(plugin_path)
          next unless list.include?(plugin.name) || list.include?(:all)
          if global_plugins.include?(plugin.name)
            warn "WARNING: plugin #{plugin.name} from #{path} was not loaded. Plugin with the same name has been already loaded."
            next
          end
          global_plugins << plugin.name
          plugins << plugin
        end
      end

      plugins.sort_by do |p|
        [list.index(p.name) || list.index(:all), p.name.to_s]
      end
    end

    attr_reader :name, :path

    def railtie_name
      name.to_s
    end

    def initialize(root)
      ActiveSupport::Deprecation.warn "Plugins are deprecated and will be removed in Rails 4.0. Use gems instead or bundler with path or git dependencies."
      @name = File.basename(root).to_sym
      config.root = root
    end

    def config
      @config ||= Engine::Configuration.new
    end

    initializer :handle_lib_autoload, :before => :set_load_path do |app|
      autoload = if app.config.reload_plugins
        config.autoload_paths
      else
        config.autoload_once_paths
      end

      autoload.concat paths["lib"].existent
    end

    initializer :load_init_rb, :before => :load_config_initializers do |app|
      init_rb = File.expand_path("init.rb", root)
      if File.file?(init_rb)
        # This double assignment is to prevent an "unused variable" warning on Ruby 1.9.3.
        config = config = app.config
        # TODO: think about evaling initrb in context of Engine (currently it's
        # always evaled in context of Rails::Application)
        eval(File.read(init_rb), binding, init_rb)
      end
    end

    initializer :sanity_check_railties_collision do
      if Engine.subclasses.map { |k| k.root.to_s }.include?(root.to_s)
        raise "\"#{name}\" is a Railtie/Engine and cannot be installed as a plugin"
      end
    end
  end
end