Skip to content

Instantly share code, notes, and snippets.

@a-ono
a-ono / accessors.coffee
Last active August 29, 2015 14:00
ruby-like accessors in coffeescript
Function::getter = (name, getter) ->
getter ?= () -> @["_" + name]
Object.defineProperty @prototype, name, get: getter, configurable: true
Function::setter = (name, setter) ->
setter ?= (value) -> @["_" + name] = value
Object.defineProperty @prototype, name, set: setter, configurable: true
Function::accessor = (name, options) ->
{get: getter, set: setter, default: value} = options ? {}
@a-ono
a-ono / gist:10230452
Created April 9, 2014 06:05
hasManyThrough
// retrieve users with specific role by executing two queries
val userIds = project1.memberships.where(_.roleId === role1.id).select(_.userId)
println(User.where(_.id in userIds).toList)
// or define additional associations for assigning and retrieving users with specific role
case class Project(name: String) extends ActiveRecord {
lazy val memberships = hasMany[Membership]
lazy val users = hasManyThrough[User, Membership](memberships)
// additional associations
@a-ono
a-ono / gist:8642560
Created January 27, 2014 02:47
play2 linkTo
def linkTo(label: String, url: String, options: (Symbol, Any)*) = Html(
options.foldLeft(<a href={url}>{label}</a>) {
case (elem, (k, v)) =>
elem % new xml.UnprefixedAttribute(k.name, v.toString, elem.attributes)
}.toString
)