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

Public Instance methods

setup_routes()

Add a route for the framework to Rails routing.

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