Module: CckForms::NeofilesDenormalize::ClassMethods
- Defined in:
- lib/cck_forms/neofiles_denormalize.rb
Instance Method Summary collapse
-
#neofiles_attrs(obj) ⇒ Object
Returns all fields of Neofiles::File obj to be denormalized.
-
#neofiles_attrs_or_id(obj_or_id, klass = ::Neofiles::File) ⇒ Object
Returns all fields of Neofiles::File to be denormalized or the object ID if the object itself can not be found.
-
#neofiles_lazy_loadable(obj) ⇒ Object
Makes obj lazy load fields NEOFILES_LAZY_ATTRS.
-
#neofiles_mock(attrs, klass) ⇒ Object
Constructs a Mongoid::Document of class klass with attrs as if it was a usual document loaded from MongoDB.
-
#neofiles_mock_or_load(attrs_or_id, klass = ::Neofiles::File) ⇒ Object
If attrs_or_id is a Hash, constructs a mock from it.
Instance Method Details
#neofiles_attrs(obj) ⇒ Object
Returns all fields of Neofiles::File obj to be denormalized
13 14 15 |
# 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) ⇒ Object
Returns all fields of Neofiles::File to be denormalized or the object ID if the object itself can not be found
18 19 20 21 22 23 24 25 26 27 28 |
# 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) ⇒ Object
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)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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) ⇒ Object
Constructs a Mongoid::Document of class klass with attrs as if it was a usual document loaded from MongoDB
31 32 33 34 35 |
# 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) ⇒ Object
If attrs_or_id is a Hash, constructs a mock from it. Otherwise, load an object by its ID
38 39 40 41 42 43 44 45 46 |
# 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 |