class AutoForme::Frameworks::Roda

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

Methods

Public Class

  1. new

Public Instance

  1. route_proc

Attributes

Public Class methods

new (*)

Return a proc that should be instance_execed in the Roda routing and and handles the route if it recognizes it, otherwise doing nothing.

[show source]
# File lib/autoforme/frameworks/roda.rb, line 49
def initialize(*)
  super
  framework = self

  matchers = [:model, :action_type]
  if framework.prefix
    matchers.unshift(framework.prefix[1..-1])
  end

  @route_proc = lambda do 
    r = request
    path = if r.respond_to?(:matched_path)
      r.matched_path
    else
      # :nocov:
      r.env['SCRIPT_NAME']
      # :nocov:
    end
    current_matchers = matchers + [lambda{@autoforme_action = framework.action_for(Request.new(self, path))}]

    r.on(*current_matchers) do
      @autoforme_text = @autoforme_action.handle
      if @autoforme_action.output_type == 'csv'
        response['Content-Type'] = 'text/csv'
        response['Content-Disposition'] = "attachment; filename=#{@autoforme_action.output_filename}"
        @autoforme_text
      elsif @autoforme_action.request.xhr?
        @autoforme_text
      else
        view(:content=>@autoforme_text)
      end
    end
  end
end