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, #filter_for, for, #form_attributes_for, #form_options_for, #inline_mtm_associations_for, #lazy_load_association_links?, #limit_for, #model, #mtm_associations_for, #order_for, #page_footer_for, #page_header_for, #redirect_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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/autoforme/frameworks/rails.rb', line 44

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
      else
        render :inline=>"<%=raw @autoforme_text %>", :layout=>!@autoforme_action.request.xhr?
      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.



37
38
39
40
41
# File 'lib/autoforme/frameworks/rails.rb', line 37

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.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/autoforme/frameworks/rails.rb', line 63

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