actioncontroller

package
v0.0.8-b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ActionCreate  = "create"
	ActionUpdate  = "update"
	ActionShow    = "show"
	ActionIndex   = "index"
	ActionDestroy = "destroy"
)

Variables

This section is empty.

Functions

func IsCanonicalAction

func IsCanonicalAction(actionName string) bool

Types

type AbstractController

type AbstractController interface {
	ActionMethods() []Action
}

type AbstractModel

type AbstractModel interface {
	Name() string
	PrimaryKey() string
	AttributeNames() []string
	AttributeForInspect(attrName string) activerecord.Attribute
	AttributesForInspect(attrNames ...string) []activerecord.Attribute
	ReflectOnAllAssociations() []*activerecord.AssociationReflection
}

type Action

type Action interface {
	ActionName() string
	ActionConstraints() Constraints
	Process(ctx *Context) Result
}

type ActionController

type ActionController struct {
	// contains filtered or unexported fields
}

func Initialize

func Initialize(init func(*C)) (*ActionController, error)

func New

func New(init func(*C)) *ActionController

func (*ActionController) Action

func (c *ActionController) Action(actionName string) Action

func (*ActionController) ActionMethods

func (c *ActionController) ActionMethods() []Action

func (*ActionController) HasAction

func (c *ActionController) HasAction(actionName string) bool

type ActionFunc

type ActionFunc func(*Context) Result

func (ActionFunc) Process

func (fn ActionFunc) Process(ctx *Context) Result

type C

type C struct {
	// contains filtered or unexported fields
}

func (*C) Action

func (c *C) Action(name string, a ActionFunc)

func (*C) AfterAction

func (c *C) AfterAction(cb Callback, only ...string)

func (*C) AppendAfterAction

func (c *C) AppendAfterAction(cb Callback, only ...string)

func (*C) AppendAroundAction

func (c *C) AppendAroundAction(cb CallbackAround, only ...string)

AppendAroundAction appends a callback around action. See CallbackAround for parameter details.

Use the around callback to wrap the action with extra logic, e.g. execute all operations within an action in a database transaction.

func WrapInTransaction(
	ctx *actioncontroller.Context, action actioncontroller.Action
) (result actioncontroller.Result) {
	err := activerecord.Transaction(ctx, func() error {
		result = action.Process(ctx)
		return result.Err()
	})
	if err != nil {
		return actionview.ContentResult(nil, err)
	}
	return nil
}

ProductsController := actioncontroller.New(func(c *actioncontroller.C) {
	c.AppendAroundAction(WrapInTransaction)
})

func (*C) AppendBeforeAction

func (c *C) AppendBeforeAction(cb Callback, only ...string)

AppendBeforeAction appends a callback before actions. See Callback for parameter details.

Use the before callback to execute necessary logic before executing an action, the implementation could completely override the behavior of the action.

The chain of "before" callbacks execution interrupts when the non-nil result is returned. It's anticipated that before filters are often used to prevent from execution certain operations or queries.

AdminController := actioncontroller.New(func(c *actioncontroller.C) {
	c.AppendBeforeAction(func(ctx *actioncontroller.Context) actioncontroller.Result {
		if !isAuthorized(ctx) {
			return actionview.ContentResult(nil, errors.New("forbidden"))
		}
		return nil
	})
})

func (*C) AroundAction

func (c *C) AroundAction(cb CallbackAround, only ...string)

func (*C) BeforeAction

func (c *C) BeforeAction(cb Callback, only ...string)

func (*C) Create

func (c *C) Create(a ActionFunc)

func (*C) Destroy

func (c *C) Destroy(a ActionFunc)

func (*C) Index

func (c *C) Index(a ActionFunc)

func (*C) Permit

func (c *C) Permit(params []activerecord.Attribute, names ...string)

func (*C) Show

func (c *C) Show(a ActionFunc)

func (*C) Update

func (c *C) Update(a ActionFunc)

type Callback

type Callback func(ctx *Context) Result

type CallbackAround

type CallbackAround func(ctx *Context, action Action) Result

type Constraints

type Constraints struct {
	Request  *StrongParameters
	Response *StrongParameters
	Match    Matcher
}

type Context

type Context struct {
	context.Context

	Params    Parameters
	Selection []QueryAttribute
}

type ErrActionNotFound

type ErrActionNotFound struct {
	ActionName string
}

ErrActionNotFound is returned when a non-existing action is triggered.

func (ErrActionNotFound) Error

func (e ErrActionNotFound) Error() string

Error returns a string representation of the error.

type Mapper

type Mapper interface {
	Resources(AbstractModel, AbstractController)

	// Match matches path to an action.
	Match(via, path string, action Action, constraints ...Constraints)

	Map() (http.Handler, error)
}

type Matcher

type Matcher interface {
	Matches(*Request) bool
}

type NamedAction

type NamedAction struct {
	Name        string
	Constraints Constraints
	ActionFunc
}

func (*NamedAction) ActionConstraints

func (a *NamedAction) ActionConstraints() Constraints

func (*NamedAction) ActionName

func (a *NamedAction) ActionName() string

type Parameters

type Parameters activesupport.Hash

func (Parameters) Get

func (p Parameters) Get(key string) Parameters

type QueryAttribute

type QueryAttribute struct {
	AttributeName    string
	NestedAttributes []QueryAttribute
}

func (QueryAttribute) NestedAttributeNames

func (qa QueryAttribute) NestedAttributeNames() []string

type Request

type Request struct {
}

type Response

type Response struct {
}

type Result

type Result interface {
	Execute(*Context) (interface{}, error)
}

Result defines a contract that represents the result of action method.

type StrongParameters

type StrongParameters struct {
	Attributes []activerecord.Attribute
}

func Require

func Require(rel *activerecord.Relation) *StrongParameters

func (*StrongParameters) Permit

func (p *StrongParameters) Permit(names ...string) *StrongParameters

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL