Last active
September 30, 2015 16:58
-
-
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/
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
<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> |
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
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