lib/rtml.rb in rtml-1.2.2 vs lib/rtml.rb in rtml-2.0.0.alpha.1
- old
+ new
@@ -1,4 +1,90 @@
-$:.unshift(File.dirname(__FILE__)) unless
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+path = File.expand_path File.join(File.dirname(__FILE__), 'lib')
+$:.unshift(path) unless $:.include?(path) || $:.include?(File.expand_path(path))
+
+RTML_ROOT = File.expand_path(File.join(File.dirname(__FILE__), "..")) unless defined?(RTML_ROOT)
+require 'rtml/version'
+
module Rtml
+ class << self
+ def root
+ RTML_ROOT
+ end
+
+ def configuration
+ @configuration ||= Rtml::Configuration.new
+ end
+
+ def configure
+ yield configuration
+ configuration.validate!
+ end
+
+ # Defines a single RTML action.
+ #
+ # Examples:
+ # Rtml.action :index, :controller => :rtml do
+ # def index
+ # # . . .
+ # end
+ # end
+ #
+ def action(name, controller, &block)
+ case controller
+ when Array then controllers = controller
+ when String, Symbol, ActionController::Base then controllers = [ controller ]
+ when Hash then controllers = [ controller.delete(:controller) ] || controller.delete(:controllers)
+ else raise ArgumentError, "Expected an Array, String, Symbol or Hash listing affected controllers"
+ end
+
+ actions_for controllers do
+ define_method name do
+ self.instance_eval &block
+ end
+ end
+ end
+
+
+ # Defines a block containing a set of RTML actions for the specified controllers.
+ #
+ # Examples:
+ # Rtml::actions_for :controller_name do
+ # def index
+ # # . . .
+ # end
+ # end
+ #
+ def actions_for(*controllers, &block)
+ raise ArgumentError, "actions_for: block containing RTML actions is expected", caller unless block_given?
+ (controllers.flatten.collect do |controller|
+ case controller
+ when String, Symbol then "#{controller.to_s.camelize}Controller".constantize
+ else controller
+ end
+ end).each do |controller|
+ helpers = controller.rtml_helpers
+ old_methods = helpers.instance_methods
+ helpers.class_eval &block
+ new_methods = helpers.instance_methods - old_methods
+ controller.rtml_actions.concat new_methods
+
+ new_methods.each do |method_name|
+ unless helpers.hidden_action?(method_name)
+ helpers.class_eval(<<-end_code, __FILE__, __LINE__+1)
+ def #{method_name}_with_ivars
+ copy_ivars_from(controller)
+ # Give rtml actions access to the controller's view helpers
+ (class << self; self; end).send(:include, controller.response.template.helpers)
+ #{method_name}_without_ivars
+ end
+ end_code
+
+ helpers.send(:alias_method_chain, method_name, :ivars)
+ end
+ unless helpers.hidden_action?(method_name) || controller.instance_methods.include?(method_name)
+ controller.add_rtml_proxy_action(method_name)
+ end
+ end
+ end
+ end
+ end
end