module Riveter::EnquiryController

Public Class Methods

enquiry_controller_for(enquiry_class, options={}) click to toggle source

configures the controller for the given enquiry

optionally define `on_success` and `on_failure` callback methods to provide additional controller logic as required

options supported include:

:as               defines the name of the form used to access the params
                  defaults to the name of the enquiry

:action           overrides the action name.
                  by default is is ":index"

:attributes       the list of permitted attributes for initializing the enquiry instance
                  defaults to the attributes defined using the `attr_*` helper methods
# File lib/riveter/enquiry_controller.rb, line 27
        def enquiry_controller_for(enquiry_class, options={})
          raise ArgumentError, "#{enquiry_class.name} does not include #{Enquiry.name} module or inherit from #{Enquiry::Base.name}" unless enquiry_class.ancestors.include?(Enquiry)

          options = {
            :as => enquiry_class.name.underscore.gsub(/_enquiry$/, ''),
            :attributes => enquiry_class.query_filter_attributes,
            :action => :index
          }.merge(options)

          action_method = options[:action]

          # define instance methods
          # which provide access to the given
          # enquiry class and the options
          define_method :enquiry_class do
            enquiry_class
          end

          define_method :enquiry_options do
            options
          end

          # define the 'index' action
          class_eval <<-RUBY
            def #{action_method}
              @enquiry = enquiry_class.new()
              if @enquiry.submit(enquiry_params)
                on_success(@enquiry) if self.respond_to?(:on_success)
              else
                on_failure(@enquiry) if self.respond_to?(:on_failure)
              end
            end
          RUBY

          include ActionsAndSupport
        end