Skip to content

Commit

Permalink
Add setter for has_many association #29
Browse files Browse the repository at this point in the history
  • Loading branch information
Draveness committed Sep 15, 2016
1 parent 5a44642 commit 05ead2b
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/metamodel/template/has_many_association.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ extension <%= association.class_name %> {
}

public extension <%= association.major_model.name %> {
var <%= association.name %>: <%= association.secondary_model.relation_name %> {
var <%= association.name %>: [<%= association.secondary_model.name %>] {
get {
let ids = <%= association.class_name %>.findBy(<%= association.major_model_id %>: privateId).map { $0.<%= association.secondary_model_id %> }
return <%= association.secondary_model.name %>.find(ids)
return <%= association.secondary_model.name %>.find(ids).result
}
set {
<%= association.class_name %>.findBy(<%= association.major_model_id %>: privateId).forEach { $0.delete() }
newValue.forEach { <%= association.class_name %>.create(<%= association.major_model_id %>: privateId, <%= association.secondary_model_id %>: $0.privateId) }
}
}
}

0 comments on commit 05ead2b

Please sign in to comment.