-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7652f96
Showing
6 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
class OneTimeDataGrailsPlugin { | ||
// the plugin version | ||
def version = "1.0" | ||
// the version or versions of Grails the plugin is designed for | ||
def grailsVersion = "1.1 > *" | ||
// the other plugins this plugin depends on | ||
def dependsOn = ['controllers':'1.1 > *'] | ||
// resources that are excluded from plugin packaging | ||
def pluginExcludes = [ | ||
"grails-app/views/error.gsp" | ||
] | ||
|
||
// TODO Fill in these fields | ||
def author = "Marc Palmer" | ||
def authorEmail = "marc@grailsrocks.com" | ||
def title = "A deferred data concept that replaces flash scope to work safely with multiple concurrent requests" | ||
def description = '''\\ | ||
Brief description of the plugin. | ||
''' | ||
def observe = ['controllers'] | ||
|
||
// URL to the plugin's documentation | ||
def documentation = "http://grails.org/plugin/one-time-data" | ||
|
||
def doWithWebDescriptor = { xml -> | ||
// TODO Implement additions to web.xml (optional), this event occurs before | ||
} | ||
|
||
def doWithSpring = { | ||
// TODO Implement runtime spring config (optional) | ||
} | ||
|
||
def doWithDynamicMethods = { ctx -> | ||
applyDynamicMethods(application) | ||
} | ||
|
||
void applyDynamicMethods(application) { | ||
def svc = application.mainContext.oneTimeDataService | ||
|
||
application.controllerClasses.each { cc -> | ||
cc.clazz.metaClass.oneTimeData = { String id, Closure dataSetup -> | ||
svc.store(delegate.request, id, dataSetup) | ||
} | ||
cc.clazz.metaClass.oneTimeData << { Closure dataSetup -> | ||
svc.store(delegate.request, dataSetup) | ||
} | ||
|
||
// Get the data from session and remove it from session, and put it in the request | ||
// so we can get it as much as we like in this request | ||
cc.clazz.metaClass.getOneTimeData = { String id -> | ||
def key = 'one.time.data.'+id | ||
def alreadyGotData = delegate.request[key] | ||
if (alreadyGotData) { | ||
return alreadyGotData | ||
} | ||
def s = delegate.session | ||
def data = s[key] | ||
s.removeAttribute(key) | ||
delegate.request['one.time.data.found.'+id] = data != null | ||
delegate.request[key] = data | ||
return data | ||
} | ||
cc.clazz.metaClass.getOneTimeData << { -> | ||
delegate.getOneTimeData(delegate.params.id) | ||
} | ||
} | ||
|
||
} | ||
def doWithApplicationContext = { applicationContext -> | ||
// TODO Implement post initialization spring config (optional) | ||
} | ||
|
||
def onChange = { event -> | ||
applyDynamicMethods(application) | ||
} | ||
|
||
def onConfigChange = { event -> | ||
// TODO Implement code that is executed when the project configuration changes. | ||
// The event is the same as for 'onChange'. | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#Grails Metadata file | ||
#Tue Mar 30 14:16:20 BST 2010 | ||
app.grails.version=1.2.1 | ||
app.name=OneTimeData | ||
plugins.hibernate=1.2.1 | ||
plugins.tomcat=1.2.1 |
37 changes: 37 additions & 0 deletions
37
grails-app/services/com/grailsrocks/onetimedata/services/OneTimeDataService.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.grailsrocks.onetimedata.services | ||
|
||
import org.springframework.web.context.request.RequestContextHolder | ||
|
||
class OneTimeDataService { | ||
static transactional = false | ||
|
||
def store(req, String id, Closure dataSetup) { | ||
def session = req.session | ||
def key = 'one.time.data.'+id | ||
def data = session[key] | ||
if (data == null) { | ||
session[key] = data = [:] | ||
} | ||
dataSetup.delegate = data | ||
dataSetup() | ||
|
||
// Put it in the request so we can use it in this request without | ||
// removing it from session | ||
req.setAttribute('one.time.data.'+id, data) | ||
return id | ||
} | ||
|
||
def store(String id, Closure dataSetup) { | ||
store(RequestContextHolder.currentRequestAttributes().currentRequest, id, dataSetup) | ||
} | ||
|
||
def store(request, Closure dataSetup) { | ||
def id = System.currentTimeMillis().toString() | ||
store(request, id, dataSetup) | ||
} | ||
|
||
def store(Closure dataSetup) { | ||
store(RequestContextHolder.currentRequestAttributes().currentRequest, id, dataSetup) | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
grails-app/taglib/com/grailsrocks/onetimedata/tags/OneTimeDataTagLib.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.grailsrocks.onetimedata.tags | ||
|
||
class OneTimeDataTagLib { | ||
static namespace = 'onetime' | ||
|
||
def exists = { attrs, body -> | ||
def id = attrs.id ?: params.id | ||
if (request['one.time.data.found.'+id]) { | ||
out << body(data:request['one.time.data.'+id]) | ||
} | ||
} | ||
|
||
def missing = { attrs, body -> | ||
def id = attrs.id ?: params.id | ||
if (false == request['one.time.data.found.'+id]) { | ||
out << body() | ||
} | ||
} | ||
|
||
def notExpected = { attrs, body -> | ||
def id = attrs.id ?: params.id | ||
if (null == request['one.time.data.found.'+id]) { | ||
out << body() | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
17 changes: 17 additions & 0 deletions
17
test/unit/com/grailsrocks/onetimedata/tags/OneTimeDataTagLibTests.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.grailsrocks.onetimedata.tags | ||
|
||
import grails.test.* | ||
|
||
class OneTimeDataTagLibTests extends TagLibUnitTestCase { | ||
protected void setUp() { | ||
super.setUp() | ||
} | ||
|
||
protected void tearDown() { | ||
super.tearDown() | ||
} | ||
|
||
void testSomething() { | ||
|
||
} | ||
} |