class AutoForme::Frameworks::Rails

  1. lib/autoforme/frameworks/rails.rb
Superclass: Framework

Methods

Public Class

  1. new
  2. setup

Public Instance

  1. setup_routes

Constants

ALL_SUPPORTED_ACTIONS_REGEXP = Regexp.union(AutoForme::Action::ALL_SUPPORTED_ACTIONS.map{|x| /#{Regexp.escape(x)}/})  

Public Class methods

new(*)

Define an autoforme method in the controller which handles the actions.

[show source]
   # File lib/autoforme/frameworks/rails.rb
51 def initialize(*)
52   super
53   framework = self
54   @controller.send(:define_method, :autoforme) do
55     if @autoforme_action = framework.action_for(Request.new(self))
56       if redirect = catch(:redirect){@autoforme_text = @autoforme_action.handle; nil}
57         redirect_to redirect
58       elsif @autoforme_action.output_type == 'csv'
59         response.headers['Content-Type'] = 'text/csv'
60         response.headers['Content-Disposition'] = "attachment; filename=#{@autoforme_action.output_filename}"
61         render :body=>@autoforme_text
62       elsif @autoforme_action.request.xhr?
63         render :html=>@autoforme_text.html_safe
64       else
65         opts = framework.opts[:view_options]
66         opts = opts ? opts.dup : {}
67         opts[:layout] = true unless opts.has_key?(:layout)
68         opts[:inline] = "<%=raw @autoforme_text %>"
69         render opts
70       end
71     else
72       render :plain=>'Unhandled Request', :status=>404
73     end
74   end
75 end
setup(controller, opts, &block)

After setting up the framework, add a route for the framework to Rails, so that requests are correctly routed.

[show source]
   # File lib/autoforme/frameworks/rails.rb
44 def self.setup(controller, opts, &block)
45   f = super
46   f.setup_routes
47   f
48 end

Public Instance methods

setup_routes()

Add a route for the framework to Rails routing.

[show source]
   # File lib/autoforme/frameworks/rails.rb
80 def setup_routes
81   if prefix
82     pre = prefix.to_s[1..-1] + '/'
83   end
84   model_regexp = Regexp.union(models.keys.map{|m| Regexp.escape(m)})
85   controller = @controller.name.sub(/Controller\z/, '').underscore
86   ::Rails.application.routes.prepend do
87     match "#{pre}:autoforme_model/:autoforme_action(/:id)" , :controller=>controller, :action=>'autoforme', :via=>[:get, :post],
88       :constraints=>{:autoforme_model=>model_regexp, :autoforme_action=>ALL_SUPPORTED_ACTIONS_REGEXP}
89   end
90   ::Rails.application.reload_routes!
91 end