-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFluxiableGrailsPlugin.groovy
90 lines (75 loc) · 2.97 KB
/
FluxiableGrailsPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import grails.plugin.fluxiable.Fluxiable
import grails.plugin.fluxiable.ActivityLink
import grails.util.GrailsNameUtils
import grails.plugin.fluxiable.ActivityException
import grails.plugin.fluxiable.Activity
class FluxiableGrailsPlugin {
def groupId = "org.icescrum"
def version = "0.3.2"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.3.8 > *"
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/error.gsp"
]
def author = "Stephane Maldini"
def authorEmail = "smaldini@doc4web.com"
def title = "Fluxiable Plugin"
def description = '''\\
Inspired by Commentable plugin, this one allows you to display domain activity in a generic manner.
'''
// URL to the plugin's documentation
def documentation = "http://grails.org/fluxiable"
def doWithSpring = {
def config = application.config
}
def doWithDynamicMethods = { ctx ->
for (domainClass in application.domainClasses) {
if (Fluxiable.class.isAssignableFrom(domainClass.clazz)) {
domainClass.clazz.metaClass {
'static' {
getRecentActivities {link=false->
def clazz = delegate
ActivityLink.getRecentActivities(clazz,link).list()
}
}
addActivity { poster, String code, String cachedLabel, String cachedDesc = null ->
if (delegate.id == null) throw new ActivityException("You must save the entity [${delegate}] before calling addActivity")
def posterClass = poster.class.name
def i = posterClass.indexOf('_$$_javassist')
if (i > -1)
posterClass = posterClass[0..i - 1]
def c = new Activity(code: code, cachedLabel: cachedLabel, posterId: poster.id, posterClass: posterClass, cachedId: delegate.id,
cachedDescription: cachedDesc)
if (!c.validate()) {
throw new ActivityException("Cannot create activity for arguments [$poster, $code, $cachedLabel], they are invalid.")
}
c.save()
def delegateClass = delegate.class.name
i = delegateClass.indexOf('_$$_javassist')
if (i > -1) delegateClass = delegateClass[0..i - 1]
def link = new ActivityLink(activity: c, activityRef: delegate.id, type: GrailsNameUtils.getPropertyName(delegateClass))
link.save()
try {
delegate.onAddActivity(c)
} catch (MissingMethodException e) {}
return delegate
}
getActivities = {link=false->
ActivityLink.getActivities(delegate,link).list()
}
getTotalActivities = {->
ActivityLink.getTotalActivities(delegate).list()[0]
}
removeActivity { Activity a ->
a.delete()
}
removeActivity { Long id ->
def c = Activity.load(id)
if (c) removeActivity(c)
}
}
}
}
}
}