按用户数量限制多租户应用程序帐户

Limiting Multitenant Application Account By Number Of Users

所以我正在使用 Rails 4 和公寓 Gem 构建一个应用程序。

我对多租户还很陌生,已经到了制定用户计划的地步。

我有 4 个主要计划,我希望每个计划都限制可以创建的用户数量,然后再告诉帐户所有者或管理员他们需要升级才能注册新用户。

我认为我更坚持应该在哪里完成。在我的帐户模型中,我有我的计划常量:

PLAN = %w[responder first_responder patrol_pro guardian]

每个 plan_types(我在注册时保存到的列)都有一定数量的可以创建的用户,这些也设置为下面的常量:

  RESPONDER_PLAN_USER_LIMIT = 6
  FIRST_RESPONDER_PLAN_USER_LIMIT = 12
  PATROL_PRO_PLAN_USER_LIMIT = 30
  GUARDIAN_PLAN_USER_LIMIT = 60

我还创建了以下方法来验证计划类型,但这看起来很笨拙,我想知道是否有一种方法可以用一种方法而不是 4 种方法来做到这一点

  def responder_plan?
    self.plan_type == 'responder'
  end

  def first_responder_plan?
    self.plan_type == 'first_responder'
  end

  def patrol_pro_plan?
    self.plan_type == 'patrol_pro'
  end

  def guardian_plan?
    self.plan_type == 'guardian'
  end

现在是最后一个问题,为了实际计算附加到我计划使用验证程序的帐户的用户,我只是不确定如何创建模型方法。

我在想这样的事情:

def over_user_limit?
  self.individual_plan? && self.users.count > INDIVIDUAL_USER_LIMIT
end

但我不知道如何对多种用户类型执行此操作?

如有任何帮助,我们将不胜感激。

编辑 1:尝试将枚举添加到我的表单时出现错误消息

SyntaxError at /accounts/new
syntax error, unexpected tIDENTIFIER, expecting ')'
    first_responder:  12
                   ^
/Users/developer/Desktop/PatrolVault-Saas/PV_SAAS/app/models/plan.rb:7: syntax error, unexpected ':', expecting keyword_end
    patrol_pro:       30
               ^
/Users/developer/Desktop/PatrolVault-Saas/PV_SAAS/app/models/plan.rb:8: syntax error, unexpected ':', expecting keyword_end
    guardian:         60
             ^

为简洁起见,这是我的表单字段:

  <%= f.fields_for :plan do |plan| %>
  <div class="col-xs-12">
    <%= f.select :plan_type, options_for_select(Plan.plan_types.map {|k, v| [k.humanize.capitalize, k]}) %>
  </div>
  <% end %>

编辑#2 引用枚举的剩余错误消息

这是我的计划模型:

enum :plan_type, [:responder, :first_responder, :patrol_pro, :guardian]

USER_LIMITS = ActiveSupport::HashWithIndifferentAccess.new( 回复者:6, first_responder: 12, patrol_pro: 30, 守护者:60 )

这是错误消息:

并且表单项没有改变。

主要问题是责任分配很混乱。

我将从建立两个职责非常明确的模型开始:

class Account < ActiveRecord::Base
  belongs_to :company
  has_one :plan
end

class Plan < ActiveRecord::Base
  belongs_to :account
end

为不同类型的计划定义规则的逻辑绝对不是帐户模型工作。

所以让我们实施计划中的规则 class:

class Plan < ActiveRecord::Base
  belongs_to :account
  # declare the column plans.plan_type as integer.
  enum plan_type: [:responder, :first_responder, :patrol_pro, :guardian]
  USER_LIMITS = ActiveSupport::HashWithIndifferentAccess.new(
    responder:        6,
    first_responder:  12,
    patrol_pro:       30,
    guardian:         60
  )

  def user_limit
    USER_LIMITS[self.plan_type]
  end
end

然后您可以实施验证:

class Plan < ActiveRecord::Base
  belongs_to :account
  # declare the column plans.plan_type as integer.
  enum plan_type: [:responder, :first_responder, :patrol_pro, :guardian]
  USER_LIMITS = ActiveSupport::HashWithIndifferentAccess.new(
    responder:        6,
    first_responder:  12,
    patrol_pro:       30,
    guardian:         60
  )

  validates :must_be_below_user_limit 

  def user_limit
    USER_LIMITS[self.plan_type]
  end

  def must_be_below_user_limit
   if self.account.users.size >= user_limit
     errors[:user_limit] = "can not more than #{user_limit} users"
   end
  end
end