Last active
July 28, 2018 13:23
-
-
Save neokoenig/8bc3645b9ee71fb848d6295b1e8f42b6 to your computer and use it in GitHub Desktop.
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
component extends="app.controllers.Controller" hint="Locations Controller" | |
{ | |
// Name of your model: | |
this.modelName="location"; | |
// Optional nested properties to include: | |
// this.includeNestedProperties = "somethign"; | |
function config() { | |
super.config(); | |
} | |
function index(){ | |
locations=model("location").findAll(); | |
} | |
function new() { | |
scaffold("new"); | |
} | |
function create() { | |
scaffold("create"); | |
} | |
function edit() { | |
scaffold("edit"); | |
} | |
function update() { | |
scaffold("update"); | |
} | |
function delete() { | |
scaffold("delete"); | |
} | |
} |
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
<cfscript> | |
//===================================================================== | |
//= Scaffolding : include this in your Controller.cfc | |
//===================================================================== | |
/** | |
* Assumes a lot about your standard CRUD controller, like singluar/plural and where your params are. | |
**/ | |
function scaffold(required string action){ | |
local.model = this.modelName; | |
local.ms = singularize(local.model); | |
local.mp = pluralize(local.model); | |
local.action = arguments.action; | |
local.where = ""; | |
local.nestedProperties = ""; | |
if(structKeyExists(this, "includeNestedProperties")){ | |
local.nestedProperties = this.includeNestedProperties; | |
} | |
if(structKeyExists(params, "key")) | |
local.where = isNumeric(params.key) ? "id = #params.key#" : "id = '#params.key#'" ; | |
switch(local.action) { | |
case "new": | |
variables[local.ms]=variables.model(local.ms).new(include=local.nestedProperties); | |
break; | |
case "create": | |
if(structkeyexists(params, local.ms)){ | |
variables[local.ms] = variables.model(local.ms).new(properties=params[local.ms], include=local.nestedProperties); | |
if ( variables[local.ms].save() ) { | |
redirectTo(route=local.mp, success=l(local.ms) & ' ' & l("successfully created")); | |
} | |
else { | |
renderView(action="add", error=l("There were problems creating that") & ' ' & l(local.ms)); | |
} | |
} | |
break; | |
case "edit": | |
variables[local.ms]=variables.model(local.ms).findOne(where=local.where, include=local.nestedProperties); | |
if(!isObject(variables[local.ms])){ | |
redirectTo(route=local.mp, error=l(local.ms) & ' ' & l("not found")); | |
} | |
break; | |
case "update": | |
if(structkeyexists(params, local.ms)){ | |
variables[local.ms]=variables.model(local.ms).findOne(where=local.where, include=local.nestedProperties); | |
variables[local.ms].update(params[local.ms]); | |
if ( variables[local.ms].save() ) { | |
redirectTo(route=local.mp, success=l(local.ms) & ' ' & l("successfully updated")); | |
} | |
else { | |
renderView(action="edit", error=l("There were problems updating that") & ' ' & l(local.ms)); | |
} | |
} | |
break; | |
case "delete": | |
variables[local.ms] = variables.model(local.ms).findOne(where=local.where, include=local.nestedProperties); | |
if ( variables[local.ms].delete() ) { | |
redirectTo(route=local.mp, success=l(local.ms) & ' ' & l("successfully deleted")); | |
} | |
else { | |
renderView(action="edit", error=l("There were problems deleting that") & ' ' & l(local.ms)); | |
} | |
break; | |
} | |
} | |
// Shortcut to a localisation function not included here: couldn't be bothered to remove it from above. | |
function l(string t){ | |
return t; | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment