Skip to content

Extend default controller methods

grafikchaos edited this page Mar 27, 2012 · 8 revisions

To extend existing controller method functionality this pattern can be used: first alias the original method and then override the method, do some stuff and call the original method.

 controller do
    def scoped_collection
      end_of_association_chain.accessible_by(current_ability)
    end
    alias_method :update_ori, :update
    alias_method :create_ori, :create
    def update
      # don't allow a user to escape his constraints
      params[:user][:lessee_id] = current_user.lessee_id if current_user.lessee_id?
      params[:user][:owner_id] = current_user.owner_id if current_user.owner_id?
      update_ori # call original method
    end
    def create
      # don't allow a user to escape his constraints
      params[:user][:lessee_id] = current_user.lessee_id if current_user.lessee_id?
      params[:user][:owner_id] = current_user.owner_id if current_user.owner_id?
      create_ori # call original method
    end
  end
Clone this wiki locally