<commit_before>package admin

import (
	"fmt"

	"github.com/jinzhu/gorm"
	"github.com/qor/qor"
	"github.com/qor/qor/resource"
	"github.com/qor/qor/roles"
	"github.com/qor/qor/utils"
)

type Resource struct {
	resource.Resource
	admin       *Admin
	Config      *Config
	Metas       []*Meta
	actions     []*Action
	scopes      map[string]*Scope
	filters     map[string]*Filter
	indexAttrs  []string
	newAttrs    []string
	editAttrs   []string
	showAttrs   []string
	cachedMetas *map[string][]*Meta
}

func (res *Resource) Meta(meta *Meta) {
	meta.base = res
	meta.updateMeta()
	res.Metas = append(res.Metas, meta)
}

func (res Resource) GetAdmin() *Admin {
	return res.admin
}

func (res Resource) ToParam() string {
	return utils.ToParamString(res.Name)
}

func (res *Resource) ConvertObjectToMap(context qor.Contextor, value interface{}, metas []*Meta) interface{} {
	var metaors []resource.Metaor
	for _, meta := range metas {
		metaors = append(metaors, meta)
	}
	return resource.ConvertObjectToMap(context, value, metaors)
}

func (res *Resource) Decode(contextor qor.Contextor, value interface{}) (errs []error) {
	return resource.Decode(contextor, value, res)
}

func (res *Resource) CallFinder(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
	if res.Finder != nil {
		return res.Finder(result, metaValues, context)
	} else {
		var primaryKey string
		if metaValues == nil {
			primaryKey = context.ResourceID
		} else if id := metaValues.Get(res.PrimaryFieldName()); id != nil {
			primaryKey = utils.ToString(id.Value)
		}

		if primaryKey != "" {
			if metaValues != nil {
				if destroy := metaValues.Get("_destroy"); destroy != nil {
					if fmt.Sprintf("%v", destroy.Value) != "0" {
						context.GetDB().Delete(result, primaryKey)
						return resource.ErrProcessorSkipLeft
					}
				}
			}
			return context.GetDB().First(result, primaryKey).Error
		}
		return nil
	}
}

func (res *Resource) CallSearcher(result interface{}, context *qor.Context) error {
	if res.Searcher != nil {
		return res.Searcher(result, context)
	} else {
		return context.GetDB().Set("gorm:order_by_primary_key", "DESC").Find(result).Error
	}
}

func (res *Resource) CallSaver(result interface{}, context *qor.Context) error {
	if res.Saver != nil {
		return res.Saver(result, context)
	} else {
		return context.GetDB().Save(result).Error
	}<commit_msg>Extract default Saver,Deleter,Finder<commit_after>package admin

import (
	"fmt"

	"github.com/jinzhu/gorm"
	"github.com/qor/qor"
	"github.com/qor/qor/resource"
	"github.com/qor/qor/roles"
	"github.com/qor/qor/utils"
)

type Resource struct {
	resource.Resource
	admin       *Admin
	Config      *Config
	Metas       []*Meta
	actions     []*Action
	scopes      map[string]*Scope
	filters     map[string]*Filter
	indexAttrs  []string
	newAttrs    []string
	editAttrs   []string
	showAttrs   []string
	cachedMetas *map[string][]*Meta
}

func (res *Resource) Meta(meta *Meta) {
	meta.base = res
	meta.updateMeta()
	res.Metas = append(res.Metas, meta)
}

func (res Resource) GetAdmin() *Admin {
	return res.admin
}

func (res Resource) ToParam() string {
	return utils.ToParamString(res.Name)
}

func (res *Resource) ConvertObjectToMap(context qor.Contextor, value interface{}, metas []*Meta) interface{} {
	var metaors []resource.Metaor
	for _, meta := range metas {
		metaors = append(metaors, meta)
	}
	return resource.ConvertObjectToMap(context, value, metaors)
}

func (res *Resource) Decode(contextor qor.Contextor, value interface{}) (errs []error) {
	return resource.Decode(contextor, value, res)
}

func (res *Resource) DefaultFinder(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
	var primaryKey string
	if metaValues == nil {
		primaryKey = context.ResourceID
	} else if id := metaValues.Get(res.PrimaryFieldName()); id != nil {
		primaryKey = utils.ToString(id.Value)
	}

	if primaryKey != "" {
		if metaValues != nil {
			if destroy := metaValues.Get("_destroy"); destroy != nil {
				if fmt.Sprintf("%v", destroy.Value) != "0" {
					context.GetDB().Delete(result, primaryKey)
					return resource.ErrProcessorSkipLeft
				}
			}
		}
		return context.GetDB().First(result, primaryKey).Error
	}
	return nil
}

func (res *Resource) CallFinder(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
	if res.Finder != nil {
		return res.Finder(result, metaValues, context)
	} else {
		return res.DefaultFinder(result, metaValues, context)
	}
}

func (res *Resource) DefaultSearcher(result interface{}, context *qor.Context) error {
	return context.GetDB().Set("gorm:order_by_primary_key", "DESC").Find(result).Error
}

func (res *Resource) CallSearcher(result interface{}, context *qor.Context) error {
	if res.Searcher != nil {
		return res.Searcher(result, context)
	} else {
		return res.DefaultSearcher(result, context)
	}
}

func (res *Resource) DefaultSaver(result interface{}, context *qor.Context) error {
	return context.GetDB().Save(result).Error
}

func (res *Resource) CallSaver(result interface{}, context *qor.Context) error {
	if res.Saver != nil {
		return res.Saver(result, context)
	} else {
		return res.DefaultSaver(result, context)
	}
{'fraction_non_alphanumeric': 0.13648596693579393, 'fraction_numerical': 0.00038446751249519417, 'mean_word_length': 4.282233502538071, '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': '5477098', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1770, 'n_words': 416}