Module: Associo::ClassMethods
- Defined in:
- lib/associo/class_methods.rb
Overview
Define new class methods for documents that include the Associo module.
Constant Summary collapse
- DEFAULT_CHUNK_SIZE =
261_120
Instance Method Summary collapse
-
#attachment(name, options = {}) ⇒ Object
rubocop:disable AbcSize, MethodLength TODO: refactor this.
- #attachment_accessor_module ⇒ Object
Instance Method Details
#attachment(name, options = {}) ⇒ Object
rubocop:disable AbcSize, MethodLength TODO: refactor this.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/associo/class_methods.rb', line 13 def (name, = {}) .symbolize_keys! name = name.to_sym chunk_size = .fetch(:chunk_size, DEFAULT_CHUNK_SIZE).to_i chunk_size = DEFAULT_CHUNK_SIZE if chunk_size.zero? self. = .dup.add(name) after_save :save_attachments before_save :nullify_nil_attachments_attributes after_save :destroy_nil_attachments before_destroy :destroy_all_attachments key :"#{name}_id", ObjectId key :"#{name}_name", String key :"#{name}_size", Integer key :"#{name}_type", String # Allow for optional, custom chunk size, in bytes. # Default size is 255k, set above. key :"#{name}_chunk_size", Integer, default: chunk_size validates_presence_of(name) if [:required] .module_eval <<-EOC def #{name} @#{name} ||= AttachmentProxy.new(self, :#{name}) end def #{name}? !nil_attachments.has_key?(:#{name}) && send(:#{name}_id?) end def #{name}=(file) if file.nil? nil_attachments[:#{name}] = send("#{name}_id") assigned_attachments.delete(:#{name}) else send("#{name}_id=", BSON::ObjectId.new) if send("#{name}_id").nil? send("#{name}_name=", Associo::FileHelpers.name(file)) send("#{name}_size=", Associo::FileHelpers.size(file)) send("#{name}_type=", Associo::FileHelpers.type(file)) assigned_attachments[:#{name}] = file nil_attachments.delete(:#{name}) end end EOC end |
#attachment_accessor_module ⇒ Object
7 8 9 |
# File 'lib/associo/class_methods.rb', line 7 def @attachment_accessor_module ||= Module.new end |