module CckForms::NeofilesDenormalize::ClassMethods
Public Instance Methods
neofiles_attrs(obj)
click to toggle source
Returns all fields of Neofiles::File obj to be denormalized
# File lib/cck_forms/neofiles_denormalize.rb, line 13 def neofiles_attrs(obj) obj.attributes.with_indifferent_access.except *NEOFILES_LAZY_ATTRS end
neofiles_attrs_or_id(obj_or_id, klass = ::Neofiles::File)
click to toggle source
Returns all fields of Neofiles::File to be denormalized or the object ID if the object itself can not be found
# File lib/cck_forms/neofiles_denormalize.rb, line 18 def neofiles_attrs_or_id(obj_or_id, klass = ::Neofiles::File) if obj_or_id.present? obj, id = if obj_or_id.is_a? klass [obj_or_id, nil] elsif obj_or_id.is_a?(::String) || obj_or_id.is_a?(::BSON::ObjectId) [::Neofiles::File.where(id: obj_or_id).first, obj_or_id.to_s] end obj.try { |x| neofiles_attrs(x) } || id end end
neofiles_lazy_loadable(obj)
click to toggle source
Makes obj lazy load fields NEOFILES_LAZY_ATTRS. That is, when these fields are accessed vie getters or read_attribute, make a request to MongoDB to fetch fresh data (all at once)
Calls superclass method
# File lib/cck_forms/neofiles_denormalize.rb, line 50 def neofiles_lazy_loadable(obj) def obj.__lazy_load return if @__lazy_loaded @__lazy_loaded = true from_db = self.class.find(id) attributes.merge! from_db.attributes.with_indifferent_access.slice(*NEOFILES_LAZY_ATTRS) end def obj.read_attribute(field) __lazy_load if field.in? NEOFILES_LAZY_ATTRS super(field) end NEOFILES_LAZY_ATTRS.each do |field| obj.define_singleton_method field do __lazy_load super() end end end
neofiles_mock(attrs, klass)
click to toggle source
Constructs a Mongoid::Document of class klass with attrs as if it was a usual document loaded from MongoDB
# File lib/cck_forms/neofiles_denormalize.rb, line 31 def neofiles_mock(attrs, klass) Mongoid::Factory.from_db(klass, attrs).tap do |obj| neofiles_lazy_loadable obj end end
neofiles_mock_or_load(attrs_or_id, klass = ::Neofiles::File)
click to toggle source
If attrs_or_id is a Hash, constructs a mock from it. Otherwise, load an object by its ID
# File lib/cck_forms/neofiles_denormalize.rb, line 38 def neofiles_mock_or_load(attrs_or_id, klass = ::Neofiles::File) if attrs_or_id.present? case attrs_or_id when ::String then klass.where(id: attrs_or_id).first when ::BSON::ObjectId then klass.where(id: attrs_or_id).first when ::Hash then neofiles_mock(attrs_or_id.with_indifferent_access, klass) end end end