فهرست منبع

Refactor named value support for PolymorphicType

Jinzhu 7 سال پیش
والد
کامیت
afaadc3942
5فایلهای تغییر یافته به همراه18 افزوده شده و 34 حذف شده
  1. 2 10
      association.go
  2. 2 10
      callback_query_preload.go
  3. 2 10
      callback_save.go
  4. 10 2
      model_struct.go
  5. 2 2
      polymorphic_test.go

+ 2 - 10
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,
 		)
 	}
 

+ 2 - 10
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)

+ 2 - 10
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)
 				}

+ 10 - 2
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
 								}
 							}

+ 2 - 2
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 {