Class: AutoForme::Frameworks::Rails

Inherits:
AutoForme::Framework show all
Defined in:
lib/autoforme/frameworks/rails.rb

Defined Under Namespace

Classes: Request

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from AutoForme::Framework

#controller, #model_classes, #models, #opts, #prefix

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AutoForme::Framework

#action_for, #association_links_for, #autocomplete_options_for, #columns_for, #display_name_for, #edit_html_for, #filter_for, for, #form_attributes_for, #form_options_for, #inline_mtm_associations_for, #lazy_load_association_links?, #limit_for, #model, #model_class, #mtm_associations_for, #order_for, #page_footer_for, #page_header_for, #redirect_for, #register_by_name, #register_by_name?, #show_html_for, #supported_actions_for, #table_class_for

Methods included from OptsAttributes

#opts_attribute

Constructor Details

#initializeRails

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/autoforme/frameworks/rails.rb', line 47

def initialize(*)
  super
  framework = self
  @controller.send(:define_method, :autoforme) do
    if @autoforme_action = framework.action_for(Request.new(self))
      if redirect = catch(:redirect){@autoforme_text = @autoforme_action.handle; nil}
        redirect_to redirect
      elsif @autoforme_action.output_type == 'csv'
        response.headers['Content-Type'] = 'text/csv'
        response.headers['Content-Disposition'] = "attachment; filename=#{@autoforme_action.output_filename}"
        render :text=>@autoforme_text
      elsif @autoforme_action.request.xhr?
        render :text=>@autoforme_text
      else
        render :inline=>"<%=raw @autoforme_text %>", :layout=>true
      end
    else
      render :text=>'Unhandled Request', :status=>404
    end
  end
end

Class Method Details

.setup(controller, opts, &block) ⇒ Object

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



40
41
42
43
44
# File 'lib/autoforme/frameworks/rails.rb', line 40

def self.setup(controller, opts, &block)
  f = super
  f.setup_routes
  f
end

Instance Method Details

#setup_routesObject

Add a route for the framework to Rails routing.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/autoforme/frameworks/rails.rb', line 72

def setup_routes
  if prefix
    pre = prefix.to_s[1..-1] + '/'
  end
  model_regexp = Regexp.union(models.keys.map{|m| Regexp.escape(m)})
  controller = @controller.name.sub(/Controller\z/, '').underscore
  ::Rails.application.routes.prepend do
    match "#{pre}:autoforme_model/:autoforme_action(/:id)" , :controller=>controller, :action=>'autoforme', :via=>[:get, :post],
      :constraints=>{:autoforme_model=>model_regexp, :autoforme_action=>ALL_SUPPORTED_ACTIONS_REGEXP}
  end
  ::Rails.application.reload_routes!
end