我想知道你们如何使用工作流程或控制器中的AASM gem,如果你想更新所有属性,还需要工作流/ AASM回调才能正常启动。
目前,我这样使用它:
class ModelController < ApplicationController
def update
@model = model.find(params[:id])
if params[:application]['state'].present?
if params[:application]['state'] == "published"
@model.publish!
end
end
if @model.update_attributes(params[:application]); ... end
end
end
感觉不对,什么是更好的解决方案?
答案 0 :(得分:4)
我通常定义多个动作来处理从一个状态到另一个状态的转换并具有明确的名称。在您的情况下,我建议您添加publish
操作:
def publish
# as the comment below states: your action
# will have to do some error catching and possibly
# redirecting; this goes only to illustrate my point
@story = Story.find(params[:id])
if @story.may_publish?
@story.publish!
else
# Throw an error as transition is not legal
end
end
在routes.rb
:
resources :stories do
member do
put :publish
end
end
现在,您的路线会准确反映故事的内容:/stories/1234/publish
答案 1 :(得分:2)
您可以覆盖模型aasm_state setter(或我的示例中的状态),以便它可以接受事件名称。然后我们检查它是否是有效事件,然后检查转换是否有效。如果不是,我们会添加正确的错误消息。
请求规范
it "should cancel" do
put "/api/ampaigns/#{@campaign.id}", {campaign: {status: "cancel"}, format: :json}, valid_session
response.code.should == "204"
end
型号规格
it "should invoke the cancel method" do
campaign.update_attribute(:status, "cancel")
campaign.canceled?.should be_true
end
it "should add an error for illegal transition" do
campaign.update_attribute(:status, "complete")
campaign.errors.should include :status
campaign.errors[:status].should == ["status cannot transition from pending to complete"]
end
it "should add an error for invalid status type" do
campaign.update_attribute(:status, "foobar")
campaign.errors.should include :status
campaign.errors[:status].should == ["status of foobar is not valid. Legal values are pending, active, canceled, completed"]
end
模型
class Campaign < ActiveRecord::Base
include AASM
aasm column: :status do
state :pending, :initial => true
state :active
state :canceled
state :completed
# Events
event :activate do
transitions from: :pending, to: :active
end
event :complete do
transitions from: :active, to: [:completed]
end
event :cancel do
transitions from: [:pending, :active], to: :canceled
end
end
def status=(value)
if self.class.method_defined?(value)
if self.send("may_#{value}?")
self.send(value)
else
errors.add(:status, "status cannot transition from #{status} to #{value}")
end
else
errors.add(:status, "status of #{value} is not valid. Legal values are #{aasm.states.map(&:name).join(", ")}")
end
end
end
答案 2 :(得分:0)
这是一个小东西,但是如果该东西不存在则哈希返回为零,那么你可以删除要出席的调用吗?
我意识到这不是你所要求的,当然。另一种方法是在模型中放置一个前置过滤器,然后检查状态。这使您的控制器对您的状态的底层存储视而不见。
另一方面,我们在这里使用AASM并且我喜欢它:)
答案 3 :(得分:0)
我希望我的模型在更新后返回新状态,这是我能想到的最简单的方法,没有很多&#34;胖&#34;在控制器中,如果您的工作流程发生变化,它可以更容易地继续前进:
class Article < ActiveRecord::Base
include Workflow
attr_accessible :workflow_state, :workflow_event # etc
validates_inclusion_of :workflow_event, in: %w(submit approve reject), allow_nil: true
after_validation :send_workflow_event
def workflow_event
@workflow_event
end
def workflow_event=(workflow_event)
@workflow_event = workflow_event
end
# this method should be private, normally, but I wanted to
# group the meaningful code together for this example
def send_workflow_event
if @workflow_event && self.send("can_#{@workflow_event}?")
self.send("#{@worklow_event}!")
end
end
# I pulled this from the workflow website, to use that example instead.
workflow do
state :new do
event :submit, :transitions_to => :awaiting_review
end
state :awaiting_review do
event :review, :transitions_to => :being_reviewed
end
state :being_reviewed do
event :accept, :transitions_to => :accepted
event :reject, :transitions_to => :rejected
end
state :accepted
state :rejected
end
end