tog-desert 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/CHANGES +28 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +295 -0
  4. data/Rakefile +100 -0
  5. data/generators/desert_plugin/USAGE +14 -0
  6. data/generators/desert_plugin/desert_plugin_generator.rb +73 -0
  7. data/generators/desert_plugin/templates/empty_file +0 -0
  8. data/generators/desert_plugin/templates/plugin_migration.rb +11 -0
  9. data/generators/desert_plugin/templates/routes.rb +4 -0
  10. data/generators/desert_plugin/templates/spec_helper.rb +8 -0
  11. data/init.rb +0 -0
  12. data/lib/desert/manager.rb +116 -0
  13. data/lib/desert/plugin.rb +74 -0
  14. data/lib/desert/plugin_migrations/extensions/1.0/schema_statements.rb +34 -0
  15. data/lib/desert/plugin_migrations/extensions/2.1/schema_statements.rb +12 -0
  16. data/lib/desert/plugin_migrations/extensions/schema_statements.rb +6 -0
  17. data/lib/desert/plugin_migrations/migrator.rb +54 -0
  18. data/lib/desert/plugin_migrations.rb +3 -0
  19. data/lib/desert/plugin_templates/1.2.0/action_mailer.rb +21 -0
  20. data/lib/desert/plugin_templates/1.2.0/action_view.rb +53 -0
  21. data/lib/desert/plugin_templates/1.99.0/action_mailer.rb +25 -0
  22. data/lib/desert/plugin_templates/1.99.0/action_view.rb +38 -0
  23. data/lib/desert/plugin_templates/2.0.0/action_mailer.rb +23 -0
  24. data/lib/desert/plugin_templates/2.0.2/action_view.rb +26 -0
  25. data/lib/desert/plugin_templates/2.1.0/action_view.rb +13 -0
  26. data/lib/desert/plugin_templates/action_controller.rb +14 -0
  27. data/lib/desert/plugin_templates/action_view.rb +16 -0
  28. data/lib/desert/plugin_templates/edge/action_view.rb +10 -0
  29. data/lib/desert/plugin_templates.rb +10 -0
  30. data/lib/desert/rails/1.2.0/initializer.rb +20 -0
  31. data/lib/desert/rails/2.0.0/plugin.rb +22 -0
  32. data/lib/desert/rails/dependencies.rb +87 -0
  33. data/lib/desert/rails/migration.rb +36 -0
  34. data/lib/desert/rails/observer.rb +22 -0
  35. data/lib/desert/rails/route_set.rb +23 -0
  36. data/lib/desert/rails.rb +11 -0
  37. data/lib/desert/ruby/object.rb +34 -0
  38. data/lib/desert/ruby.rb +2 -0
  39. data/lib/desert/supported_rails_versions.rb +9 -0
  40. data/lib/desert/version_checker.rb +26 -0
  41. data/lib/desert.rb +14 -0
  42. metadata +95 -0
