Module: PopulateMe::DocumentMixins::Callbacks
- Included in:
- PopulateMe::Document
- Defined in:
- lib/populate_me/document_mixins/callbacks.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #ensure_delete_attachments ⇒ Object
- #ensure_delete_related ⇒ Object
- #ensure_id ⇒ Object
- #ensure_new ⇒ Object
- #ensure_not_new ⇒ Object
- #ensure_position ⇒ Object
- #exec_callback(name) ⇒ Object
- #recurse_callback(name) ⇒ Object
Class Method Details
.included(base) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/populate_me/document_mixins/callbacks.rb', line 76 def self.included(base) base.extend(ClassMethods) base.class_eval do [:save,:create,:update,:delete].each do |cb| before cb, :recurse_callback after cb, :recurse_callback end before :create, :ensure_id before :create, :ensure_position after :create, :ensure_not_new after :save, :snapshot before :delete, :ensure_delete_related before :delete, :ensure_delete_attachments after :delete, :ensure_new end end |
Instance Method Details
#ensure_delete_attachments ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/populate_me/document_mixins/callbacks.rb', line 66 def self.class.fields.each do |k,v| if v[:type]==:attachment self.(k).delete_all end end end |
#ensure_delete_related ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/populate_me/document_mixins/callbacks.rb', line 54 def self.class.relationships.each do |k,v| if v[:dependent] klass = WebUtils.resolve_class_name v[:class_name] next if klass.nil? klass.admin_find(query: {v[:foreign_key]=>self.id}).each do |d| d.delete end end end end |
#ensure_id ⇒ Object
27 28 29 30 31 32 |
# File 'lib/populate_me/document_mixins/callbacks.rb', line 27 def ensure_id if self.id.nil? self.id = SecureRandom.hex end self end |
#ensure_new ⇒ Object
74 |
# File 'lib/populate_me/document_mixins/callbacks.rb', line 74 def ensure_new; self._is_new = true; end |
#ensure_not_new ⇒ Object
34 |
# File 'lib/populate_me/document_mixins/callbacks.rb', line 34 def ensure_not_new; self._is_new = false; end |
#ensure_position ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/populate_me/document_mixins/callbacks.rb', line 36 def ensure_position self.class.fields.each do |k,v| if v[:type]==:position return unless self.__send__(k).nil? values = if v[:scope].nil? self.class.admin_distinct k else self.class.admin_distinct(k, query: { v[:scope] => self.__send__(v[:scope]) }) end val = values.empty? ? 0 : (values.max + 1) self.set k => val end end self end |
#exec_callback(name) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/populate_me/document_mixins/callbacks.rb', line 7 def exec_callback name name = name.to_sym return self if self.class.callbacks[name].nil? self.class.callbacks[name].each do |job| if job.respond_to?(:call) self.instance_exec name, &job else meth = self.method(job) meth.arity==1 ? meth.call(name) : meth.call end end self end |
#recurse_callback(name) ⇒ Object
21 22 23 24 25 |
# File 'lib/populate_me/document_mixins/callbacks.rb', line 21 def recurse_callback name nested_docs.each do |d| d.exec_callback name end end |