将Mongoid会话设为只读 [英] Make mongoid session read only

查看:95
本文介绍了将Mongoid会话设为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mongoid.yml中有不同的会话,其中一个会话提供了来自静态mongo数据库的数据.我想知道是否可以在只读模式下加载"会话,以便不能对savecreatedestroydestroy_all进行任何更改.我的mongoid.yml看起来像这样:

I have different sessions in my mongoid.yml, where one session provides data from a static mongo database. I was wondering if it is possible, to "load" a session in read only mode, so that no changes with save, create, destroy or destroy_all can be made. My mongoid.yml looks like this:

production:
  sessions:
    default:
      database: my_app_production
      hosts:
        - localhost:27017
      options:
        read: primary
    static_content:
      database: static_content_db
      hosts:
        - localhost:27017
      options:
        read: primary
  options:
    use_utc: true

对于static_content会话,我有特殊的模型,它们看起来像这样:

I have special models for the static_content session, they look like this:

  class StaticMappings
    include Mongoid::Document
    include Mongoid::Attributes::Dynamic
    store_in collection: "static_mappings", session: "static_content"
  end

我想防止自己意外地调用诸如StaticMappings.destroy_allStaticMappings.create(...)之类的东西.这可能吗?

I want to prevent myself form accidentially calling things like StaticMappings.destroy_all or a StaticMappings.create(...). Is this possible?

我发现了这个使整个模型与Mongoid保持只读状态,但这不会阻止某人在模型实例上调用createdestroy.

I found this Making an entire model read-only with Mongoid, but this won't prevent someone from calling create or destroy on a model instance.

推荐答案

这是一个老问题,但是我最近遇到了同样的问题,因此决定分享.虽然,我想注意到,这不是一个基于问题的解决方案,而是一个针对每个模型的解决方案.

That's an old question, but I met the same problem recently, so decided to share. Although, I want to notice, that this is not a per-sesson solution, but a per-model one.

我发现,有两种方法可以实现这一点:

As I figured out, there are two ways to make it possible:

如果查看Mongoid代码,将会看到所有保存,删除或更新的函数,请调用readonly?来检查模型是否为只读.没有真正记载,有一个缺点-创建和创建!允许在此模型上使用(销毁,更新,保存不会运行).

If you look through the Mongoid code, you'll see that all functions that save, delete or update, call readonly? to check if the model is read-only. Not really documented and has a drawback -- create and create! are allowed on this model (destroys, updates, saves won't run though).

private

def readonly?
  true
end

2.自定义回调

除了前面的方法外,您还可以添加回调以确保即使创建也不会通过:

2. A custom callback

In addition to the previous method you can add callback(s) to ensure that even creates won't pass through:

before_create :readonly_secret

private

def readonly?
  true
end

def readonly_secret
  raise Mongoid::Errors::ReadonlyDocument, self.class if readonly?
end

本质上,您可以一起摆脱readonly?方法,并添加其他回调,例如before_savebefore_destroybefore_updatebefore_create

Essentialy, you can get rid of readonly? method alltogether, and add other callbacks such as before_save, before_destroy, before_update, before_create

如果您需要从运行时代码中操作只读状态,则可以为模型的类定义一个属性:

if you feel a need to manipulate read-only state from a runtime code, you can define an attribute for your model's class:

before_create :readonly_secret

class << self
  attr_accessor :readonly
end

private

def readonly?
  self.class.readonly.nil? ? true : self.class.readonly
end

def readonly_secret
  raise Mongoid::Errors::ReadonlyDocument, self.class if readonly?
  true
end

这篇关于将Mongoid会话设为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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