简体   繁体   English

扩展gem / engine模型,使其包含来自主应用程序的acts_as_tenant

[英]Extending gem/engine models to include acts_as_tenant from main app

There are a few post on this subject but the light has not turned on yet. 关于这个主题有几篇文章,但是灯还没有打开。

I'm trying to extend a rails gem/engine Plutus to use acts_as_tenant 我正在尝试扩展Rails gem / engine Plutus以使用acts_as_tenant

Plutus provides a double entry accounting system for an application. Plutus为应用程序提供了双重记帐系统。 One of the limitations is that the design allows for only one customer or one set of books. 局限性之一是该设计仅允许一个客户或一组书籍。 What I am trying to do is add multi-tenancy using acts_as_tenant with as little as possible modifications to the Plutus engine. 我正在尝试做的是使用act_as_tenant添加多租户,同时对Plutus引擎进行尽可能少的修改。 The goal is not to significantly alter Plutus with a different fork, but to add a few optional methods or attributes to Plutus that are only used if you want multiple accounts. 目的不是要用其他分叉来显着改变Plutus,而是要向Plutus添加一些可选方法或属性,这些方法或属性仅在需要多个帐户时才使用。

I have it semi-working, but need help in finding where to put stuff and help in clear up what is not working. 我有一半工作,但需要帮助找到放置在哪里的东西,并帮助清理不起作用的地方。 The condensed Plutus models are: 精简的Plutus模型是:

class Account < ActiveRecord::Base
    has_many :credit_amounts, :extend => AmountsExtension
    has_many :debit_amounts, :extend => AmountsExtension
    has_many :credit_transactions, :through => :credit_amounts, :source => :transaction
    has_many :debit_transactions, :through => :debit_amounts, :source => :transaction
end

class Amount < ActiveRecord::Base
  belongs_to :transaction
  belongs_to :account
end

class Transaction < ActiveRecord::Base
   belongs_to :commercial_document, :polymorphic => true
   has_many :credit_amounts, :extend => AmountsExtension
   has_many :debit_amounts, :extend => AmountsExtension
   has_many :credit_accounts, :through => :credit_amounts, :source => :account
   has_many :debit_accounts, :through => :debit_amounts, :source => :account
end

Then sti classes on Account: Asset, Equity, Expense, Liability, Revenue and sti classes on Amount: DebitAmount, CreditAmount. 然后,在帐户上的sti类:资产,权益,费用,负债,收入和在金额上的sti类:DebitAmount,CreditAmount。 This is a little beyond my rails knowledge but this may be on of the most compact double entry schemes I've ever seen (I'm not an accountant, but I have had to add accounting features on apps in the past). 这有点超出我的理解范围,但这可能是我见过的最紧凑的两次录入方案的一部分(我不是会计师,但过去我不得不在应用程序上添加记账功能)。

Semi-working means that the only thing I've modified in Plutus is adding a tenant_id to the three models and getting acts_as_tenant to extend two of the three models. 半工作意味着我在Plutus中修改的唯一一件事就是向这三个模型添加一个tenant_id ,并让acts_as_tenant扩展这三个模型中的两个。 From the console on the main application I've found that: 在主应用程序的控制台上,我发现:

Plutus::Account.acts_as_tenant(:tenant)
Plutus::Amount.acts_as_tenant(:tenant)
Plutus::Transaction.acts_as_tenant(:tenant)

works for Account and Transaction, but errors on Amount with uninitialized constant Transaction , and I'm not sure why. 适用于帐户和交易,但未uninitialized constant Transaction金额错误,我不确定为什么。 Any ideas? 有任何想法吗?

I've read the rails guide on engines and extending with decorators or concerns, but have not figured out how to send acts_as_tenant(:tenant) to the model using those approaches. 我已经阅读了有关引擎的Rails指南,并扩展了装饰器或关注点,但还没有弄清楚如何使用这些方法将acts_as_tenant(:tenant)发送到模型。 Where would I put those three lines of code in the main application (providing I figure out how to get Amount to work!)? 我应该将这三行代码放在主应用程序的哪里(前提是我弄清楚如何使Amount起作用!)?

Is there a better approach? 有没有更好的方法?

I still have a few class methods that I will have to extend or modify, but no use trying that until I get over this first hurdle. 我仍然有一些类方法需要扩展或修改,但是在克服第一个障碍之前,尝试使用这些方法是没有用的。

The problem was that Plutus is a name-spaced engine, and while it works fine within the engine, calling it from outside the engine (main app) could raise conflicts. 问题在于,Plutus是一个以名称分隔的引擎,尽管它在引擎内运行良好,但从引擎(主应用程序)外部调用它可能会引起冲突。

To fix it, a class_name option was added to the associations. 为了解决这个问题,在关联中添加了class_name选项。

module Plutus
    class Amount < ActiveRecord::Base
      belongs_to :transaction, class_name:"Plutus::Transaction"
      belongs_to :account, class_name:"Plutus::Account"

      validates_presence_of :type, :amount, :transaction, :account
    end
end

Still never figured out the best place to stick the ActsAsTenant calls. 仍然从来没有想出最好的方法来ActsAsTenant调用。 I stuck them in the Concerns directory and they didn't get called. 我将它们粘贴在“关注问题”目录中,但没有被调用。 Ended up putting them in my Tenant model and all is fine. 最终将它们放入我的Tenant模型中,一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM