mileszs
7/6/2009 - 9:21 PM

rfqs_controller.rb

class RfqsController < ApplicationController
  before_filter :find_rfq, :only => %w(show edit update destroy)
  before_filter :login_required, :only => %w(index show edit update destroy)

  def index
    @rfqs = current_user.organization.rfqs
    respond_to do |format|
      format.html
    end
  end

  def show
    @rfq = Rfq.find(params[:id])
    respond_to do |format|
      format.html
    end
  end

  def new
    if logged_in?
      @user = current_user
      @organization = @user.organization
      @address = @organization.addresses[0] || @organization.addresses.build
      @phone = @organization.phones[0] || @organization.phones.build
    else
      @organization = Organization.new
      @user = User.new
      @address = Address.new
      @phone = Phone.new
    end
    @rfq = Rfq.new
  end

  def edit
    @rfq = Rfq.find(params[:id])
    @user = current_user
    @organization = @rfq.organization
    @address = @organization.addresses[0] || @organization.addresses.build
    @phone = @organization.phones[0] || @organization.phones.build
  end

  def create
    # TODO/OPTIMIZE: This is going to need refactored.  It's big and ugly.
    # ... and it should get bigger, because we're not accounting for an
    # existing customer or user.
    if logged_in?
      @user = current_user
      @organization = @user.organization
    else
      @organization = Organization.find_or_initialize_by_name(params[:organization][:name], params[:organization])
    end

    if @organization.save
      if params[:rfq][:dust_jacket].nil? || params[:rfq][:dust_jacket] != '1'
        params[:rfq][:dust_jacket] = false
      end
      @user ||= @organization.users.find_by_email(params[:user][:email]) || @organization.users.build(params[:user])
      @address = @organization.addresses.find_by_address1(params[:address][:address1]) || @organization.addresses.build(params[:address])
      @phone = @organization.phones.find_by_phone_number(params[:phone][:phone_number]) || @organization.phones.build(params[:phone])
      @rfq = @organization.rfqs.build(params[:rfq])
      @rfq.address = @address
      @rfq.creator = @user

      respond_to do |format|
        if [@rfq, @user, @address].all?(&:valid?)
          Rfq.transaction do
            @rfq.save!
            @user.register!
            @address.save!
            @phone.save!
            @rfq.notify_admin
          end
          flash[:notice] = 'Your request has been recieved!  We will contact you shortly.'
          format.html { redirect_to '/' }
        else
          format.html { render :action => :new }
        end
      end
    else
      respond_to do |format|
        format.html { render :action => :new }
      end
    end
  end

  protected

    def find_rfq
      @rfq = Rfq.find(params[:id])
    end

    def authorized?
      if @rfq
        logged_in? && (current_user.admin? || @rfq.editable_by?(current_user))
      else
        logged_in?
      end
    end
end