Skip to content

Commit

Permalink
*should fix requested changes (cleaned up the code a bit too)
Browse files Browse the repository at this point in the history
  • Loading branch information
mclark4386 committed Oct 21, 2018
1 parent 77ad6b2 commit f2e5783
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 27 deletions.
5 changes: 3 additions & 2 deletions associations/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/nulls"
"github.com/gobuffalo/uuid"
)

// Association represents a definition of a model association
Expand Down Expand Up @@ -160,4 +159,6 @@ func isZero(i interface{}) bool {
return v.Interface() == reflect.Zero(v.Type()).Interface()
}

var emptyUUID = uuid.Nil.String()
func IsZeroOfUnderlyingType(x interface{}) bool {
return x == reflect.Zero(reflect.TypeOf(x)).Interface()
}
16 changes: 3 additions & 13 deletions associations/has_many_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (

"github.com/gobuffalo/flect"
"github.com/gobuffalo/pop/nulls"

"github.com/gobuffalo/uuid"
"github.com/markbates/inflect"
)

// hasManyAssociation is the implementation for the has_many
Expand Down Expand Up @@ -137,15 +134,8 @@ func (a *hasManyAssociation) AfterProcess() AssociationStatement {
ids := []interface{}{ownerID}

for i := 0; i < v.Len(); i++ {
fval := v.Index(i).FieldByName(otherIDField)

id := ""
if fval.Type().Name() == "UUID" {
id = fval.Interface().(uuid.UUID).String()
} else {
id = fmt.Sprint(fval.Interface())
}
if id != "0" && id != emptyUUID {
id := v.Index(i).FieldByName(otherIDField).Interface()
if !IsZeroOfUnderlyingType(id) {
ids = append(ids, id)
}
}
Expand All @@ -158,7 +148,7 @@ func (a *hasManyAssociation) AfterProcess() AssociationStatement {

fk := a.fkID
if fk == "" {
fk = inflect.Underscore(a.ownerName) + "_id"
fk = flect.Underscore(a.ownerName) + "_id"
}

ret := fmt.Sprintf("UPDATE %s SET %s = ? WHERE %s in (?", a.tableName, fk, otherIDField)
Expand Down
16 changes: 4 additions & 12 deletions associations/has_one_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

"github.com/gobuffalo/flect"
"github.com/gobuffalo/pop/nulls"
"github.com/gobuffalo/uuid"
"github.com/markbates/inflect"
)

type hasOneAssociation struct {
Expand Down Expand Up @@ -37,7 +35,7 @@ func hasOneAssociationBuilder(p associationParams) (Association, error) {
fval := p.modelValue.FieldByName(p.field.Name)
return &hasOneAssociation{
owner: p.model,
ownedTableName: p.popTags.Find("has_one").Value + "s",
ownedTableName: flect.Pluralize(p.popTags.Find("has_one").Value),
ownedModel: fval,
ownedType: fval.Type(),
ownerID: ownerID.Interface(),
Expand Down Expand Up @@ -107,20 +105,14 @@ func (h *hasOneAssociation) AfterSetup() error {

func (h *hasOneAssociation) AfterProcess() AssociationStatement {
otherIDField := "ID"
fval := h.ownedModel.FieldByName(otherIDField)
id := h.ownedModel.FieldByName(otherIDField).Interface()

ownerIDField := "ID"
ownerID := reflect.Indirect(reflect.ValueOf(h.owner)).FieldByName(ownerIDField).Interface()

ids := []interface{}{ownerID}

id := ""
if fval.Type().Name() == "UUID" {
id = fval.Interface().(uuid.UUID).String()
} else {
id = fmt.Sprint(fval.Interface())
}
if id != "0" && id != emptyUUID {
if !IsZeroOfUnderlyingType(id) {
ids = append(ids, id)
} else {
return AssociationStatement{
Expand All @@ -131,7 +123,7 @@ func (h *hasOneAssociation) AfterProcess() AssociationStatement {

fk := h.fkID
if fk == "" {
fk = inflect.Underscore(h.ownerName) + "_id"
fk = flect.Underscore(h.ownerName) + "_id"
}

ret := fmt.Sprintf("UPDATE %s SET %s = ? WHERE %s = ?", h.ownedTableName, fk, otherIDField)
Expand Down

0 comments on commit f2e5783

Please sign in to comment.