class RedmineActsAsTaggableOn::Migration

Public Instance Methods

down() click to toggle source
Calls superclass method
# File lib/redmine_acts_as_taggable_on/migration.rb, line 22
def down
  enforce_declarations!

  if ok_to_go_down?
    super
  else
    say 'Not dropping "tags" and "taggings" because they\re still needed by'
    say 'the following plugins:'
    plugins_still_using_tables.each { |p| say p.id, true }
  end
end
up() click to toggle source
Calls superclass method
# File lib/redmine_acts_as_taggable_on/migration.rb, line 11
def up
  enforce_declarations!
  check_for_old_style_plugins

  if ok_to_go_up?
    super
  else
    say 'Not creating "tags" and "taggings" because they already exist'
  end
end

Private Instance Methods

assert_schema_match!() click to toggle source
# File lib/redmine_acts_as_taggable_on/migration.rb, line 70
def assert_schema_match!
  if (obtain_structure('tags') != expected_tags_structure) ||
     (obtain_structure('taggings') != expected_taggings_structure)
    msg = "A plugin is already using the \"tags\" or \"taggings\" tables, and\n"
    msg << "the structure of the table does not match the structure expected\n"
    msg << "by #{current_plugin.id}.\n"
    raise SchemaMismatchError, msg
  end
end
check_for_old_style_plugins() click to toggle source

Check if any plugins are using acts-as-taggable-on directly; the purpose of this is only to print a warning if so.

# File lib/redmine_acts_as_taggable_on/migration.rb, line 54
def check_for_old_style_plugins
  Redmine::Plugin.all.each { |p| p.requires_acts_as_taggable_on? }
  nil
end
current_plugin() click to toggle source
# File lib/redmine_acts_as_taggable_on/migration.rb, line 48
def current_plugin
  Redmine::Plugin::Migrator.current_plugin
end
current_plugin_declaration_made?() click to toggle source
# File lib/redmine_acts_as_taggable_on/migration.rb, line 44
def current_plugin_declaration_made?
  current_plugin.requires_acts_as_taggable_on?
end
enforce_declarations!() click to toggle source
# File lib/redmine_acts_as_taggable_on/migration.rb, line 35
def enforce_declarations!
  unless current_plugin_declaration_made?
    msg = "You have to declare that you need redmine_acts_as_taggable_on inside\n"
    msg << "init.rb. See https://github.com/hdgarrood/redmine_acts_as_taggable_on\n"
    msg << "for more details.\n\n"
    fail msg
  end
end
expected_taggings_structure() click to toggle source
# File lib/redmine_acts_as_taggable_on/migration.rb, line 93
def expected_taggings_structure
  [
    ['tag_id', 'integer'],
    ['taggable_id', 'integer'],
    ['taggable_type', 'string'],
    ['tagger_id', 'integer'],
    ['tagger_type', 'string'],
    ['context', 'string'],
  ].sort
end
expected_tags_structure() click to toggle source
# File lib/redmine_acts_as_taggable_on/migration.rb, line 87
def expected_tags_structure
  [
    ['name', 'string']
  ]
end
obtain_structure(table_name) click to toggle source
# File lib/redmine_acts_as_taggable_on/migration.rb, line 80
def obtain_structure(table_name)
  ActiveRecord::Base.connection.columns(table_name).
    reject { |c| %w(created_at updated_at id).include? c.name }.
    map { |c| [c.name, c.type.to_s] }.
    sort
end
ok_to_go_down?() click to toggle source
# File lib/redmine_acts_as_taggable_on/migration.rb, line 112
def ok_to_go_down?
  plugins_still_using_tables.empty?
end
ok_to_go_up?() click to toggle source
# File lib/redmine_acts_as_taggable_on/migration.rb, line 59
def ok_to_go_up?
  tables_already_exist = %w(tags taggings).any? do |table|
    ActiveRecord::Base.connection.table_exists? table
  end
  if tables_already_exist
    assert_schema_match!
    return false
  end
  true
end
plugins_still_using_tables() click to toggle source

A list of plugins which are using the acts_as_taggable_on tables (excluding the current one)

# File lib/redmine_acts_as_taggable_on/migration.rb, line 106
def plugins_still_using_tables
  Redmine::Plugin.all.
    select(&:using_acts_as_taggable_on_tables?).
    reject {|p| p == Redmine::Plugin::Migrator.current_plugin }
end