All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.grails.scaffolding.registry.input.BidirectionalToManyInputRenderer.groovy Maven / Gradle / Ivy

The newest version!
package org.grails.scaffolding.registry.input

import org.grails.scaffolding.model.property.DomainProperty
import org.grails.scaffolding.registry.DomainInputRenderer
import grails.util.GrailsNameUtils
import grails.web.mapping.LinkGenerator
import org.grails.datastore.mapping.model.PersistentProperty
import org.grails.datastore.mapping.model.types.ToMany

/**
 * The default renderer for rendering bidirectional to many associations
 *
 * @author James Kleeh
 */
class BidirectionalToManyInputRenderer implements DomainInputRenderer {

    protected LinkGenerator linkGenerator

    BidirectionalToManyInputRenderer(LinkGenerator linkGenerator) {
        this.linkGenerator = linkGenerator
    }

    @Override
    boolean supports(DomainProperty property) {
        PersistentProperty persistentProperty = property.persistentProperty
        persistentProperty instanceof ToMany && persistentProperty.bidirectional
    }

    protected String getPropertyName(DomainProperty property) {
        GrailsNameUtils.getPropertyName(property.rootBeanType)
    }

    protected String getAssociatedClassName(DomainProperty property) {
        property.associatedType.simpleName
    }

    @Override
    Closure renderInput(Map defaultAttributes, DomainProperty property) {
        final String objectName = "${getPropertyName(property)}.id"
        defaultAttributes.remove('required')
        defaultAttributes.remove('readonly')
        defaultAttributes.href = linkGenerator.link(resource: property.associatedType, action: "create", params: [(objectName): ""])
        return { ->
            a("Add ${getAssociatedClassName(property)}", defaultAttributes)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy