Module: PluginAWeek::PluginMigrations::Extensions::Plugin

Defined in:
lib/plugin_migrations/extensions/plugin.rb

Overview

Adds migration/fixture support

Instance Method Summary collapse

Instance Method Details

#current_versionObject

The current version of the plugin migrated in the database



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/plugin_migrations/extensions/plugin.rb', line 12

def current_version
  begin
    if result = ActiveRecord::Base.connection.select_one("SELECT version FROM #{PluginAWeek::PluginMigrations::Migrator.schema_info_table_name} WHERE plugin_name = '#{name}'")
      result['version'].to_i
    else
      # There probably isn't an entry for this plugin in the migration info table.
      0
    end
  rescue ActiveRecord::StatementInvalid
    # No migraiton info table, so never migrated
    0
  end
end

#fixtures(names = nil) ⇒ Object

The paths of all of the plugin’s fixtures



43
44
45
46
# File 'lib/plugin_migrations/extensions/plugin.rb', line 43

def fixtures(names = nil)
  names ||= '*'
  Dir["#{fixtures_path}/{#{names}}.{yml,csv}"].sort
end

#fixtures_pathObject

The location of the plugin’s fixtures



38
39
40
# File 'lib/plugin_migrations/extensions/plugin.rb', line 38

def fixtures_path
  "#{root}/test/fixtures"
end

#latest_versionObject

The latest version of the plugin according to the current migrations



27
28
29
30
# File 'lib/plugin_migrations/extensions/plugin.rb', line 27

def latest_version
  migrations = Dir["#{migration_path}/[0-9]*_[_a-z0-9]*.rb"]
  migrations.map {|migration| File.basename(migration).match(/^([0-9]+)/)[1].to_i}.max || 0
end

#load_fixtures(*args) ⇒ Object

Loads the fixtures for the plugin



49
50
51
52
53
# File 'lib/plugin_migrations/extensions/plugin.rb', line 49

def load_fixtures(*args)
  fixtures(*args).each do |fixture|
    Fixtures.create_fixtures(File.dirname(fixture), File.basename(fixture, '.*'))
  end
end

#migrate(version = nil) ⇒ Object

Migrate this plugin to the given version



33
34
35
# File 'lib/plugin_migrations/extensions/plugin.rb', line 33

def migrate(version = nil)
  PluginAWeek::PluginMigrations::Migrator.migrate_plugin(self, version)
end

#migration_pathObject

The location of the plugin’s migrations



7
8
9
# File 'lib/plugin_migrations/extensions/plugin.rb', line 7

def migration_path
  "#{root}/db/migrate"
end