Module: Engines::PluginExtension

Included in:
GemPlugin, Plugin
Defined in:
lib/engines/plugin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/engines/plugin.rb', line 16

def self.included(base)
  # Plugins can add code paths to this attribute in init.rb if they 
  # need plugin directories to be added to the load path, i.e.
  #
  #   plugin.code_paths << 'app/other_classes'
  #
  # Defaults to ["app/controllers", "app/helpers", "app/models", "components"]
  base.send :attr_accessor, :code_paths

  # Plugins can add paths to this attribute in init.rb if they need
  # controllers loaded from additional locations. 
  base.send :attr_accessor, :controller_paths

  # The directory in this plugin to mirror into the shared directory
  # under +public+.
  #
  # Defaults to "assets" (see default_public_directory).
  base.send :attr_accessor, :public_directory
  
  base.alias_method_chain :initialize, :default_paths   
end

Instance Method Details

#add_plugin_view_pathsObject



96
97
98
99
100
101
102
# File 'lib/engines/plugin.rb', line 96

def add_plugin_view_paths
  view_path = File.join(directory, 'app', 'views')
  if File.exist?(view_path)
    ActionController::Base.view_paths.insert(1, view_path) # push it just underneath the app
    ActionView::TemplateFinder.process_view_paths(view_path)
  end
end

#initialize_with_default_paths(*args) ⇒ Object



69
70
71
72
# File 'lib/engines/plugin.rb', line 69

def initialize_with_default_paths(*args)
  initialize_without_default_paths(*args)
  initialize_default_paths
end

#latest_migrationObject

Returns the version number of the latest migration for this plugin. Returns nil if this plugin has no migrations.



121
122
123
124
125
# File 'lib/engines/plugin.rb', line 121

def latest_migration
  migrations = Dir[migration_directory+"/*.rb"]
  return nil if migrations.empty?
  migrations.map { |p| File.basename(p) }.sort.last.match(/0*(\d+)\_/)[1].to_i
end

#load(initializer) ⇒ Object

Extends the superclass’ load method to additionally mirror public assets



83
84
85
86
87
88
# File 'lib/engines/plugin.rb', line 83

def load(initializer)
  return if loaded?
  super initializer
  add_plugin_view_paths
  Assets.mirror_files_for(self)
end

#load_pathsObject

Returns a list of paths this plugin wishes to make available in $LOAD_PATH

Overwrites the correspondend method in the superclass



77
78
79
80
# File 'lib/engines/plugin.rb', line 77

def load_paths
  report_nonexistant_or_empty_plugin! unless valid?
  select_existing_paths :code_paths
end

#migrate(version = nil) ⇒ Object

Migrate this plugin to the given version. See Engines::Plugin::Migrator for more information.



129
130
131
# File 'lib/engines/plugin.rb', line 129

def migrate(version = nil)
  Engines::Plugin::Migrator.migrate_plugin(self, version)
end

#migration_directoryObject

The directory containing this plugin’s migrations (plugin/db/migrate)



115
116
117
# File 'lib/engines/plugin.rb', line 115

def migration_directory
  File.join(self.directory, 'db', 'migrate')
end

#public_asset_directoryObject

The path to this plugin’s public files



105
106
107
# File 'lib/engines/plugin.rb', line 105

def public_asset_directory
  "#{File.basename(Engines.public_directory)}/#{name}"
end

#routes_pathObject

The path to this plugin’s routes file



110
111
112
# File 'lib/engines/plugin.rb', line 110

def routes_path
  File.join(directory, "routes.rb")
end

#select_existing_paths(name) ⇒ Object

for code_paths and controller_paths select those paths that actually exist in the plugin’s directory



92
93
94
# File 'lib/engines/plugin.rb', line 92

def select_existing_paths(name)
  Engines.select_existing_paths(self.send(name).map { |p| File.join(directory, p) })
end