views:

31

answers:

2

Hi, i have a requirement to allow the user to define some custom field in one of the system entities. do you have any suggestion/pattern/plugin that will help me add this feature to my application.

thanks,

Meni

+1  A: 

Sounds like you want your application to be an infinitely adjustable wrench that users can modify at will. Is that fair?

I don't think it's possible or desirable. Think about what happens when you add an attribute to an existing domain object in Grails. The attribute is added to the ORM mapping, which means the tables have to be modified. The UI has another text box added for data entry; the list page has another column added to its table.

There's a lot going on when you add an attribute. How will you manage multiple users modifying the app all at the same time? What happens when one user is modifying a table while another is accessing the old version?

You ask too much. I don't think it's a reasonable requirement. Grails' sweet spot is rapid development of web-based CRUD applications. I don't think that includes modification by users at runtime.

duffymo
this requirement exist and implemented in many systems. so i don't think that i'm asking too much. maybe the implementation is complex but it's doable - i did it a few years ago in JEE application and i'll be happy to get help and ideas from developers that did it in Grails.
Meni Lubetkin
Agreed with the poster while it's technically duable Grails is the wrong technology for this. You could write stackoverflow.com in assembly but it wouldn't be a good diea when you take into account all the extra effort involved.
Jared
+2  A: 

You can add a Map property to your domain class and store arbitrary data there. It's rather limited though. It will generate a table with varchar(255) keys and values, so you need to manage any type conversions yourself, e.g.

class Thing {
   String name
   Map extraProperties = [:]
}

int age = 123
def thing = new Thing(name: 'whatever')
thing.extraProperties.age = age.toString()
thing.save()

...

def thing = Thing.get(thingId)
int age = thing.extraProperties.age.toInteger()

See section "5.2.4 Sets, Lists and Maps" at http://grails.org/doc/latest/ for the brief online docs.

Burt Beckwith
No type safety or abstraction there.
duffymo
so if i want to do it dynamic - i'll maybe need additional map to store the type or to put in the map a class with properties that will describe the type:Class extraProp { int typeCode String Value}
Meni Lubetkin
@duffymo Type safety and abstraction weren't mentioned in the requirements.
Matt Lachman
As an alternative to Burt's approach, but in the same vein, see http://stackoverflow.com/questions/3237393/how-to-adjust-constraints-db-mapping-for-map-within-grails-domain-class/3240589#3240589
Matt Lachman
@Matt Lachman - Precious little was mentioned in the "requirements". Just a point of interest, nothing more.
duffymo