Can someone give an active write example for pluginaweek - statemachine?

Can someone give a simple example on how to use pluginaweek state_machine to model a ticket with an active record? I do not understand the complex examples from the docs.

Examples of conditions:

  • new β†’ accepted, rejected, feedback
  • accepted β†’ resolved or feedback
  • feedback β†’ accepted or resolved
+4
source share
1 answer

Ticket model example (not verified)

class Ticket < ActiveRecord::Base attr_accessible :name, :description attr_accessible :state_event validates :name, :presence => true state_machine :initial => :new do event :accept do transition [:new, :feedback] => :accepted end event :decline do transition :new => :declined end event :feedback do transition [:new, :accepted] => :feedbacked end event :solve do transition [:accepted, :feedback] => :solved end end end 

Get all possible transitions in the form

 <%= f.collection_select :state_event, @ticket.state_transitions, :event, :human_to_name, :include_blank => @ticket.human_state_name %> 

Get ticket status: <%= ticket.state %>

Get all possible transitions as links:

 <% ticket.state_transitions.each do |transition| %> <%= link_to transition.event, ticket_path(ticket, ticket: {:state_event => transition.event}), :method => :put %> <% end %> 

List all possible transitions for filtering in the controller

 <ul> <li class="<%= 'active' if params[:state].blank? %>"><%= link_to 'All', tickets_path %></li> <% Ticket.state_machine.states.each do |state| %> <li class="<%= 'active' if params[:state] == state.name.to_s %>"> <%= link_to state.name, tickets_path(:state => state.name) %> </li> <% end %> </ul> class TicketsController extends ApplicationController ... def index @tickets = Ticket.where("state = ?", params[:state]) ... 
+6
source

Source: https://habr.com/ru/post/1399691/


All Articles