如何在关注中定义state_machine?

 悦爱张晓桀 发布于 2023-02-12 17:59

我试图将一些重复的逻辑分解为一个问题.部分重复逻辑是state_machine.

简化的Database,Site,SftpUser多包含,除其他外,这样的:

class Database < ActiveRecord::Base
  # ...
  state_machine :deploy_state, initial: :halted do
    state :pending
  end
end

我试图将此重构为一个问题:

module Deployable
  extend ActiveSupport::Concern

  included do
    state_machine :deploy_state, initial: :halted do
      state :pending
    end
  end
end

# Tested with:
class DeployableDouble < ActiveRecord::Base
  extend Deployable
end

describe DeployableDouble do
  let(:subject) { DeployableDouble.new }

  it "should have default state halted" do
    subject.deploy_state.must_equal "halted"
  end
end

但是,这不是在a state_machnine中实现a的正确方法concern,因为这会导致:NoMethodError: undefined method 'deploy_state' for .这表明Double根本没有分配一个状态机.

included do实际上是实际上是正确的回调吗?它可能是一个问题state_machine,它需要一个ActiveRecord :: Base的子类吗?我得不到的东西?我很关注忧虑的概念.

1 个回答
  • 好的.我感觉真的很蠢.不应该extend是具有模块的类,而是include该模块.明显.

    # Tested with:
    class DeployableDouble
      include Deployable
    end
    

    一旦写完你就会监督的其中一件事.此外,ActiveRecord::Base不需要扩展,因为state_machine只是普通的旧Ruby,适用于通用的Ruby对象.

    2023-02-12 18:02 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有