class AutoForme::Frameworks::Rails
Constants
- ALL_SUPPORTED_ACTIONS_REGEXP
Public Class Methods
new(*)
click to toggle source
Define an autoforme method in the controller which handles the actions.
Calls superclass method
AutoForme::Framework::new
# File lib/autoforme/frameworks/rails.rb 47 def initialize(*) 48 super 49 framework = self 50 @controller.send(:define_method, :autoforme) do 51 if @autoforme_action = framework.action_for(Request.new(self)) 52 if redirect = catch(:redirect){@autoforme_text = @autoforme_action.handle; nil} 53 redirect_to redirect 54 elsif @autoforme_action.output_type == 'csv' 55 response.headers['Content-Type'] = 'text/csv' 56 response.headers['Content-Disposition'] = "attachment; filename=#{@autoforme_action.output_filename}" 57 render :body=>@autoforme_text 58 elsif @autoforme_action.request.xhr? 59 render :html=>@autoforme_text.html_safe 60 else 61 render :inline=>"<%=raw @autoforme_text %>", :layout=>true 62 end 63 else 64 render :plain=>'Unhandled Request', :status=>404 65 end 66 end 67 end
setup(controller, opts, &block)
click to toggle source
After setting up the framework, add a route for the framework to Rails
, so that requests are correctly routed.
Calls superclass method
AutoForme::Framework::setup
# File lib/autoforme/frameworks/rails.rb 40 def self.setup(controller, opts, &block) 41 f = super 42 f.setup_routes 43 f 44 end
Public Instance Methods
setup_routes()
click to toggle source
Add a route for the framework to Rails
routing.
# File lib/autoforme/frameworks/rails.rb 72 def setup_routes 73 if prefix 74 pre = prefix.to_s[1..-1] + '/' 75 end 76 model_regexp = Regexp.union(models.keys.map{|m| Regexp.escape(m)}) 77 controller = @controller.name.sub(/Controller\z/, '').underscore 78 ::Rails.application.routes.prepend do 79 match "#{pre}:autoforme_model/:autoforme_action(/:id)" , :controller=>controller, :action=>'autoforme', :via=>[:get, :post], 80 :constraints=>{:autoforme_model=>model_regexp, :autoforme_action=>ALL_SUPPORTED_ACTIONS_REGEXP} 81 end 82 ::Rails.application.reload_routes! 83 end