Skip to content

Commit

Permalink
Refactor qor example
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 5, 2015
1 parent ea8ea70 commit 1bd1a83
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ worker_data
*.exe

# Example
/example/example
/example/public
/example/harp.json

Expand Down
8 changes: 4 additions & 4 deletions admin/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func (res *Resource) GetMetas(_attrs ...[]string) []resource.Metaor {
structFields := scope.GetModelStruct().StructFields
attrs = []string{}

StructFields:
Fields:
for _, field := range structFields {
for _, meta := range res.Metas {
if field.Name == meta.Alias {
attrs = append(attrs, meta.Name)
continue StructFields
continue Fields
}
}

Expand All @@ -139,7 +139,7 @@ func (res *Resource) GetMetas(_attrs ...[]string) []resource.Metaor {

for _, value := range []string{"CreatedAt", "UpdatedAt", "DeletedAt"} {
if value == field.Name {
continue StructFields
continue Fields
}
}

Expand All @@ -149,7 +149,7 @@ func (res *Resource) GetMetas(_attrs ...[]string) []resource.Metaor {
MetaIncluded:
for _, meta := range res.Metas {
for _, attr := range attrs {
if attr == meta.Alias {
if attr == meta.Alias || attr == meta.Name {
continue MetaIncluded
}
}
Expand Down
Binary file removed example/example
Binary file not shown.
24 changes: 4 additions & 20 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,18 @@ func main() {
user.Meta(&admin.Meta{Name: "fullname", Alias: "name"})

user.Meta(&admin.Meta{Name: "State", Type: "select_one", Collection: []string{"Active", "Inactive"}})
user.Meta(&admin.Meta{Name: "RoleID", Label: "Role", Type: "select_one",
Collection: func(resource interface{}, context *qor.Context) (results [][]string) {
if roles := []Role{}; !context.GetDB().Find(&roles).RecordNotFound() {
for _, role := range roles {
results = append(results, []string{fmt.Sprintf("%v", role.ID), role.Name})
}
}
return
},
})
user.Meta(&admin.Meta{Name: "Role", Type: "select_one"})

user.Meta(&admin.Meta{Name: "Languages", Type: "select_many"})

user.Meta(&admin.Meta{Name: "description", Type: "rich_editor", Resource: Admin.NewResource(&admin.AssetManager{})})

Admin.AddResource(&Language{}, &admin.Config{Name: "Locales", Menu: []string{"Products Management"}})

product := Admin.AddResource(&Product{}, &admin.Config{Menu: []string{"Products Management"}})
product.SearchAttrs("Name", "Description")
product.Meta(&admin.Meta{Name: "CollectionID", Label: "Collection", Type: "select_one",
Collection: func(resource interface{}, context *qor.Context) (results [][]string) {
if collections := []Collection{}; !context.GetDB().Find(&collections).RecordNotFound() {
for _, collection := range collections {
results = append(results, []string{fmt.Sprintf("%v", collection.ID), collection.Name})
}
}
return
},
})
product.Meta(&admin.Meta{Name: "Collection", Type: "select_one"})

Admin.AddResource(&Collection{}, &admin.Config{Menu: []string{"Products Management"}})
Admin.AddResource(&Order{}, &admin.Config{Menu: []string{"Orders Management"}})

Expand Down
2 changes: 2 additions & 0 deletions example/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type User struct {
Birthday *time.Time
Description string `sql:"size:622550"`
RoleID uint
Role Role
Languages []Language `gorm:"many2many:user_languages;"`
CreditCard CreditCard
CreditCardID uint
Expand Down Expand Up @@ -71,6 +72,7 @@ type Product struct {
Name *string `l10n:"sync"`
Description *string
CollectionID uint
Collection Collection
l10n.Locale
publish.Status
}
Expand Down

0 comments on commit 1bd1a83

Please sign in to comment.