Class: AutoForme::Frameworks::Sinatra

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

Defined Under Namespace

Classes: Request

Instance Attribute Summary

Attributes inherited from AutoForme::Framework

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

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?, setup, #show_html_for, #supported_actions_for, #table_class_for

Methods included from OptsAttributes

#opts_attribute

Constructor Details

#initializeSinatra

Add get and post routes when creating the framework. These routes can potentially match other routes, but in that case use pass to try the next route.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/autoforme/frameworks/sinatra.rb', line 39

def initialize(*)
  super
  framework = self
  block = lambda do
    if @autoforme_action = framework.action_for(Request.new(self))
      @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
        erb "<%= @autoforme_text %>".dup
      end
    else
      pass
    end
  end

  prefix = Regexp.escape(framework.prefix) if framework.prefix
  @controller.get %r{\A#{prefix}/(\w+)/(\w+)(?:/([\w-]+))?\z}, &block
  @controller.post %r{\A#{prefix}/(\w+)/(\w+)(?:/([\w-]+))?\z}, &block
end