Skip to content

Instantly share code, notes, and snippets.

@chrisdpeters
Last active September 30, 2015 16:58
Show Gist options
  • Save chrisdpeters/1831143 to your computer and use it in GitHub Desktop.
Save chrisdpeters/1831143 to your computer and use it in GitHub Desktop.
Structuring CFCs a Little Like Ruby http://www.chrisdpeters.com/structuring-cfcs-like-ruby/
<cfcomponent extends="Controller">
<!------------------------------------->
<!--- Public --->
<cffunction name="init">
<cfset filters(through="findPost", only="show,edit,update,destroy")>
</cffunction>
<cffunction name="show">
<!--- ... --->
</cffunction>
<cffunction name="edit">
<!--- ... --->
</cffunction>
<cffunction name="update">
<!--- ... --->
</cffunction>
<cffunction name="destroy">
<!--- ... --->
</cffunction>
<!------------------------------------->
<!--- Private --->
<cffunction name="findPost" access="private">
<cfset post = model("post").findByKey(params.key)>
</cffunction>
</cfcomponent>
class PostsController < ApplicationController
before_filter :find_post, :only => [:show, :edit, :update, :destroy]
def show
# ...
end
def edit
# ...
end
def update
# ...
end
def destroy
# ...
end
private
def find_post
@post = Post.find(params[:id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment