Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CustomersController < ApplicationController
- define_model :customer, :redirect_on_save => proc { edit_customer_path @customer },
- :before_save => :save_contacts,
- :after_save => proc{ @customer.save_actual_term }
- after_filter :fetch_customers, :only => [:list, :print]
- before_filter :find_customer, :only => [:set_approve, :approve]
- around_filter ScopedAccess::Filter.new(Customer, :scoped_by_filter)
- def approve
- if @customer.can_be_approved?
- @customer.credit_limit = params[:credit_limit]
- @customer.approve!
- flash[:notice] = "Customer has been approved."
- else
- flash[:warning] = "The selected customer cannot be approved!"
- end
- redirect_to customer_path(@customer)
- end
- def list
- @customers = Customer.find(:all, :order => 'name')
- session[:last_list_filter] = uri_encode(params[:filter] || {})
- end
- def print
- @customers = Customer.find(:all, :order => 'name')
- @landscape = false
- @paper = 'letter'
- render :layout => false
- end
- protected
- def fetch_customers
- @customers = Customer.find(:all, :order => 'name')
- end
- # before save
- def save_contacts
- return if params[:contact].blank?
- fields, values = [*params[:contact]].transpose
- contacts = values.transpose.reject { |vals| vals[0].blank? }.collect do |vals|
- attributes = fields.zip(vals).inject({}) { |h, p| h.merge(p[0] => p[1]) }
- Contact.new attributes
- end
- @customer.contacts = contacts
- end
- def find_customer
- @customer = Customer.find(params[:id])
- end
- end
- class Hash
- def to_attribute_array
- fields, values = [*self].transpose
- values.transpose.reject { |vals| vals.all?(&:blank?) }.collect do |vals|
- fields.zip(vals).inject({}) { |h, p| h.merge(p[0] => p[1]) }
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement