Skip to content

Instantly share code, notes, and snippets.

@victorcreed
Created March 29, 2013 17:06
Show Gist options
  • Save victorcreed/5272150 to your computer and use it in GitHub Desktop.
Save victorcreed/5272150 to your computer and use it in GitHub Desktop.
metaprograming in controller or dynamic controller ruby on rails or whatever
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