Created
March 11, 2013 16:36
-
-
Save jferris/5135533 to your computer and use it in GitHub Desktop.
Duplication for method calls vs before_filters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Filters | |
| # Action name is duplicated in filter blacklist or whitelist | |
| class PoniesController < ApplicationController | |
| before_filter :find_pony, except: [:index] | |
| before_filter :authorize_rider, only: [:edit, :update, :destroy] | |
| def show | |
| end | |
| def index | |
| @ponies = Pony.all | |
| end | |
| def edit | |
| end | |
| def update | |
| if @pony.save | |
| redirect_to @pony | |
| else | |
| render 'edit' | |
| end | |
| end | |
| def destroy | |
| @pony.destroy | |
| redirect_to @poney | |
| end | |
| private | |
| def authorize_rider | |
| find_pony | |
| unless @poney.has_rider?(current_user) | |
| redirect_to root_url, error: 'Get off my pony' | |
| end | |
| end | |
| def find_pony | |
| Pony.find(params[:id]) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Method calls | |
| # Method name is duplicated in each action | |
| class PoniesController < ApplicationController | |
| def show | |
| @pony = find_pony | |
| end | |
| def index | |
| @ponies = Pony.all | |
| end | |
| def edit | |
| authorize_rider | |
| @pony = find_pony | |
| end | |
| def update | |
| authorize_rider | |
| @pony = find_pony | |
| if @pony.save | |
| redirect_to @pony | |
| else | |
| render 'edit' | |
| end | |
| end | |
| def destroy | |
| authorize_rider | |
| @pony = find_pony | |
| @pony.destroy | |
| redirect_to @pony | |
| end | |
| private | |
| def authorize_rider | |
| pony = find_pony | |
| unless pony.has_rider?(current_user) | |
| redirect_to root_url, error: 'Get off my pony' | |
| end | |
| end | |
| def find_pony | |
| Pony.find(params[:id]) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment