From afaadc3942ff1bb71627ba9f5af9112e02874b48 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 6 Oct 2016 20:33:48 +0800 Subject: [PATCH] Refactor named value support for PolymorphicType --- association.go | 12 ++---------- callback_query_preload.go | 12 ++---------- callback_save.go | 12 ++---------- model_struct.go | 12 ++++++++++-- polymorphic_test.go | 4 ++-- 5 files changed, 18 insertions(+), 34 deletions(-) diff --git a/association.go b/association.go index f57c99af..9a3a338b 100644 --- a/association.go +++ b/association.go @@ -63,11 +63,7 @@ func (association *Association) Replace(values ...interface{}) *Association { } else { // Polymorphic Relations if relationship.PolymorphicDBName != "" { - value := scope.TableName() - if relationship.PolymorphicValue != "" { - value = relationship.PolymorphicValue - } - newDB = newDB.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.PolymorphicDBName)), value) + newDB = newDB.Where(fmt.Sprintf("%v = ?", scope.Quote(relationship.PolymorphicDBName)), relationship.PolymorphicValue) } // Delete Relations except new created @@ -286,13 +282,9 @@ func (association *Association) Count() int { } if relationship.PolymorphicType != "" { - value := scope.TableName() - if relationship.PolymorphicValue != "" { - value = relationship.PolymorphicValue - } query = query.Where( fmt.Sprintf("%v.%v = ?", scope.New(fieldValue).QuotedTableName(), scope.Quote(relationship.PolymorphicDBName)), - value, + relationship.PolymorphicValue, ) } diff --git a/callback_query_preload.go b/callback_query_preload.go index 13d0b85d..efa65bb5 100644 --- a/callback_query_preload.go +++ b/callback_query_preload.go @@ -114,11 +114,7 @@ func (scope *Scope) handleHasOnePreload(field *Field, conditions []interface{}) values := toQueryValues(primaryKeys) if relation.PolymorphicType != "" { query += fmt.Sprintf(" AND %v = ?", scope.Quote(relation.PolymorphicDBName)) - value := scope.TableName() - if relation.PolymorphicValue != "" { - value = relation.PolymorphicValue - } - values = append(values, value) + values = append(values, relation.PolymorphicValue) } results := makeSlice(field.Struct.Type) @@ -167,11 +163,7 @@ func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{}) values := toQueryValues(primaryKeys) if relation.PolymorphicType != "" { query += fmt.Sprintf(" AND %v = ?", scope.Quote(relation.PolymorphicDBName)) - value := scope.TableName() - if relation.PolymorphicValue != "" { - value = relation.PolymorphicValue - } - values = append(values, value) + values = append(values, relation.PolymorphicValue) } results := makeSlice(field.Struct.Type) diff --git a/callback_save.go b/callback_save.go index 6f4e2652..ea9ec174 100644 --- a/callback_save.go +++ b/callback_save.go @@ -60,11 +60,7 @@ func saveAfterAssociationsCallback(scope *Scope) { } if relationship.PolymorphicType != "" { - value := scope.TableName() - if relationship.PolymorphicValue != "" { - value = relationship.PolymorphicValue - } - scope.Err(newScope.SetColumn(relationship.PolymorphicType, value)) + scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue)) } scope.Err(newDB.Save(elem).Error) @@ -86,11 +82,7 @@ func saveAfterAssociationsCallback(scope *Scope) { } if relationship.PolymorphicType != "" { - value := scope.TableName() - if relationship.PolymorphicValue != "" { - value = relationship.PolymorphicValue - } - scope.Err(newScope.SetColumn(relationship.PolymorphicType, value)) + scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue)) } scope.Err(scope.NewDB().Save(elem).Error) } diff --git a/model_struct.go b/model_struct.go index ad8b9f9a..9a609585 100644 --- a/model_struct.go +++ b/model_struct.go @@ -294,7 +294,11 @@ func (scope *Scope) GetModelStruct() *ModelStruct { relationship.PolymorphicType = polymorphicType.Name relationship.PolymorphicDBName = polymorphicType.DBName // if Dog has multiple set of toys set name of the set (instead of default 'dogs') - relationship.PolymorphicValue = field.TagSettings["VALUE"] + if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok { + relationship.PolymorphicValue = value + } else { + relationship.PolymorphicValue = scope.TableName() + } polymorphicType.IsForeignKey = true } } @@ -388,7 +392,11 @@ func (scope *Scope) GetModelStruct() *ModelStruct { relationship.PolymorphicType = polymorphicType.Name relationship.PolymorphicDBName = polymorphicType.DBName // if Cat has several different types of toys set name for each (instead of default 'cats') - relationship.PolymorphicValue = field.TagSettings["VALUE"] + if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok { + relationship.PolymorphicValue = value + } else { + relationship.PolymorphicValue = scope.TableName() + } polymorphicType.IsForeignKey = true } } diff --git a/polymorphic_test.go b/polymorphic_test.go index b3a5c79e..d1ecfbbb 100644 --- a/polymorphic_test.go +++ b/polymorphic_test.go @@ -21,8 +21,8 @@ type Dog struct { type Hamster struct { Id int Name string - PreferredToy Toy `gorm:"polymorphic:Owner;value:hamster_preferred"` - OtherToy Toy `gorm:"polymorphic:Owner;value:hamster_other"` + PreferredToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_preferred"` + OtherToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_other"` } type Toy struct {