module Formize::ActionController

Public Class Methods

included(base) click to toggle source
# File lib/formize/action_controller.rb, line 5
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

save_and_respond(resource, options={}) { |resource, format| ... } click to toggle source

Adds method to provides a default response for create/update actions It saves the record/resource and return response with good status and headers

# File lib/formize/action_controller.rb, line 11
def save_and_respond(resource, options={}, &block)
  creation = resource.new_record?
  resource.attributes = options[:attributes] unless options[:attributes].nil?
  respond_to do |format|
    # if ((block_given? and block.arity == 1) ? yield(resource) : (block_given? and block.arity == 2) ? yield(resource, format) : resource.save)
    if (block_given? ? yield(resource, format) : resource.save)
      status = (creation ? :created : :ok)
      response.headers["X-Return-Code"] = "success"
      response.headers["X-Saved-Record-Id"] = resource.id.to_s
      format.html { params[:dialog] ? head(status) : redirect_to(options[:url] || resource) }
      format.json { render :json => resource.to_json, :status => status, :location => resource}
      format.xml  { render  :xml => resource.to_xml,  :status => status, :location => resource }
    else
      response.headers["X-Return-Code"] = "invalid"
      format.html { render :action => (resource.new_record? ? "new" : "edit")}
      format.json { render :json => resource.errors.to_json, :status => :unprocessable_entity }
      format.xml  { render  :xml => resource.errors.to_xml,  :status => :unprocessable_entity }
    end
  end
end