Skip to content

Commit

Permalink
Radiant::Extension now inherits from Rails::Engine
Browse files Browse the repository at this point in the history
 - remove routing logic
 - deprecate `extension_config`
 - add each extension's lib directory to autoload paths
  • Loading branch information
mislav authored and saturnflyer committed Mar 15, 2012
1 parent 188a0ce commit 1298c10
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 67 deletions.
25 changes: 11 additions & 14 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

module Radiant
class Application < Rails::Application
require 'radiant/core_ext'

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down Expand Up @@ -62,21 +64,16 @@ class Application < Rails::Application
AdminUI.instance.load_default_nav
end

# FIXME: remove this after the extension system works again
config.autoload_paths += %W(
#{config.root}/vendor/extensions/archive/app/models
#{config.root}/vendor/extensions/archive/lib
#{config.root}/vendor/extensions/textile_filter/lib
)
require 'radiant/extension'
Dir["#{config.root}/vendor/extensions/*/*_extension.rb"].each do |extension|
require extension
end

config.to_prepare do
FileNotFoundPage
EnvDumpPage
ArchivePage
ArchiveYearIndexPage
ArchiveMonthIndexPage
ArchiveDayIndexPage
TextileFilter
Page.send(:include, TextileTags)
extensions = Radiant::Application.railties.engines.select { |e| e.is_a? Radiant::Extension }
extensions.each do |ext|
ext.activate if ext.respond_to? :activate
end
end

config.secret_token = "4ac217d6512aae25ea83a25d58c30bed06520ac20ff8040da552f88d3046cf9103c1a7ca21254c9fc64a6f3dd59e00e206e7c410d612390be23d834b48f7b1e8"
Expand Down
6 changes: 0 additions & 6 deletions config/initializers/core_ext.rb → lib/radiant/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ def to_name(last_part = '')
self.underscore.gsub('/', ' ').humanize.titlecase.gsub(/\s*#{last_part}$/, '')
end

unless methods.include?('parameterize')
def parameterize(sep = '-')
remove_formatting.downcase.replace_whitespace(sep).collapse(sep)
end
end

alias :to_slug :parameterize
alias :slugify :parameterize
alias :slugerize :parameterize
Expand Down
128 changes: 81 additions & 47 deletions lib/radiant/extension.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
require 'annotatable'
require 'simpleton'
require 'radiant/admin_ui'

Rails::Railtie::ABSTRACT_RAILTIES << 'Radiant::Extension'

module Radiant
class Extension
include Simpleton
include Annotatable
class Extension < ::Rails::Engine
# generate extension metadata accessors
[:version, :description, :url, :extension_name].each do |method|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{method}(value = nil)
value.nil?? @#{method} : (@#{method} = value)
end
RUBY
end

class Configuration < ::Rails::Engine::Configuration
def paths
super.tap do |p|
# declare that classes under all extension "lib" directories
# should auto-load and eager load (when applicable)
p.lib.autoload!
p.lib.eager_load!
end
end
end

module Configurable
def self.included(base)
base.class_eval do
include ::Rails::Engine::Configurable

annotate :version, :description, :url, :extension_name, :path
def self.config
@config ||= Configuration.new(find_root_with_flag("lib"))
end
end
end
end

attr_writer :active

Expand All @@ -27,29 +54,10 @@ def enabled?
active? and migrated?
end

# Conventional plugin-like routing
def routed?
File.exist?(routing_file)
end

def migrations_path
File.join(self.root, 'db', 'migrate')
end

def migrates_from
@migrates_from ||= {}
end

def routing_file
File.join(self.root, 'config', 'routes.rb')
end

def load_initializers
Dir["#{self.root}/config/initializers/**/*.rb"].sort.each do |initializer|
load(initializer)
end
end

def migrator
unless @migrator
extension = self
Expand Down Expand Up @@ -105,33 +113,48 @@ def extension_enabled?(extension)

class << self

def activate_extension
return if instance.active?
instance.activate if instance.respond_to? :activate
ActionController::Routing::Routes.add_configuration_file(instance.routing_file) if instance.routed?
ActionController::Routing::Routes.reload
instance.active = true
end
alias :activate :activate_extension
# def activate_extension
# return if instance.active?
# instance.activate if instance.respond_to? :activate
# ActionController::Routing::Routes.add_configuration_file(instance.routing_file) if instance.routed?
# ActionController::Routing::Routes.reload
# instance.active = true
# end
# alias :activate :activate_extension

def deactivate_extension
return unless instance.active?
instance.active = false
instance.deactivate if instance.respond_to? :deactivate
end
alias :deactivate :deactivate_extension
# def deactivate_extension
# return unless instance.active?
# instance.active = false
# instance.deactivate if instance.respond_to? :deactivate
# end

def define_routes(&block)
ActiveSupport::Deprecation.warn("define_routes has been deprecated in favor of your extension's config/routes.rb",caller)
route_definitions << block
def inherited(subclass)
super
subclass.called_from = caller.first.sub(/:\d+$/, '')
subclass.extension_name(subclass.name.to_name('Extension'))
end

def inherited(subclass)
subclass.extension_name = subclass.name.to_name('Extension')
def subclasses
superclass.subclasses
end

def route_definitions
@route_definitions ||= []
# override the original method to compensate for some extensions
# not having a "lib" directory. also, don't traverse upwards beyond
# the "vendor/extensions" directory
def find_root_with_flag(flag, default = nil)
path = File.dirname(self.called_from)
default ||= path

while path && !File.exist?("#{path}/#{flag}") && !path.ends_with?('vendor/extensions')
parent_dir = File.dirname(path)
path = parent_dir != path && parent_dir
end

root = File.exist?("#{path}/#{flag}") ? path : default
raise "Could not find root path for #{self}" unless root

Config::CONFIG['host_os'] =~ /mswin|mingw/ ?
Pathname.new(root).expand_path : Pathname.new(root).realpath
end

def migrate_from(extension_name, until_migration=nil)
Expand All @@ -147,7 +170,18 @@ def migrate_from(extension_name, until_migration=nil)
# end
# end
def extension_config(&block)
yield Rails.configuration
ActiveSupport::Deprecation.warn(<<-MSG, caller)
extension_config is deprecated. Use `config` or `initializer` methods instead:
# example config: add a load path for this specific Extension
config.autoload_paths << File.expand_path("../lib/some/path", __FILE__)
# example initializer block
initializer "my_extension.add_middleware" do |app|
app.middleware.use MyExtension::Middleware
end
MSG
yield config
end

end
Expand Down

0 comments on commit 1298c10

Please sign in to comment.