Sha256: b340ae2cf8fd87a67c05cbd0a2538d7eb2006a9171362bc21c06bf280ae89488

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module PluginAWeek #:nodoc:
  module PluginMigrations
    module Extensions #:nodoc:
      # Adds migration/fixture support
      module Plugin
        # The location of the plugin's migrations
        def migration_path
          "#{root}/db/migrate"
        end

        # The current version of the plugin migrated in the database
        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

        # The latest version of the plugin according to the current migrations
        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

        # Migrate this plugin to the given version
        def migrate(version = nil)
          PluginAWeek::PluginMigrations::Migrator.migrate_plugin(self, version)
        end

        # The location of the plugin's fixtures
        def fixtures_path
          "#{root}/test/fixtures"
        end

        # The paths of all of the plugin's fixtures
        def fixtures(names = nil)
          names ||= '*'
          Dir["#{fixtures_path}/{#{names}}.{yml,csv}"].sort
        end

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

Plugin.class_eval do
  include PluginAWeek::PluginMigrations::Extensions::Plugin
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
plugin_migrations-0.1.2 lib/plugin_migrations/extensions/plugin.rb