Skip to content

Instantly share code, notes, and snippets.

@safarista
Created September 3, 2012 19:17
Show Gist options
  • Save safarista/3612562 to your computer and use it in GitHub Desktop.
Save safarista/3612562 to your computer and use it in GitHub Desktop.
require "application_responder"
class ApplicationController < ActionController::Base
# ...
def self.acts_as_magical
around_filter :magic_new, :only => :new
around_filter :magic_show, :only => :show
around_filter :magic_edit, :only => :edit
around_filter :magic_update, :only => :update
around_filter :magic_create, :only => :create
around_filter :magic_index, :only => :index
around_filter :magic_destroy, :only => :destroy
end
# ...
protected
def magic_index
instance_variable_set("@#{object_name.pluralize}", data_source)
yield
end
def magic_show
instance_variable_set("@#{object_name}", data_source.find_by_id(params[:id]))
yield
end
def magic_new
instance_variable_set("@#{object_name}", data_source.new)
yield
end
def magic_create
instance_variable_set("@#{object_name}", data_source.create(params[object_name]))
yield
respond_with *lineage
end
def magic_edit
instance_variable_set("@#{object_name}", data_source.find_by_id(params[:id]))
yield
end
def magic_update
instance_variable_set("@#{object_name}", data_source.find_by_id(params[:id]))
instance_varialbe_get("@#{object_name}").update_attributes(params[object_name])
yield
respond_with *lineage
end
def magic_destroy
instance_variable_set("@#{object_name}", data_source.find_by_id(params[:id]))
instance_varialbe_get("@#{object_name}").destroy
yield
respond_with *lineage
end
def object
instance_variable_get("@#{object_name}")
end
def object_name
self.class.name.split('::').last.sub(/Controller$/,'').singularize.underscore
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment