<commit_before>package resources

import (
	"log"
	"net/http"

	"github.com/qor/qor"
	"github.com/qor/qor/admin"
	"github.com/qor/qor/roles"

	. "github.com/qor/qor-example/app/models"
	"github.com/qor/qor/i18n"
	"github.com/qor/qor/i18n/backends/database"
)

var (
	Admin *admin.Admin
	I18n  *i18n.I18n
)

func init() {
	// setting up QOR admin
	Admin = admin.New(&qor.Config{DB: Pub.DraftDB()})
	Admin.AddResource(Pub)
	Admin.SetAuth(&Auth{})

	I18n = i18n.New(database.New(StagingDB))
	Admin.AddResource(I18n)

	roles.Register("admin", func(req *http.Request, currentUser qor.CurrentUser) bool {
		if currentUser == nil {
			return false
		}

		if currentUser.(*User).Role == "admin" {
			return true
		}

		return false
	})

	roles.Register("user", func(req *http.Request, currentUser qor.CurrentUser) bool {
		if currentUser == nil {
			return false
		}

		if currentUser.(*User).Role == "user" {
			return true
		}

		return false
	})

	user := Admin.AddResource(
		&User{},
		&admin.Config{
			Menu: []string{"Users"},
			Name: "Users",
		},
	)

	user.Meta(&admin.Meta{
		Name:  "UserRole",
		Label: "Role",
		//Type:  "select_one",
		Collection: func(resource interface{}, context *qor.Context) (results [][]string) {
			return [][]string{
				{"admin", "admin"},
				{"user", "user"},
			}
		},
		Valuer: func(value interface{}, context *qor.Context) interface{} {
			if value == nil {
				return value
			}
			user := value.(*User)
			log.Println("user", user.Role)
			return user.Role
		},
	})

	user.IndexAttrs("ID", "Name", "Role")
	user.NewAttrs("Name", "UserRole")
	user.EditAttrs("Name", "UserRole")

	author := Admin.AddResource(
		&Author{},
		&admin.Config{Menu: []string{
			"Authors"},
			Name: "Author",
		},
	)
	
	author.IndexAttrs("ID", "Name")
	author.SearchAttrs("ID", "Name")
	
	book := Admin.AddResource(
		&Book{},
		&admin.Config{
			Menu: []string{"Books"},
			Name: "Books",
		},
	)

	book.Meta(&admin.Meta{
		Name:  "FormattedDate",
		Label: "Release Date",
		Valuer: func(value interface{}, context *qor.Context) interface{} {
			book := value.(*Book)
			return book.ReleaseDate.Format("Jan 2, 2006")
		},
	})

	book.Meta(&admin.Meta{
		Name: "Synopsis",
		Type: "rich_editor",
	})

	// which fields should be displayed in the books list on admin
	book.IndexAttrs("ID", "Title", "Authors", "FormattedDate", "Price")
	// which fields should be editable in the book esit interface
	book.NewAttrs("Title", "Authors", "Synopsis", "ReleaseDate", "Price", "CoverImage")<commit_msg>Remove unneeded admin config<commit_after>package resources

import (
	"log"
	"net/http"

	"github.com/qor/qor"
	"github.com/qor/qor/admin"
	"github.com/qor/qor/roles"

	. "github.com/qor/qor-example/app/models"
	"github.com/qor/qor/i18n"
	"github.com/qor/qor/i18n/backends/database"
)

var (
	Admin *admin.Admin
	I18n  *i18n.I18n
)

func init() {
	// setting up QOR admin
	Admin = admin.New(&qor.Config{DB: Pub.DraftDB()})
	Admin.AddResource(Pub)
	Admin.SetAuth(&Auth{})

	I18n = i18n.New(database.New(StagingDB))
	Admin.AddResource(I18n)

	roles.Register("admin", func(req *http.Request, currentUser qor.CurrentUser) bool {
		if currentUser == nil {
			return false
		}

		if currentUser.(*User).Role == "admin" {
			return true
		}

		return false
	})

	roles.Register("user", func(req *http.Request, currentUser qor.CurrentUser) bool {
		if currentUser == nil {
			return false
		}

		if currentUser.(*User).Role == "user" {
			return true
		}

		return false
	})

	user := Admin.AddResource(&User{})

	user.Meta(&admin.Meta{
		Name:  "UserRole",
		Label: "Role",
		//Type:  "select_one",
		Collection: func(resource interface{}, context *qor.Context) (results [][]string) {
			return [][]string{
				{"admin", "admin"},
				{"user", "user"},
			}
		},
		Valuer: func(value interface{}, context *qor.Context) interface{} {
			if value == nil {
				return value
			}
			user := value.(*User)
			log.Println("user", user.Role)
			return user.Role
		},
	})

	user.IndexAttrs("ID", "Name", "Role")
	user.NewAttrs("Name", "UserRole")
	user.EditAttrs("Name", "UserRole")

	author := Admin.AddResource(&Author{})

	author.IndexAttrs("ID", "Name")
	author.SearchAttrs("ID", "Name")

	book := Admin.AddResource(&Book{})

	book.Meta(&admin.Meta{
		Name:  "FormattedDate",
		Label: "Release Date",
		Valuer: func(value interface{}, context *qor.Context) interface{} {
			book := value.(*Book)
			return book.ReleaseDate.Format("Jan 2, 2006")
		},
	})

	book.Meta(&admin.Meta{
		Name: "Synopsis",
		Type: "rich_editor",
	})

	// which fields should be displayed in the books list on admin
	book.IndexAttrs("ID", "Title", "Authors", "FormattedDate", "Price")
	// which fields should be editable in the book esit interface
	book.NewAttrs("Title", "Authors", "Synopsis", "ReleaseDate", "Price", "CoverImage")
{'fraction_non_alphanumeric': 0.1797752808988764, 'fraction_numerical': 0.008739076154806492, 'mean_word_length': 3.826305220883534, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6057953', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1769, 'n_words': 370}