Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' into work
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Adams committed Dec 23, 2008
2 parents 0e3d2b6 + f5cd38e commit fda3ccf
Show file tree
Hide file tree
Showing 28 changed files with 209 additions and 178 deletions.
7 changes: 7 additions & 0 deletions vendor/plugins/engines/CHANGELOG
@@ -1,5 +1,12 @@
= EDGE

* Samuel Williams (http://www.oriontransfer.co.nz/):
Thanks to Tekin for his patches.
Updated migrations system to tie in more closely with the current rails mechanism.
Rake task for updating database schema info
rake db:migrate:upgrade_plugin_migrations
Please see http://engines.lighthouseapp.com/projects/10178-engines-plugin/tickets/17 for more information.

* Refactored the view loading to work with changes in Edge Rails

* Fixed integration of plugin migrations with the new, default timestamped migrations in Edge Rails
Expand Down
4 changes: 3 additions & 1 deletion vendor/plugins/engines/Rakefile
Expand Up @@ -92,7 +92,7 @@ namespace :test do
run ["cd #{vendor_dir}",
"mkdir rails",
"cd rails",
"curl -s -L http://github.com/rails/rails/tarball/v2.1.0 | tar xzv --strip-components 1"]
"curl -s -L http://github.com/rails/rails/tarball/#{ENV['RAILS']} | tar xzv --strip-components 1"]
else
out.puts " Cloning Rails Tag #{ENV['RAILS']} from GitHub (can be slow - set CURL=true to use curl)"
run ["cd #{vendor_dir}",
Expand Down Expand Up @@ -124,6 +124,8 @@ namespace :test do
h[env] = {"adapter" => "sqlite3", "database" => "engines_#{env}.sqlite3"} ; h
end.to_yaml)
end
out.puts " installing exception_notification plugin"
run "cd #{test_app_dir} && ./script/plugin install exception_notification"
end
end

Expand Down
5 changes: 2 additions & 3 deletions vendor/plugins/engines/boot.rb
@@ -1,8 +1,7 @@
begin
require 'rails/version'
unless Rails::VERSION::MAJOR >= 2 ||
(Rails::VERSION::MAJOR >= 1 && Rails::VERSION::MINOR >= 99)
raise "This version of the engines plugin requires Rails 2.0 or later!"
unless Rails::VERSION::MAJOR >= 2 && Rails::VERSION::MINOR >= 2 && Rails::VERSION::TINY >= 0
raise "This version of the engines plugin requires Rails 2.2.0 or later!"
end
end

Expand Down
Expand Up @@ -2,11 +2,15 @@
# within the database.
class PluginMigrationGenerator < Rails::Generator::Base

# 255 characters max for Windows NTFS (http://en.wikipedia.org/wiki/Filename)
# minus 14 for timestamp, minus some extra chars for dot, underscore, file
# extension. So let's have 230.
MAX_FILENAME_LENGTH = 230

def initialize(runtime_args, runtime_options={})
super
@options = {:assigns => {}}

ensure_plugin_schema_table_exists
ensure_schema_table_exists
get_plugins_to_migrate(runtime_args)

if @plugins_to_migrate.empty?
Expand All @@ -25,10 +29,9 @@ def manifest
end

protected

# Create the plugin schema table if it doesn't already exist. See
# Engines::RailsExtensions::Migrations#initialize_schema_migrations_table_with_engine_additions
def ensure_plugin_schema_table_exists

# Create the schema table if it doesn't already exist.
def ensure_schema_table_exists
ActiveRecord::Base.connection.initialize_schema_migrations_table
end

Expand Down Expand Up @@ -69,11 +72,27 @@ def get_plugins_to_migrate(plugin_names)
@options[:assigns][:current_versions] = @current_versions
end

# Construct a unique migration name based on the plugins involved and the
# versions they should reach after this migration is run.
# Returns a migration name. If the descriptive migration name based on the
# plugin names involved is shorter than 230 characters that one will be
# used. Otherwise a shorter name will be returned.
def build_migration_name
returning descriptive_migration_name do |name|
name.replace short_migration_name if name.length > MAX_FILENAME_LENGTH
end
end

# Construct a unique migration name based on the plugins involved and the
# versions they should reach after this migration is run. The name constructed
# needs to be lowercase
def descriptive_migration_name
@plugins_to_migrate.map do |plugin|
"#{plugin.name}_to_version_#{@new_versions[plugin.name]}"
end.join("_and_")
end
end.join("_and_").downcase
end

