有人可以给出一个有效记录的例子pluginaweek - 的statemachine? [英] Can somebody give an active record example for pluginaweek - statemachine?

查看:105
本文介绍了有人可以给出一个有效记录的例子pluginaweek - 的statemachine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供有关如何使用它们 pluginaweek state_machine 一个简单的例子,一票模型的活动记录? 我不明白,从文档中复杂的例子。

Can somebody give a simple example on howto use pluginaweek state_machine for a ticket model with active record? I do not understand the complex examples from the docs.

举例指出:

  • 新建 - >接受,谢绝,反馈
  • 在接受 - >解决或反馈
  • 反馈 - >接受或解决

推荐答案

为例票模型(未测试)

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 form

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

获取车票的状态:&LT;%= ticket.state%&GT;

获取所有可能的票转换为链接:

Get all possible ticket 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 to filter in 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])
    ...       

这篇关于有人可以给出一个有效记录的例子pluginaweek - 的statemachine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