Sha256: 2d01fe6ad686e17c1870c364c1fb56cba30ad70952c652ced5659e97c9ef5b88

Contents?: true

Size: 830 Bytes

Versions: 2

Compression:

Stored size: 830 Bytes

Contents

class PositionableController < ApplicationController
  before_filter :find_resource
  
  public_api = %w( move_lower move_down move_higher move_up move_to_bottom
                   insert_at_bottom move_to_top insert_at_top remove_from_list ).freeze
  
  public_api.each do |action|
    define_method(action) do
      @resource.send(action, params[:list].to_sym)
      redirect_accordingly
    end
  end
  
  def insert_at
    @resource.insert_at(params[:position].to_i, params[:list].to_sym)
    redirect_accordingly
  end
  
  protected
    def find_resource
      klass = params[:resource].camelize.constantize
      @resource = klass.find(params[:id])
    end
    
    def redirect_accordingly
      begin
        redirect_to :back
      rescue ActionController::RedirectBackError
        redirect_to '/'
      end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aguids-positionable-0.2.1 app/controllers/positionable_controller.rb
aguids-positionable-0.2.2 app/controllers/positionable_controller.rb