-
-
Save victorcreed/5272150 to your computer and use it in GitHub Desktop.
metaprograming in controller or dynamic controller ruby on rails or whatever
This file contains 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
class VideosController < ApplicationController | |
before_filter :video, only: [:show, :edit, :update, :watch, :buy, :rent] | |
def update | |
authorize! :update, @video | |
if @video.update_attributes params[:video] | |
redirect_to @video | |
else | |
render :edit | |
end | |
end | |
["watch", "buy", "rent"].each do |a| | |
VideosController.class_eval do | |
define_method a do | |
authorize!(a.to_sym, @video) | |
@response = Video.send a, @video | |
end | |
end | |
end | |
private | |
def video | |
@video = Video.find params[:id] | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment