Class: Engines::Plugin

Inherits:
Rails::Plugin
  • Object
show all
Defined in:
vendor/plugins/engines/lib/engines/plugin.rb,
vendor/plugins/engines/lib/engines/plugin/list.rb,
vendor/plugins/engines/lib/engines/plugin/loader.rb,
vendor/plugins/engines/lib/engines/plugin/locator.rb

Defined Under Namespace

Classes: FileSystemLocator, List, Loader, Migrator

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (Plugin) initialize(directory)

A new instance of Plugin



35
36
37
38
39
# File 'vendor/plugins/engines/lib/engines/plugin.rb', line 35

def initialize(directory)
  super directory
  @controller_paths = default_controller_paths
  @public_directory = default_public_directory
end

Instance Attribute Details

- (Object) controller_paths

Plugins can add paths to this attribute in init.rb if they need controllers loaded from additional locations.



12
13
14
# File 'vendor/plugins/engines/lib/engines/plugin.rb', line 12

def controller_paths
  @controller_paths
end

- (Object) public_directory

The directory in this plugin to mirror into the shared directory under public.

Defaults to "assets" (see default_public_directory).



18
19
20
# File 'vendor/plugins/engines/lib/engines/plugin.rb', line 18

def public_directory
  @public_directory
end

Instance Method Details

- (Object) add_plugin_locale_paths



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'vendor/plugins/engines/lib/engines/plugin.rb', line 54

def add_plugin_locale_paths
  locale_path = File.join(directory, 'locales')
  return unless File.exists?(locale_path)

  locale_files = Dir[File.join(locale_path, '*.{rb,yml}')]
  return if locale_files.blank?

  first_app_element = 
    I18n.load_path.select{ |e| e =~ /^#{ RAILS_ROOT }/ }.reject{ |e| e =~ /^#{ RAILS_ROOT }\/vendor\/plugins/ }.first
  app_index = I18n.load_path.index(first_app_element) || - 1

  I18n.load_path.insert(app_index, *locale_files)
end

- (Object) latest_migration

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



80
81
82
# File 'vendor/plugins/engines/lib/engines/plugin.rb', line 80

def latest_migration
  migrations.last
end

- (Object) load(initializer)

Extends the superclass’ load method to additionally mirror public assets



42
43
44
45
46
47
# File 'vendor/plugins/engines/lib/engines/plugin.rb', line 42

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

- (Object) migrate(version = nil)

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



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

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

- (Object) migration_directory

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



74
75
76
# File 'vendor/plugins/engines/lib/engines/plugin.rb', line 74

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

- (Object) migrations

Returns the version numbers of all migrations for this plugin.



85
86
87
88
# File 'vendor/plugins/engines/lib/engines/plugin.rb', line 85

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

- (Object) public_asset_directory

The path to this plugin’s public files



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

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

- (Object) select_existing_paths(name)

select those paths that actually exist in the plugin’s directory



50
51
52
# File 'vendor/plugins/engines/lib/engines/plugin.rb', line 50

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