Skip to content

Commit

Permalink
Merge branch 'master' into more-scope-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Mar 11, 2018
2 parents 8e5ef0f + c6bcdcf commit 3d0b4e9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
4 changes: 1 addition & 3 deletions associations/belongs_to_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package associations
import (
"fmt"
"reflect"

"github.com/markbates/inflect"
)

// belongsToAssociation is the implementation for the belongs_to
Expand All @@ -23,7 +21,7 @@ func init() {

func belongsToAssociationBuilder(p associationParams) (Association, error) {
fval := p.modelValue.FieldByName(p.field.Name)
ownerIDField := fmt.Sprintf("%s%s", inflect.Capitalize(fval.Type().Name()), "ID")
ownerIDField := fmt.Sprintf("%s%s", p.field.Name, "ID")

if _, found := p.modelType.FieldByName(ownerIDField); !found {
return nil, fmt.Errorf("there is no '%s' defined in model '%s'", ownerIDField, p.modelType.Name())
Expand Down
17 changes: 5 additions & 12 deletions associations/belongs_to_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,25 @@ import (
)

type fooBelongsTo struct {
ID uuid.UUID `db:"id"`
NestedBars []nestedBar `has_many:"nested_bars"`
ID uuid.UUID `db:"id"`
}

func (f fooBelongsTo) TableName() string {
return "foosy"
}

type barBelongsTo struct {
FooBelongsToID uuid.UUID `db:"foo_id"`
Foo fooBelongsTo `belongs_to:"foo"`
NestedBar nestedBar `has_one:"nestedBar"`
}

type nestedBar struct {
ID uuid.UUID `db:"id"`
fooBelongsToID uuid.UUID `db:"foo_belongs_to_id"`
FooID uuid.UUID `db:"foo_id"`
Foo fooBelongsTo `belongs_to:"foo"`
}

func Test_Belongs_To_Association(t *testing.T) {
a := require.New(t)

id, _ := uuid.NewV1()
bar := barBelongsTo{FooBelongsToID: id}
bar := barBelongsTo{FooID: id}

as, err := associations.AssociationsForStruct(&bar, "Foo.NestedBars")
as, err := associations.AssociationsForStruct(&bar, "Foo")
a.NoError(err)
a.Equal(len(as), 1)
a.Equal(reflect.Struct, as[0].Kind())
Expand Down
2 changes: 1 addition & 1 deletion finders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func Test_Find_Eager_Has_One_With_Inner_Associations_Struct(t *testing.T) {
err = tx.Create(&composer)
a.NoError(err)

coolSong := Song{Title: "Hook", UserID: user.ID, ComposerID: composer.ID}
coolSong := Song{Title: "Hook", UserID: user.ID, ComposedByID: composer.ID}
err = tx.Create(&coolSong)
a.NoError(err)

Expand Down
2 changes: 1 addition & 1 deletion migrations/20160808213310_setup_tests2.up.fizz
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ create_table("songs", func(t) {
t.Column("id", "uuid", {"primary":true})
t.Column("u_id", "int", {"null":true})
t.Column("title", "string", {})
t.Column("composer_id", "int", {"null":true})
t.Column("composed_by_id", "int", {"null":true})
})

create_table("composers", func(t) {
Expand Down
14 changes: 7 additions & 7 deletions pop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ type Enemy struct {
}

type Song struct {
ID uuid.UUID `db:"id"`
Title string `db:"title"`
UserID int `db:"u_id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
ComposerID int `json:"composer_id" db:"composer_id"`
ComposedBy Composer `belongs_to:"composer"`
ID uuid.UUID `db:"id"`
Title string `db:"title"`
UserID int `db:"u_id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
ComposedByID int `json:"composed_by_id" db:"composed_by_id"`
ComposedBy Composer `belongs_to:"composer"`
}

type Composer struct {
Expand Down

0 comments on commit 3d0b4e9

Please sign in to comment.