Giter VIP home page Giter VIP logo

taggable's Introduction

Taggable Grails Plugin

The Taggable plugin that adds a generic mechanism for tagging data.

Taggable Plugin

This plugin provides an alternative to the Acts as Taggable hosted at grails.org and with the following features.

Classes can be made taggable by implementing the grails.plugins.taggable.Taggable interface

  • Method chaining can be used to add tags
  • The table name the domain classes use is customizable
  • Utilizes extensive caching to improve performance
  • Property use of packages to avoid domain conflicts

Requirements

Grails Version: 4.0.0

Installation

Add this dependency to build.gradle

    dependencies {
    compile "io.github.gpc:taggable:4.0.0"
}

By default, the plugin will force all tags to lower case. If you want to preserve the case of tags, you must specify the following in application.yml

grails:
    taggable:
        preserve:
            case: true

alternatively in application.groovy:

grails.taggable.preserve.case = true

This will preserve the supplied case of tags when adding and removing them. Eg adding tags "grails" and "Grails" will result in two tags on the object. The finder methods for locating objects by tag still require the exact case of the tag you want to find - each tag is treated as discrete.

Usage

Implement the Taggable interface:

import grails.plugins.taggable.*
class Vehicle implements Taggable {
}

Add some tags:

def v = Vehicle.get(1)
v.addTag("red")
 .addTag("sporty")
 .addTag("expensive")
// Alternatively
v.setTags(['red', 'sporty', 'expensive'])
// Or
v.addTags(['electric', 'hybrid'])

Query:

def v = Vehicle.get(1)
println v.tags
def vehicles = Vehicle.findAllByTag("sporty") // Also takes params eg [max:5]
def count = Vehicle.countByTag("sporty")

assert 3 == Vehicle.totalTags
assert ['expensive', 'red','sporty'] == Vehicle.allTags

// Find all cars with tag "electric", where the instances also 
def teslaElectricCars = Vehicle.findAllByTagWithCriteria('electric') {
    eq('manufacturer', 'Tesla Motors')
}

// Find all the tags for this class, using the supplied params and criteria that operate on the TAGS
def fiveCoolTags = Vehicle.findAllTagsWithCriteria( [max:5]) {
    ilike('name', '%cool%')
}

Query with HQL:

//NOTE: Tag and TagLink aren't GORM domain objects, but they are both mapped and available for HQL
//Find Vehicles by tag, but group on model
String findByTagHQL = """
   SELECT vehicle
   FROM Vehicle vehicle
            ,TagLink tagLink
   WHERE vehicle.id = tagLink.tagRef
   AND tagLink.type = 'Vehicle'
   AND tagLink.tag.name IN (:tags)
   GROUP BY vehicle.model.name
   ORDER BY vehicle.model.name
"""
List tags = ["SUV", "gas-guzzler", "tank", "boat"]
def gasGuzzlers = Vehicle.executeQuery(findByTagHQL, [tags: tags])


//List all tags since there isn't a Tag domain object
String listAllHQL = """
   SELECT tag
   FROM Tag tag
   ORDER BY tag.name
"""
def allTags = Vehicle.executeQuery(listAllHQL)

Tag parsing:

def tags = "red,sporty,expensive"
def v = Vehicle.get(1)
v.parseTags(tags)
assert ['expensive', 'red', 'sporty'] == v.tags

tags = "red/sporty/expensive"

v.parseTags(tags, "/")
assert ['expensive', 'red', 'sporty'] == v.tags

Configuration options

Customized Table names

You can change the table names used for the Tag and TagLink classes in Config.groovy:

grails.taggable.tag.table="MY_TAGS"
grails.taggable.tagLink.table="MY_TAG_LINKS"

Turn hibernate autoImport back on

Since version 1.0 snapshots, hibernate autoImport is turned off in the mapping DSL to avoid clashes with existing domain classes called Tag or TagLink. If you really need to revert to the old behaviour you can do so with this Config.groovy:

grails.taggable.tag.autoImport=true
grails.taggable.tagLink.autoImport=true

taggable's People

Contributors

cazacugmihai avatar codeconsole avatar graemerocher avatar jjelliott avatar lhotari avatar marcpalmer avatar pledbrook avatar rhyolight avatar sbglasius avatar stokito avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

taggable's Issues

Support for non-Long ids

TagLink requires domains being tagged to have a Long id.

My project is using a locally modified version of the plugin where I changed tagRef to be a UUID.
If there is any demand - I can create a pull request updating the plugin to generate TagLink from a template, a la Sprint Security Core's s2-quickstart script, that will allow users to change the tagRef type.

Thoughts?

Incompatible with Grails 4

Due to removal of deprecated methods on the DefaultGrailsDomainClass, the plugin is broken on Grails 4.

change max length of tag name to 191 chars to support utf8mb4

I need to change my database's charset to utf8mb4. In MySQL this means that the maximum of length of any indexed string columns is reduced from 255 to 191 chars. The Tag.name property is indexed, so as things currently stand it can't be used with the utf8mb4 charset.

In order to support this charset I'm proposing to reduce the maximum length of this field to 191 chars. Although in theory this could be a breaking change for some users, in practice I think the likelihood of any user actually having tags that are named with more than 191 chars is an acceptable risk.