@@ -0,0 +1,74 @@
1
+ module Desert
2
+ class Plugin # nodoc
3
+ attr_reader :name, :path
4
+ def initialize(path)
5
+ @path = File.expand_path(path)
6
+ @name = File.basename(@path)
7
+ end
8
+
9
+ def migration_path
10
+ "#{@path}/db/migrate"
11
+ end
12
+
13
+ # The path to the views for this plugin
14
+ def templates_path
15
+ "#{@path}/app/views"
16
+ end
17
+
18
+ def controllers_path
19
+ "#{@path}/app/controllers"
20
+ end
21
+
22
+ # TODO: Test me
23
+ def models_path
24
+ "#{@path}/app/models"
25
+ end
26
+
27
+ # TODO: Test me
28
+ def helpers_path
29
+ "#{@path}/app/helpers"
30
+ end
31
+
32
+ # The path to the layout for this plugin
33
+ def layouts_path
34
+ "#{templates_path}/layouts"
35
+ end
36
+
37
+ # Finds a template with the specified path
38
+ def find_template(template)
39
+ template_path = "#{templates_path}/#{template}"
40
+ File.exists?(template_path) ? template_path : nil
41
+ end
42
+
43
+ def framework_paths
44
+ # TODO: Don't include dirs for frameworks that are not used
45
+ %w(
46
+ railties
47
+ railties/lib
48
+ actionpack/lib
49
+ activesupport/lib
50
+ activerecord/lib
51
+ actionmailer/lib
52
+ actionwebservice/lib
53
+ ).map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
54
+ end
55
+
56
+ def ==(other)
57
+ self.path == other.path
58
+ end
59
+
60
+ def migration
61
+ @migration ||= PluginMigrations::Migrator.new(:up, migration_path)
62
+ end
63
+
64
+ def with_current_plugin
65
+ old_plugin = PluginMigrations::Migrator.current_plugin
66
+ begin
67
+ PluginMigrations::Migrator.current_plugin = self
68
+ yield
69
+ ensure
70
+ PluginMigrations::Migrator.current_plugin = old_plugin
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,34 @@
1
+ ActiveRecord::ConnectionAdapters::SchemaStatements.module_eval do
2
+ def initialize_schema_information_with_plugins
3
+ initialize_schema_information_without_plugins
4
+
5
+ begin
6
+ execute "CREATE TABLE #{Desert::PluginMigrations::Migrator.schema_info_table_name} (plugin_name #{type_to_sql(:string)}, version #{type_to_sql(:integer)})"
7
+ rescue ActiveRecord::StatementInvalid
8
+ # Schema has been initialized
9
+ end
10
+ end
11
+ alias_method_chain :initialize_schema_information, :plugins
12
+
13
+ def dump_schema_information_with_plugins
14
+ schema_information = []
15
+
16
+ dump = dump_schema_information_without_plugins
17
+ schema_information << dump if dump
18
+
19
+ begin
20
+ plugins = ActiveRecord::Base.connection.select_all("SELECT * FROM #{Desert::PluginMigrations::Migrator.schema_info_table_name}")
21
+ plugins.each do |plugin|
22
+ if (version = plugin['version'].to_i) > 0
23
+ plugin_name = ActiveRecord::Base.quote_value(plugin['plugin_name'])
24
+ schema_information << "INSERT INTO #{Desert::PluginMigrations::Migrator.schema_info_table_name} (plugin_name, version) VALUES (#{plugin_name}, #{version})"
25
+ end
26
+ end
27
+ rescue ActiveRecord::StatementInvalid
28
+ # No Schema Info
29
+ end
30
+
31
+ schema_information.join(";\n")
32
+ end
33
+ alias_method_chain :dump_schema_information, :plugins
34
+ end
@@ -0,0 +1,12 @@
1
+ ActiveRecord::ConnectionAdapters::SchemaStatements.module_eval do
2
+ def initialize_schema_migrations_table_with_plugins
3
+ initialize_schema_migrations_table_without_plugins
4
+
5
+ begin
6
+ execute "CREATE TABLE #{Desert::PluginMigrations::Migrator.schema_info_table_name} (plugin_name #{type_to_sql(:string)}, version #{type_to_sql(:integer)})"
7
+ rescue ActiveRecord::StatementInvalid
8
+ # Schema has been initialized
9
+ end
10
+ end
11
+ alias_method_chain :initialize_schema_migrations_table, :plugins
12
+ end
@@ -0,0 +1,6 @@
1
+ dir = File.dirname(__FILE__)
2
+ if ActiveRecord::ConnectionAdapters::SchemaStatements.instance_methods.include?('initialize_schema_information')
3
+ require "#{dir}/1.0/schema_statements"
4
+ else
5
+ require "#{dir}/2.1/schema_statements"
6
+ end
@@ -0,0 +1,54 @@
1
+ module Desert #:nodoc:
2
+ module PluginMigrations
3
+ # Responsible for migrating plugins. PluginMigrations.Migrator.current_plugin
4
+ # indicates which plugin is currently being migrated
5
+ class Migrator < ActiveRecord::Migrator
6
+ # We need to be able to set the current plugin being migrated.
7
+ cattr_accessor :current_plugin
8
+
9
+ class << self
10
+ # Runs the migrations from a plugin, up (or down) to the version given
11
+ def migrate_plugin(plugin, version = nil)
12
+ self.current_plugin = plugin
13
+ if ActiveRecord::Base.connection.respond_to?(:initialize_schema_migrations_table)
14
+ ActiveRecord::Base.connection.initialize_schema_migrations_table
15
+ end
16
+ migrate(plugin.migration_path, version)
17
+ end
18
+
19
+ def schema_info_table_name #:nodoc:
20
+ ActiveRecord::Base.table_name_prefix + 'plugin_schema_info' + ActiveRecord::Base.table_name_suffix
21
+ end
22
+ alias_method :schema_migrations_table_name, :schema_info_table_name
23
+
24
+ def current_version #:nodoc:
25
+ result = ActiveRecord::Base.connection.select_one("SELECT version FROM #{schema_info_table_name} WHERE plugin_name = '#{current_plugin.name}'")
26
+ if result
27
+ result['version'].to_i
28
+ else
29
+ # There probably isn't an entry for this plugin in the migration info table.
30
+ 0
31
+ end
32
+ end
33
+ end
34
+
35
+ def set_schema_version(version)
36
+ version = down? ? version.to_i - 1 : version.to_i
37
+
38
+ if ActiveRecord::Base.connection.select_one("SELECT version FROM #{self.class.schema_info_table_name} WHERE plugin_name = '#{current_plugin.name}'").nil?
39
+ # We need to create the entry since it doesn't exist
40
+ ActiveRecord::Base.connection.execute("INSERT INTO #{self.class.schema_info_table_name} (version, plugin_name) VALUES (#{version},'#{current_plugin.name}')")
41
+ else
42
+ ActiveRecord::Base.connection.update("UPDATE #{self.class.schema_info_table_name} SET version = #{version} WHERE plugin_name = '#{current_plugin.name}'")
43
+ end
44
+ end
45
+ alias_method :record_version_state_after_migrating, :set_schema_version
46
+
47
+
48
+ def migrated
49
+ current_plugin_version = self.class.current_version
50
+ (1..current_plugin_version).to_a
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/plugin_migrations/migrator"
3
+ require "#{dir}/plugin_migrations/extensions/schema_statements"
@@ -0,0 +1,21 @@
1
+ module ActionMailer #:nodoc
2
+ class Base #:nodoc:
3
+ private
4
+ def template_path_with_plugin_routing
5
+ template_paths = [template_path_without_plugin_routing]
6
+ Desert::Manager.plugins.reverse.each do |plugin|
7
+ template_paths << "#{plugin.templates_path}/#{mailer_name}"
8
+ end
9
+ "{#{template_paths * ','}}"
10
+ end
11
+ alias_method_chain :template_path, :plugin_routing
12
+
13
+ def initialize_template_class(assigns)
14
+ view_path = File.dirname(Dir["#{template_path}/#{@template}.*"].first)
15
+ returning(template = ActionView::Base.new(view_path, assigns, self)) do
16
+ template.extend ApplicationHelper
17
+ template.extend self.class.master_helper_module
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,53 @@
1
+ module ActionView
2
+ class Base
3
+ attr_reader :view_paths
4
+ def initialize_with_desert(base_path = nil, assigns_for_first_render = {}, controller = nil)
5
+ initialize_without_desert(base_path, assigns_for_first_render, controller)
6
+
7
+ @view_paths = [base_path]
8
+ Desert::Manager.plugins_and_app.reverse.each do |plugin|
9
+ @view_paths << plugin.templates_path
10
+ end
11
+ end
12
+ alias_method_chain :initialize, :desert
13
+
14
+ private
15
+ def full_path_template_exists?(path, extension)
16
+ file_path = "#{path}.#{extension}"
17
+ @@method_names.has_key?(file_path) || FileTest.exists?(file_path)
18
+ end
19
+
20
+ def find_template_extension_for(template_path)
21
+ view_paths.each do |view_path|
22
+ full_path = "#{view_path}/#{template_path}"
23
+ if match = @@template_handlers.find { |k,| full_path_template_exists?(template_path, k) }
24
+ return match.first.to_sym
25
+ elsif full_path_template_exists?(full_path, :rhtml)
26
+ return :rhtml
27
+ elsif full_path_template_exists?(full_path, :rxml)
28
+ return :rxml
29
+ elsif full_path_template_exists?(full_path, :rjs)
30
+ return :rjs
31
+ end
32
+ end
33
+ raise ActionViewError, "No rhtml, rxml, rjs or delegate template found for #{template_path} in #{@base_path}"
34
+ end
35
+
36
+ def full_template_path_with_plugin_routing(template_path, extension)
37
+ full_template_path = full_template_path_without_plugin_routing(template_path, extension)
38
+
39
+ unless File.exist?(full_template_path)
40
+ # Look through the plugins for the template
41
+ Desert::Manager.plugins.reverse.each do |plugin|
42
+ if plugin_template_path = plugin.find_template("#{template_path}.#{extension}")
43
+ full_template_path = plugin_template_path
44
+ break
45
+ end
46
+ end
47
+ end
48
+
49
+ full_template_path
50
+ end
51
+ alias_method_chain :full_template_path, :plugin_routing
52
+ end
53
+ end
@@ -0,0 +1,25 @@
1
+ module ActionMailer #:nodoc
2
+ class Base #:nodoc:
3
+ private
4
+ def template_path_with_plugin_routing
5
+ result = nil
6
+ Desert::Manager.plugins_and_app.reverse.each do |plugin|
7
+ relative_path = "#{plugin.templates_path}/#{mailer_name}"
8
+ unless Dir["#{relative_path}/#{@template}.*"].empty?
9
+ result = relative_path
10
+ break
11
+ end
12
+ end
13
+ result || template_path_without_plugin_routing
14
+ end
15
+ alias_method_chain :template_path, :plugin_routing
16
+
17
+ def initialize_template_class(assigns)
18
+ view_path = File.dirname(Dir["#{template_path}/#{@template}.*"].first)
19
+ returning(template = ActionView::Base.new([view_path], assigns, self)) do
20
+ template.extend ApplicationHelper
21
+ template.extend self.class.master_helper_module
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ module ActionView #:nodoc:
2
+ class Base #:nodoc:
3
+ def initialize_with_desert_plugins(*args)
4
+ initialize_without_desert_plugins *args
5
+
6
+ Desert::Manager.plugins.reverse.each do |plugin|
7
+ view_paths << plugin.templates_path
8
+ end
9
+ end
10
+ alias_method_chain :initialize, :desert_plugins
11
+
12
+ def find_template_extension_from_handler(template_path, formatted = nil)
13
+ checked_template_path = formatted ? "#{template_path}.#{template_format}" : template_path
14
+
15
+ view_paths.each do |view_path|
16
+ template_handler_preferences.each do |template_type|
17
+ extensions =
18
+ case template_type
19
+ when :javascript
20
+ [:rjs]
21
+ when :delegate
22
+ @@template_handlers.keys
23
+ else
24
+ [template_type]
25
+ end
26
+
27
+ extensions.each do |extension|
28
+ file_path = File.join(view_path, "#{checked_template_path}.#{extension}")
29
+ if File.exist?(file_path)
30
+ return formatted ? "#{template_format}.#{extension}" : extension.to_s
31
+ end
32
+ end
33
+ end
34
+ end
35
+ nil
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,23 @@
1
+ module ActionMailer #:nodoc
2
+ class Base #:nodoc:
3
+ private
4
+ def template_path_with_plugin_routing
5
+ template_paths = [template_path_without_plugin_routing]
6
+ Desert::Manager.plugins.reverse.each do |plugin|
7
+ template_paths << "#{plugin.templates_path}/#{mailer_name}"
8
+ end
9
+ "{#{template_paths * ','}}"
10
+ end
11
+ alias_method_chain :template_path, :plugin_routing
12
+
13
+ def initialize_template_class(assigns)
14
+ view_paths = Dir[template_path].collect do |path|
15
+ File.dirname(path)
16
+ end
17
+ returning(template = ActionView::Base.new(view_paths, assigns, self)) do
18
+ template.extend ApplicationHelper
19
+ template.extend self.class.master_helper_module
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module ActionView #:nodoc:
2
+ class Base #:nodoc:
3
+ def initialize_with_desert_plugins(*args)
4
+ initialize_without_desert_plugins *args
5
+
6
+ Desert::Manager.plugins.reverse.each do |plugin|
7
+ append_view_path plugin.templates_path
8
+ end
9
+ end
10
+ alias_method_chain :initialize, :desert_plugins
11
+
12
+ def find_template_extension_from_handler(template_path, formatted = nil)
13
+ checked_template_path = formatted ? "#{template_path}.#{template_format}" : template_path
14
+
15
+ view_paths.each do |view_path|
16
+ self.class.template_handler_extensions.each do |extension|
17
+ file_path = File.join(view_path, "#{checked_template_path}.#{extension}")
18
+ if File.exist?(file_path)
19
+ return formatted ? "#{template_format}.#{extension}" : extension.to_s
20
+ end
21
+ end
22
+ end
23
+ nil
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ module ActionView #:nodoc:
2
+ class TemplateFinder #:nodoc:
3
+ def initialize_with_desert_plugins(base, *paths)
4
+ self.class.process_view_paths(*paths)
5
+ initialize_without_desert_plugins base, *paths
6
+
7
+ Desert::Manager.plugins.reverse.each do |plugin|
8
+ append_view_path plugin.templates_path
9
+ end
10
+ end
11
+ alias_method_chain :initialize, :desert_plugins
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module ActionController #:nodoc:
2
+ module Layout #:nodoc:
3
+ module ClassMethods #:nodoc:
4
+ private
5
+ def layout_list_with_plugin_routing
6
+ plugin_layouts = Desert::Manager.layout_paths.join(",")
7
+ plugin_layouts.empty? ?
8
+ layout_list_without_plugin_routing :
9
+ layout_list_without_plugin_routing + Dir["{#{plugin_layouts}}/**/*"]
10
+ end
11
+ alias_method_chain :layout_list, :plugin_routing
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ dir = File.dirname(__FILE__)
2
+ if ActionView.const_defined?(:TemplateFinder)
3
+ require "#{dir}/2.1.0/action_view"
4
+ else
5
+ if ActionView::Base.private_instance_methods.include?('find_template_extension_from_handler')
6
+ if ActionView::Base.instance_methods.include?('template_handler_preferences')
7
+ require "#{dir}/1.99.0/action_view"
8
+ else
9
+ require "#{dir}/2.0.2/action_view"
10
+ end
11
+ elsif ActionView.const_defined?(:PathSet)
12
+ require "#{dir}/edge/action_view"
13
+ else
14
+ require "#{dir}/1.2.0/action_view"
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ ActionView::Base.class_eval do
2
+ def initialize_with_desert_plugins(*args)
3
+ initialize_without_desert_plugins *args
4
+
5
+ Desert::Manager.plugins.reverse.each do |plugin|
6
+ view_paths << plugin.templates_path
7
+ end
8
+ end
9
+ alias_method_chain :initialize, :desert_plugins
10
+ end
@@ -0,0 +1,10 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "#{dir}/plugin_templates/action_controller"
3
+ if Desert::VersionChecker.rails_version_is_below_1990?
4
+ require "#{dir}/plugin_templates/1.2.0/action_mailer"
5
+ elsif Desert::VersionChecker.rails_version_is_below_rc2?
6
+ require "#{dir}/plugin_templates/1.99.0/action_mailer"
7
+ else
8
+ require "#{dir}/plugin_templates/2.0.0/action_mailer"
9
+ end
10
+ require "#{dir}/plugin_templates/action_view"
@@ -0,0 +1,20 @@
1
+ module Rails
2
+ class Initializer
3
+ def load_plugin_with_desert(directory)
4
+ return if Desert::Manager.plugin_exists?(directory)
5
+ plugin = Desert::Manager.register_plugin(directory) do
6
+ load_plugin_without_desert(directory)
7
+ end
8
+ # TODO: Can we use Initializer::Configuration#default_load_paths to do this?
9
+ configuration.controller_paths << plugin.controllers_path
10
+ end
11
+ alias_method_chain :load_plugin, :desert
12
+
13
+ def require_plugin(plugin_name)
14
+ find_plugins(configuration.plugin_paths).sort.each do |path|
15
+ return load_plugin(path) if File.basename(path) == plugin_name
16
+ end
17
+ raise "Plugin '#{plugin_name}' does not exist"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ class Rails::Plugin
2
+ attr_accessor :initializer
3
+ def require_plugin(plugin_name)
4
+ initializer.configuration.plugin_locators.each do |locator|
5
+ locator.new(initializer).each do |plugin_loader|
6
+ return plugin_loader.load(initializer) if plugin_loader.name.to_s == plugin_name.to_s
7
+ end
8
+ end
9
+ raise "Plugin '#{plugin_name}' does not exist"
10
+ end
11
+
12
+ def load_with_desert(initializer)
13
+ @initializer = initializer
14
+ return if Desert::Manager.plugin_exists?(directory)
15
+ plugin = Desert::Manager.register_plugin(directory) do
16
+ load_without_desert(initializer)
17
+ end
18
+ # TODO: Can we use Initializer::Configuration#default_load_paths to do this?
19
+ initializer.configuration.controller_paths << plugin.controllers_path
20
+ end
21
+ alias_method_chain :load, :desert
22
+ end
@@ -0,0 +1,87 @@
1
+ dependencies = ActiveSupport.const_defined?(:Dependencies) ? ActiveSupport::Dependencies : Dependencies
2
+ dependencies.module_eval do
3
+ def load_missing_constant_with_desert(from_mod, const_name)
4
+ from_mod = guard_against_anonymous_module(from_mod)
5
+ qualified_name = qualified_name_for from_mod, const_name
6
+ path_suffix = qualified_name.underscore
7
+
8
+ if define_constant_with_desert_loading(from_mod, const_name, qualified_name, path_suffix)
9
+ return from_mod.const_get(const_name)
10
+ end
11
+
12
+ if has_parent_module?(from_mod)
13
+ look_for_constant_in_parent_module(from_mod, const_name, qualified_name, path_suffix)
14
+ else
15
+ raise NameError, "Constant #{qualified_name} from #{path_suffix}.rb not found"
16
+ end
17
+ end
18
+ alias_method_chain :load_missing_constant, :desert
19
+
20
+ def load_once_path?(path)
21
+ load_once_paths.any? { |base| File.expand_path(path).starts_with? File.expand_path(base) }
22
+ end
23
+
24
+ def depend_on_with_desert(file_name, swallow_load_errors = false)
25
+ successfully_loaded_in_plugin = false
26
+ Desert::Manager.files_on_load_path(file_name).each do |file|
27
+ require_or_load(file)
28
+ successfully_loaded_in_plugin = true
29
+ end
30
+ begin
31
+ unless successfully_loaded_in_plugin
32
+ require_or_load file_name
33
+ loaded << File.expand_path(file_name)
34
+ end
35
+ rescue LoadError
36
+ if !swallow_load_errors && !successfully_loaded_in_plugin
37
+ raise
38
+ end
39
+ end
40
+ end
41
+ alias_method_chain :depend_on, :desert
42
+
43
+ protected
44
+ def define_constant_with_desert_loading(from_mod, const_name, qualified_name, path_suffix)
45
+ define_constant_from_file(from_mod, const_name, qualified_name, path_suffix) ||
46
+ define_constant_from_directory(from_mod, const_name, qualified_name, path_suffix)
47
+ end
48
+
49
+ def has_parent_module?(mod)
50
+ mod != Object
51
+ end
52
+
53
+ def look_for_constant_in_parent_module(from_mod, const_name, qualified_name, path_suffix)
54
+ return from_mod.parent.const_missing(const_name)
55
+ rescue NameError => e
56
+ raise NameError, "Constant #{qualified_name} from #{path_suffix}.rb not found\n#{e.message}"
57
+ end
58
+
59
+ def guard_against_anonymous_module(from_mod)
60
+ return Object if from_mod.name.blank?
61
+ return from_mod
62
+ end
63
+
64
+ def define_constant_from_file(from_mod, const_name, qualified_name, path_suffix)
65
+ files = Desert::Manager.files_on_load_path(path_suffix)
66
+ files.each do |file|
67
+ require_or_load file
68
+ loaded << file.gsub(/\.rb$/, '')
69
+ next if autoloaded_constants.include?(qualified_name)
70
+ next if load_once_path?(file)
71
+ autoloaded_constants << qualified_name
72
+ end
73
+ loaded << File.expand_path(path_suffix)
74
+ from_mod.const_defined?(const_name)
75
+ end
76
+
77
+ def define_constant_from_directory(from_mod, const_name, qualified_name, path_suffix)
78
+ return false unless Desert::Manager.directory_on_load_path?(path_suffix)
79
+
80
+ if autoloaded_constants.include?(qualified_name)
81
+ raise "Constant #{qualified_name} is already autoloaded but is not defined as a constant"
82
+ end
83
+ from_mod.const_set(const_name, Module.new)
84
+ autoloaded_constants << qualified_name
85
+ return true
86
+ end
87
+ end
@@ -0,0 +1,36 @@
1
+ class ActiveRecord::Migration
2
+ module DesertMigration
3
+ def migrate_plugin(plugin_name, version)
4
+ plugin = find_plugin(plugin_name)
5
+ Desert::PluginMigrations::Migrator.migrate_plugin(
6
+ plugin,
7
+ version
8
+ )
9
+ end
10
+
11
+ protected
12
+ def find_plugin(plugin_name)
13
+ plugin = Desert::Manager.find_plugin(plugin_name.to_s)
14
+ return plugin if plugin
15
+ raise ArgumentError, "No plugin found named #{plugin_name}"
16
+ end
17
+
18
+ def table_exists?(table_name)
19
+ vals = select_all("DESC #{table_name}")
20
+ return true
21
+ rescue ActiveRecord::StatementInvalid
22
+ return false
23
+ end
24
+
25
+ def column_exists?(table_name, column_name)
26
+ val = select_one("select #{column_name} from #{table_name}")
27
+ return true
28
+ rescue ActiveRecord::StatementInvalid
29
+ return false
30
+ end
31
+ def table_exists?(table_name)
32
+ ActiveRecord::Base.connection.tables.include? table_name
33
+ end
34
+ end
35
+ extend DesertMigration
36
+ end
@@ -0,0 +1,22 @@
1
+ module Desert
2
+ module Rails
3
+ module Observer
4
+ def self.observers=(*observers)
5
+ @observers = observers.flatten
6
+ end
7
+ def self.observers
8
+ @observers ||= []
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ class Rails::Initializer
15
+ def load_observers_with_desert
16
+ # todo verify if this should be logged
17
+ puts "Adding #{Desert::Rails::Observer.observers.inspect} to #{ActiveRecord::Base.observers.inspect}"
18
+ ActiveRecord::Base.observers += Desert::Rails::Observer.observers.uniq
19
+ load_observers_without_desert
20
+ end
21
+ alias_method_chain :load_observers, :desert
22
+ end
@@ -0,0 +1,23 @@
1
+ module Desert
2
+ module Rails
3
+ module RouteSet
4
+ # Loads the set of routes from within a plugin and evaluates them at this
5
+ # point within an application's main <tt>routes.rb</tt> file.
6
+ #
7
+ # Plugin routes are loaded from <tt><plugin_root>/routes.rb</tt>.
8
+ def routes_from_plugin(name)
9
+ name = name.to_s
10
+ routes_path = File.join(
11
+ Desert::Manager.plugin_path(name),
12
+ "config/routes.rb"
13
+ )
14
+ RAILS_DEFAULT_LOGGER.debug "Loading routes from #{routes_path}."
15
+ eval(IO.read(routes_path), binding, routes_path) if File.file?(routes_path)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class ActionController::Routing::RouteSet::Mapper
22
+ include Desert::Rails::RouteSet
23
+ end