# Short migration name that will be used if the descriptive_migration_name
# exceeds 230 characters
def short_migration_name
'plugin_migrations'
end
end
2 changes: 1 addition & 1 deletion vendor/plugins/engines/lib/engines.rb
Expand Up @@ -144,7 +144,7 @@ def mirror_files_from(source, destination)
source_files -= source_dirs

unless source_files.empty?
base_target_dir = File.join(destination, File.dirname(source_files.first))
base_target_dir = File.join(destination, File.dirname(source_files.first).gsub(source, ''))
FileUtils.mkdir_p(base_target_dir)
end

Expand Down
2 changes: 0 additions & 2 deletions vendor/plugins/engines/lib/engines/assets.rb
Expand Up @@ -13,7 +13,6 @@ class << self
def initialize_base_public_directory
dir = Engines.public_directory
unless File.exist?(dir)
Engines.logger.debug "Creating public engine files directory '#{dir}'"
FileUtils.mkdir_p(dir)
end
readme = File.join(dir, "README")
Expand All @@ -26,7 +25,6 @@ def initialize_base_public_directory
def mirror_files_for(plugin)
return if plugin.public_directory.nil?
begin
Engines.logger.debug "Attempting to copy plugin assets from '#{plugin.public_directory}' to '#{Engines.public_directory}'"
Engines.mirror_files_from(plugin.public_directory, File.join(Engines.public_directory, plugin.name))
rescue Exception => e
Engines.logger.warn "WARNING: Couldn't create the public file structure for plugin '#{plugin.name}'; Error follows:"
Expand Down
15 changes: 7 additions & 8 deletions vendor/plugins/engines/lib/engines/plugin.rb
Expand Up @@ -89,11 +89,6 @@ 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
if( Rails::VERSION::STRING =~ /\A2\.2\.\d/ )
ActionView::Base.process_view_paths(view_path)
else
ActionView::TemplateFinder.process_view_paths(view_path)
end
end
end

Expand All @@ -115,11 +110,15 @@ def migration_directory
# Returns the version number of the latest migration for this plugin. Returns
# nil if this plugin has no migrations.
def latest_migration
migrations.last
end

# Returns the version numbers of all migrations for this plugin.
def migrations
migrations = Dir[migration_directory+"/*.rb"]
return nil if migrations.empty?
migrations.map { |p| File.basename(p) }.sort.last.match(/0*(\d+)\_/)[1].to_i
migrations.map { |p| File.basename(p).match(/0*(\d+)\_/)[1].to_i }.sort
end

# Migrate this plugin to the given version. See Engines::Plugin::Migrator for more
# information.
def migrate(version = nil)
Expand Down
73 changes: 20 additions & 53 deletions vendor/plugins/engines/lib/engines/plugin/migrator.rb
Expand Up @@ -12,63 +12,30 @@ class Engines::Plugin::Migrator < ActiveRecord::Migrator
# We need to be able to set the 'current' engine being migrated.
cattr_accessor :current_plugin

# Runs the migrations from a plugin, up (or down) to the version given
def self.migrate_plugin(plugin, version)
self.current_plugin = plugin
# There seems to be a bug in Rails' own migrations, where migrating
# to the existing version causes all migrations to be run where that
# migration number doesn't exist (i.e. zero). We could fix this by
# removing the line if the version hits zero...?
return if current_version(plugin) == version
migrate(plugin.migration_directory, version)
end

# Returns the name of the table used to store schema information about
# installed plugins.
#
# See Engines.schema_info_table for more details.
def self.schema_info_table_name
proper_table_name Engines.schema_info_table
end

# Returns the current version of the given plugin
def self.current_version(plugin=current_plugin)
result = ActiveRecord::Base.connection.select_one(<<-ESQL
SELECT version FROM #{schema_info_table_name}
WHERE plugin_name = '#{plugin.name}'
ESQL
)
if result
result["version"].to_i
else
# There probably isn't an entry for this engine in the migration info table.
# We need to create that entry, and set the version to 0
ActiveRecord::Base.connection.execute(<<-ESQL
INSERT INTO #{schema_info_table_name} (version, plugin_name)
VALUES (0,'#{plugin.name}')
ESQL
)
0
class << self
# Runs the migrations from a plugin, up (or down) to the version given
def migrate_plugin(plugin, version)
self.current_plugin = plugin
return if current_version(plugin) == version
migrate(plugin.migration_directory, version)
end

