Skip to content

Commit b573fc5

Browse files
committed
finalized crud
1 parent cd46427 commit b573fc5

File tree

13 files changed

+236
-8
lines changed

13 files changed

+236
-8
lines changed

commands/coldbox/create/orm-crud.cfc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ component extends="coldbox-cli.models.BaseCommand" {
7373
// ********************** generate handler ************************************//
7474

7575
// Read Handler Content
76-
var hContent = fileRead( "#variables.settings.templatesPath#/crud/HandlerContent.txt" );
76+
var hContent = fileRead( "#variables.settings.templatesPath#/crud/#arguments.boxlang ? "bx" : "cfml"#/HandlerContent.txt" );
7777
// Token replacement
7878
hContent = replaceNoCase(
7979
hContent,
@@ -113,7 +113,7 @@ component extends="coldbox-cli.models.BaseCommand" {
113113
);
114114
var views = [ "edit", "editor", "new" ];
115115
for ( var thisView in views ) {
116-
var vContent = fileRead( "#variables.settings.templatesPath#/crud/#thisView#.txt" );
116+
var vContent = fileRead( "#variables.settings.templatesPath#/crud/#arguments.boxlang ? "bx" : "cfml"#/#thisView#.txt" );
117117
vContent = replaceNoCase(
118118
vContent,
119119
"|entity|",
@@ -127,17 +127,17 @@ component extends="coldbox-cli.models.BaseCommand" {
127127
"all"
128128
);
129129
fileWrite(
130-
arguments.viewsDirectory & "/#arguments.pluralName#/#thisView#.cfm",
130+
arguments.viewsDirectory & "/#arguments.pluralName#/#thisView#.#arguments.boxlang ? ".bxm" : "cfm"#",
131131
vContent
132132
);
133-
printInfo( "Generated View: [" & arguments.viewsDirectory & "/#arguments.pluralName#/#thisView#.cfm]" );
133+
printInfo( "Generated View: [" & arguments.viewsDirectory & "/#arguments.pluralName#/#thisView#.#arguments.boxlang ? ".bxm" : "cfm"#]" );
134134
}
135135

136136
// ********************** generate table output ************************************//
137137

138138
// Build table output for index
139139
savecontent variable="local.tableData" {
140-
include "#variables.settings.templatesPath#/crud/table.cfm";
140+
include "#variables.settings.templatesPath#/crud/#arguments.boxlang ? "bx" : "cfml"#/table.#arguments.boxlang ? ".bxm" : "cfm"#";
141141
}
142142
tableData = replaceNoCase(
143143
tableData,
@@ -152,7 +152,7 @@ component extends="coldbox-cli.models.BaseCommand" {
152152
"all"
153153
);
154154
// index data
155-
var vContent = fileRead( "#variables.settings.templatesPath#/crud/index.txt" );
155+
var vContent = fileRead( "#variables.settings.templatesPath#/crud/#arguments.boxlang ? "bx" : "cfml"#/index.txt" );
156156
vContent = replaceNoCase(
157157
vContent,
158158
"|entity|",
@@ -172,10 +172,10 @@ component extends="coldbox-cli.models.BaseCommand" {
172172
"all"
173173
);
174174
fileWrite(
175-
arguments.viewsDirectory & "/#arguments.pluralName#/index.cfm",
175+
arguments.viewsDirectory & "/#arguments.pluralName#/index.#arguments.boxlang ? ".bxm" : "cfm"#",
176176
vContent
177177
);
178-
printInfo( "Generated View: [" & arguments.viewsDirectory & "/#arguments.pluralName#/index.cfm]" );
178+
printInfo( "Generated View: [" & arguments.viewsDirectory & "/#arguments.pluralName#/index.#arguments.boxlang ? ".bxm" : "cfm"#]" );
179179
} else {
180180
return error( "The entity: #entityName# has no properties, so I have no clue what to CRUD on dude!" );
181181
}

templates/crud/bx/HandlerContent.txt

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* Manage |entityPlural|
3+
* It will be your responsibility to fine tune this template, add validations, try/catch blocks, logging, etc.
4+
*/
5+
class extends="coldbox.system.EventHandler"{
6+
7+
// DI Virtual Entity Service
8+
property name="ORMService" inject="entityService:|entity|";
9+
10+
// HTTP Method Security
11+
this.allowedMethods = {
12+
index = "GET",
13+
new = "GET",
14+
edit = "GET",
15+
delete = "POST,DELETE",
16+
save = "POST,PUT"
17+
};
18+
19+
/**
20+
* Param incoming format, defaults to `html`
21+
*/
22+
function preHandler( event, rc, prc ){
23+
event.paramValue( "format", "html" );
24+
}
25+
26+
/**
27+
* Listing
28+
*/
29+
function index( event, rc, prc ){
30+
// Get all |entityPlural|
31+
prc.|entityPlural| = ORMService.getAll();
32+
// Multi-format rendering
33+
event.renderData( data=prc.|entityPlural|, formats="xml,json,html,pdf" );
34+
}
35+
36+
/**
37+
* New Form
38+
*/
39+
function new( event, rc, prc ){
40+
// get new |entity|
41+
prc.|entity| = ORMService.new();
42+
43+
event.setView( "|entityPlural|/new" );
44+
}
45+
46+
/**
47+
* Edit Form
48+
*/
49+
function edit( event, rc, prc ){
50+
// get persisted |entity|
51+
prc.|entity| = ORMService.get( rc.|pk| );
52+
53+
event.setView( "|entityPlural|/edit" );
54+
}
55+
56+
/**
57+
* View |entity| mostly used for RESTful services only.
58+
*/
59+
function show( event, rc, prc ){
60+
// Default rendering.
61+
event.paramValue( "format", "json" );
62+
// Get requested entity by id
63+
prc.|entity| = ORMService.get( rc.|pk| );
64+
// Multi-format rendering
65+
event.renderData( data=prc.|entity|, formats="xml,json" );
66+
}
67+
68+
/**
69+
* Save and Update
70+
*/
71+
function save( event, rc, prc ){
72+
// get |entity| to persist or update and populate it with incoming form
73+
prc.|entity| = populateModel(
74+
model = ORMService.get( rc.|pk| ),
75+
exclude = "|pk|",
76+
composeRelationships = true
77+
);
78+
79+
// Do your validations here
80+
81+
// Save it
82+
ORMService.save( prc.|entity| );
83+
84+
// RESTful Handler
85+
switch(rc.format){
86+
// xml,json,jsont are by default. Add your own or remove
87+
case "xml" : case "json" : case "jsont" :{
88+
event.renderData( data=prc.|entity|, type=rc.format, location="/|entityPlural|/show/#prc.|entity|.get|pk|()#" );
89+
break;
90+
}
91+
// HTML
92+
default:{
93+
// Show a nice notice
94+
flash.put( "notice", { message="|entity| Created", type="success" } );
95+
// Redirect to listing: change to `setNextEvent()` if using ColdBox <5
96+
relocate( '|entityPlural|' );
97+
}
98+
}
99+
}
100+
101+
/**
102+
* Delete
103+
*/
104+
function delete( event, rc, prc ){
105+
// Delete record by ID
106+
var removed = ORMService.delete( ORMService.get( rc.|pk| ) );
107+
108+
// RESTful Handler
109+
switch( rc.format ){
110+
// xml,json,jsont are by default. Add your own or remove
111+
case "xml" : case "json" : case "jsont" :{
112+
var restData = { "deleted" = removed };
113+
event.renderData( data=restData, type=rc.format );
114+
break;
115+
}
116+
// HTML
117+
default:{
118+
// Show a nice notice
119+
flash.put( "notice", { message="|entity| Poofed!", type="success" } );
120+
// Redirect to listing: change to `setNextEvent()` if using ColdBox <5
121+
relocate( '|entityPlural|' );
122+
}
123+
}
124+
}
125+
126+
}

templates/crud/bx/edit.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<bx:output>
2+
#view( view: "|entityPlural|/editor", args: { title : "Update |entity|" } )#
3+
</bx:output>

templates/crud/bx/editor.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!---
2+
EntityField Comment:
3+
- textAreas = A list of properties that are textareas
4+
- booleanSelect = If true creates a select box, else two radio buttons
5+
- manyToOne = { manyToOnePropertyName = {valuecolumn='',namecolumn='',criteria={},sortorder=""} }
6+
A structure of data to help with many to one relationships on how they are presented.
7+
Possible key values for each key are [valuecolumn='',namecolumn='',criteria={},sortorder=string]. Example: {criteria={productid=1},sortorder='Department desc'}
8+
- ManyToMany = { manyToManyPropertyName = {valuecolumn='',namecolumn='',criteria={},sortorder="",selectColumn='' }
9+
A structure of data to help with many to one relationships on how they are presented.
10+
Possible key values for each key are [valuecolumn='',namecolumn='',criteria={},sortorder=string,selectColumn='']. Example: {criteria={productid=1},sortorder='Department desc'}
11+
- showRelations = If true (default) will show one to many and one to one relations as a view table snapshot
12+
--->
13+
14+
<bx:output>
15+
<h1>#args.title#</h1>
16+
17+
<!--- Submit Form --->
18+
#html.startForm( action: '|entityPlural|.save' )#
19+
20+
<!--- Convert Entity To Fields --->
21+
#html.entityFields(
22+
entity = prc.|entity|,
23+
groupWrapper = "div class='form-group'",
24+
class = "form-control",
25+
fieldWrapper = "",
26+
labelWrapper = "",
27+
textAreas = "",
28+
booleanSelect = true,
29+
manyToOne = {},
30+
manyToMany = {},
31+
showRelations = true
32+
)#
33+
34+
<!--- Submit --->
35+
<div class="form-group">
36+
#html.href( href="|entityPlural|", text="Cancel", class="btn btn-default" )#
37+
#html.submitButton( value="Save", class="btn btn-primary" )#
38+
</div>
39+
40+
#html.endForm()#
41+
</bx:output>

templates/crud/bx/index.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<bx:output>
2+
<h1>|entityPlural|</h1>
3+
4+
<!--- MessageBox --->
5+
<bx:if flash.exists( "notice" )>
6+
<div class="alert alert-#flash.get( "notice" ).type#">
7+
#flash.get( "notice" ).message#
8+
</div>
9+
</bx:if>
10+
11+
<!--- Create Button --->
12+
#html.href( href="|entityPlural|.new", text="Create |entity|", class="btn btn-primary" )#
13+
14+
<!--- Listing --->
15+
|tableListing|
16+
</bx:output>

templates/crud/bx/new.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<bx:output>
2+
#view( view="|entityPlural|/editor", args={ title="Create |entity|" } )#
3+
</bx:output>

templates/crud/bx/table.bxm

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<bx:output>
2+
<table class="table table-hover table-striped">
3+
<thead>
4+
<tr>
5+
<bx:loop array="#metadata.properties#" index="thisProp">
6+
<bx:if compareNoCase( thisProp.fieldType, "column" ) EQ 0>
7+
<th>#thisProp.name#</th>
8+
</bx:if>
9+
</bx:loop>
10+
<th width="150">Actions</th>
11+
</tr>
12+
</thead>
13+
14+
<tbody>
15+
%bx:loop array="##prc.#arguments.pluralName###" index="thisRecord">
16+
<tr>
17+
<bx:loop array="#metadata.properties#" index="thisProp">
18+
<bx:if compareNoCase( thisProp.fieldType, "column" ) EQ 0>
19+
<td>##thisRecord.get#thisProp.name#()##</td>
20+
</bx:if>
21+
</bx:loop>
22+
<!--- Actions --->
23+
<td>
24+
##html.startForm( action="#arguments.pluralname#.delete" )##
25+
##html.hiddenField( name="#metadata.pk#", bind=thisRecord )##
26+
##html.submitButton( value="Delete", onclick="return confirm('Really Delete Record?')", class="btn btn-danger" )##
27+
##html.href(
28+
href = "#arguments.pluralName#.edit",
29+
queryString = "#metadata.pk#=##thisRecord.get#metadata.pk#()##",
30+
text = "Edit",
31+
class = "btn btn-info"
32+
)##
33+
##html.endForm()##
34+
</td>
35+
</tr>
36+
%/bx:loop>
37+
</tbody>
38+
</table>
39+
</bx:output>
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)