github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/lib/status/status.go (about)

     1  package status
     2  
     3  import (
     4  	"github.com/fragmenta/query"
     5  	"github.com/fragmenta/view/helpers"
     6  )
     7  
     8  // Status values valid in the status field added with status.ResourceStatus.
     9  const (
    10  	None      = 0
    11  	Draft     = 1
    12  	Suspended = 50
    13  	Published = 100
    14  )
    15  
    16  // ResourceStatus adds a status field to resources.
    17  type ResourceStatus struct {
    18  	Status int64
    19  }
    20  
    21  // WherePublished modifies the given query to select status greater than published.
    22  // Note this selects >= Published.
    23  func WherePublished(q *query.Query) *query.Query {
    24  	return q.Where("status >= ?", Published)
    25  }
    26  
    27  // Options returns an array of statuses for a status select.
    28  func Options() []helpers.Option {
    29  	var options []helpers.Option
    30  
    31  	options = append(options, helpers.Option{Id: Draft, Name: "Draft"})
    32  	options = append(options, helpers.Option{Id: Suspended, Name: "Suspended"})
    33  	options = append(options, helpers.Option{Id: Published, Name: "Published"})
    34  
    35  	return options
    36  }
    37  
    38  // OptionsAll returns a list of options starting with a None option using the name passed in,
    39  // which is useful for filter menus filtering on status.
    40  func OptionsAll(name string) []helpers.Option {
    41  	options := Options()
    42  	return append(options, helpers.Option{Id: None, Name: name})
    43  }
    44  
    45  // StatusOptions returns an array of statuses for a status select for this resource.
    46  func (r *ResourceStatus) StatusOptions() []helpers.Option {
    47  	return Options()
    48  }
    49  
    50  // StatusDisplay returns a string representation of the model status.
    51  func (r *ResourceStatus) StatusDisplay() string {
    52  	for _, o := range r.StatusOptions() {
    53  		if o.Id == r.Status {
    54  			return o.Name
    55  		}
    56  	}
    57  	return ""
    58  }