def current_version(plugin=current_plugin)
# Delete migrations that don't match .. to_i will work because the number comes first
::ActiveRecord::Base.connection.select_values(
"SELECT version FROM #{schema_migrations_table_name}"
).delete_if{ |v| v.match(/-#{plugin.name}/) == nil }.map(&:to_i).max || 0
end
end

def migrated(plugin=current_plugin)
current = ActiveRecord::Base.connection.select_value(<<-ESQL
SELECT version FROM #{self.class.schema_info_table_name}
WHERE plugin_name = '#{plugin.name}'
ESQL
).to_i
current ? (1..current).to_a : []

def migrated
sm_table = self.class.schema_migrations_table_name
::ActiveRecord::Base.connection.select_values(
"SELECT version FROM #{sm_table}"
).delete_if{ |v| v.match(/-#{current_plugin.name}/) == nil }.map(&:to_i).sort
end

# Sets the version of the plugin in Engines::Plugin::Migrator.current_plugin to
# the given version.
def record_version_state_after_migrating(version)
ActiveRecord::Base.connection.update(<<-ESQL
UPDATE #{self.class.schema_info_table_name}
SET version = #{down? ? version.to_i - 1 : version.to_i}
WHERE plugin_name = '#{self.current_plugin.name}'
ESQL
)
super(version.to_s + "-" + current_plugin.name)
end
end
Expand Up @@ -16,9 +16,6 @@
module Engines::RailsExtensions::ActionMailer
def self.included(base) #:nodoc:
base.class_eval do
# TODO commented this out because it seems to break ActionMailer
# how can this be fixed?

alias_method_chain :template_path, :engine_additions
alias_method_chain :initialize_template_class, :engine_additions
end
Expand Down Expand Up @@ -70,7 +67,7 @@ def initialize_template_class_with_engine_additions(assigns)
#
# ActionView::Base.new(ActionController::Base.view_paths.dup, assigns, self)
renderer = initialize_template_class_without_engine_additions(assigns)
renderer.finder.view_paths.unshift(*ActionController::Base.view_paths.dup)
renderer.view_paths.unshift(*ActionController::Base.view_paths.dup)
renderer
end
end
Expand Down
Expand Up @@ -102,33 +102,26 @@ def require_or_load_with_engine_additions(file_name, const_path=nil)
# if we recognise this type
# (this regexp splits out the module/filename from any instances of app/#{type}, so that
# modules are still respected.)
if file_name =~ /^(.*app\/#{file_type}s\/)?(.*_#{file_type})(\.rb)?$/
if file_name =~ /^(.*app\/#{file_type}s\/)+(.*_#{file_type})(\.rb)?$/
base_name = $2
# ... go through the plugins from first started to last, so that
# code with a high precedence (started later) will override lower precedence
# implementations
Engines.plugins.each do |plugin|
plugin_file_name = File.expand_path(File.join(plugin.directory, 'app', "#{file_type}s", base_name))
#Engines.logger.debug("checking plugin '#{plugin.name}' for '#{base_name}'")
if File.file?("#{plugin_file_name}.rb")
Engines.logger.debug("==> loading from plugin '#{plugin.name}'")
file_loaded = true if require_or_load_without_engine_additions(plugin_file_name, const_path)
end
end

# finally, load any application-specific controller classes using the 'proper'
# rails load mechanism, EXCEPT when we're testing engines and could load this file
# from an engine
if Engines.disable_application_code_loading
Engines.logger.debug("loading from application disabled.")
else
unless Engines.disable_application_code_loading
# Ensure we are only loading from the /app directory at this point
app_file_name = File.join(RAILS_ROOT, 'app', "#{file_type}s", "#{base_name}")
if File.file?("#{app_file_name}.rb")
Engines.logger.debug("loading from application: #{base_name}")
file_loaded = true if require_or_load_without_engine_additions(app_file_name, const_path)
else
Engines.logger.debug("(file not found in application)")
end
end
end
Expand All @@ -140,6 +133,6 @@ def require_or_load_with_engine_additions(file_name, const_path=nil)
end
end

module ::Dependencies #:nodoc:
module ActiveSupport::Dependencies #:nodoc:
include Engines::RailsExtensions::Dependencies
end

0 comments on commit fda3ccf

Please sign in to comment.