All that's required is adding a maxSize: 191 constraint to the name field. Some Info about utf8mb4 is available here: http://mathiasbynens.be/notes/mysql-utf8mb4

Invalid tag_link types in case of 'lazy' hibernate proxies

Hi there,
In cases when Hibernate uses 'lazy' loading of domain object it doesn't actually create a full object of the specific type but only a proxy which has different type name than original one. This results in wrong behaviour of taggable.
For example:
Lets say there is an domain object:
class Book {
Author author
}

def book = Book.get(3453)
book.author.addTag("test")

will end up as database call similar to this:

insert into tag_links (actor_id, date_created, tag_id, tag_ref, type) values (null, '2014-05-21 13:27:03', 14, 90, 'author_$$_javassist_190')

book.author is in fact javassist hibernate proxy not a real Author object. So if there is a subsequent call:

def author = Author.get(567567)
author.getTagLinks()

it will do:
select ... tag_links this_ inner join tags tag2_ on this_.tag_id=tag2_.id where this_.tag_id=14 and this_.tag_ref=90 and this_.type='autor' limit 1

and fail to find the tag because the type is obviously different.

One way to fix it - is to change:
GrailsNameUtils.getPropertyName(instance.class) calls in TaggableGrailsPlugin.groovy
onto:
GrailsNameUtils.getPropertyName(Hibernate.getClass(instance)) - Hibernate.getClass() properly resolves class of the object event if it's a proxy.

BR

Using with Grails 5.2.x -- repo location?

Hello,
I am trying to use the taggable plugin on a 5.2.4 grails app. It looks like this github repo has been updated somewhat recently, but some of the content might be out of sync? Or, I may be missing some obvious things...

The docs say to add
compile "io.github.gpc:taggable:4.0.0"
to the dependencies, but AFAIU that is outdated in recent gradle, and perhaps should be
implementation "io.github.gpc:taggable:4.0.0"
?

Also, the docs seem to suggest the plugin is available in the standard grails plugin site. I can no longer find it there, and get an error suggesting that the 'stanard' repos don't know about it.

buildscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://repo.grails.org/grails/core" }
    }

Any pointers to overcome my misunderstandings would be most welcome :-)

Upgrade to Grails 3.1.1

I am migrating 2.4.4 project to 3.1.1 and as discussed in 9506 , @graemerocher does this plugin needs to be compiled with 3.1.1 against GORM 5, so that there's no conflict?

Also documentation needs to be updated for its dependency to - compile "org.grails.plugins:taggable:2.0.0-SNAPSHOT".

Taggable dependency not working

This issue started happening yesterday and seems to be related to this alert:
https://grails.org/blog/2021-02-12-alerts-regarding-bintray-sunset.html


> Could not resolve all dependencies for configuration ':runtime'.
   > Could not resolve org.grails.plugins:taggable:2.1.0-SNAPSHOT.
     Required by:
         chartertools:chartertools:4.1.0
      > Could not resolve org.grails.plugins:taggable:2.1.0-SNAPSHOT.
         > Unable to load Maven meta-data from http://dl.bintray.com/stefanogualdi/plugins/org/grails/plugins/taggable/2.1.0-SNAPSHOT/maven-metadata.xml.
            > Could not GET 'http://dl.bintray.com/stefanogualdi/plugins/org/grails/plugins/taggable/2.1.0-SNAPSHOT/maven-metadata.xml'. Received status code 403 from server: Forbidden
      > Could not resolve org.grails.plugins:taggable:2.1.0-SNAPSHOT.
         > Unable to load Maven meta-data from http://dl.bintray.com/agorapulse/libs/org/grails/plugins/taggable/2.1.0-SNAPSHOT/maven-metadata.xml.
            > Could not GET 'http://dl.bintray.com/agorapulse/libs/org/grails/plugins/taggable/2.1.0-SNAPSHOT/maven-metadata.xml'. Received status code 403 from server: Forbidden

We use the Taggable plugin in two different Grails projects (also different versionS).
This error occurs when running Grails version 3.3.8 and JDK 8.

Do we need to create a local dependency of this plugin?
If so, which version of the code should we use? The 2.1.0 version is nowhere to be found on github.

Any suggestions would be greatly appreciated since this is a critical plugin for this project.

Trait not being applied to Java Domain classes

I have my domain classes as Java/JPA object. In Grails 2.5.2, the taggable plugin would correctly apply the trait to them. In 3.0.10 with this version of the plugin, I get compile errors that my domain objects that implement Taggable need to implement the methods defined in the trait.

I am not sure if this is a taggable code issue or grails 3.0.10? I believe the taggable code may need a trait interceptor?

Publishing 3.1 compatible version?

Currently, I can't find this plugin on Bintray, only a snapshot from last year on repos.grails.org.
Travis successfully builds this plugin, but fails because of a missing configuration for publishing it.

Are there any publishing plans, so people with grails 3.1 can easily use this plugin?
A new snapshot releas on repos.grails.org would be sufficient. Is someone working on this?

The OpenSpeedMonitor depends on this plugin and people should really be able to get it from somewhere.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.