Module: Mongoid::Denormalize
- Defined in:
- lib/mongoid-denormalize.rb,
lib/mongoid-denormalize/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.4.0"
Class Method Summary collapse
-
.add_hook_to_child(child_class, fields, options) ⇒ Object
Add hook to child class to denormalize fields when parent relation is changed.
-
.add_hook_to_parent(child_class, fields, options) ⇒ Object
Add hook to parent class to denormalize fields when parent object is updated.
-
.field_type(klass, field) ⇒ Object
Retreive the type of a field from the given class.
-
.get_fields_with_names(child_class, fields, options) ⇒ Object
Check options and return name for each field.
- .included(base) ⇒ Object
-
.parent_class(child_class, from) ⇒ Object
Retrieve parent class.
Class Method Details
.add_hook_to_child(child_class, fields, options) ⇒ Object
Add hook to child class to denormalize fields when parent relation is changed
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mongoid-denormalize.rb', line 54 def self.add_hook_to_child(child_class, fields, ) from = [:from].to_s child_class.send([:child_callback] || 'before_save') do if send("#{from}_id_changed?") fields.each do |field| send("#{field[:as]}=", send(from)&.send(field[:name])) end end end end |
.add_hook_to_parent(child_class, fields, options) ⇒ Object
Add hook to parent class to denormalize fields when parent object is updated
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mongoid-denormalize.rb', line 67 def self.add_hook_to_parent(child_class, fields, ) from = [:from].to_s parent = parent_class(child_class, from) relation = parent.relations[child_class.relations[from].inverse_of.to_s] || parent.relations[child_class.model_name.plural] || parent.relations[child_class.model_name.singular] unless relation raise "Option :inverse_of is needed for 'belongs_to :#{from}' into #{child_class}." end parent.after_update do attributes = {} fields.each do |field| attributes[field[:as]] = send(field[:name]) if send("#{field[:name]}_changed?") end next if attributes.blank? case relation.relation.to_s when 'Mongoid::Relations::Referenced::One', 'Mongoid::Association::Referenced::HasOne::Proxy' if (document = send(relation.name)) document.collection.update_one({_id: document._id}, {'$set' => attributes}) end when 'Mongoid::Relations::Referenced::Many', 'Mongoid::Association::Referenced::HasMany::Proxy' send(relation.name).update_all('$set' => attributes) else raise "Relation type unsupported: #{relation.relation}" end end end |
.field_type(klass, field) ⇒ Object
Retreive the type of a field from the given class
109 110 111 |
# File 'lib/mongoid-denormalize.rb', line 109 def self.field_type(klass, field) klass.fields[field.to_s].[:type] end |
.get_fields_with_names(child_class, fields, options) ⇒ Object
Check options and return name for each field
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mongoid-denormalize.rb', line 29 def self.get_fields_with_names(child_class, fields, ) parent = parent_class(child_class, [:from].to_s) if .include?(:as) [:as] = [[:as]] unless [:as].is_a?(Array) unless fields.size == [:as].size raise ArgumentError, 'When option :as is used you must pass a name for each field.' end return fields.map.with_index do |field, index| {name: field, as: [:as][index], type: field_type(parent, field)} end elsif .include?(:prefix) return fields.map do |field| {name: field, as: "#{[:prefix]}_#{field}", type: field_type(parent, field)} end end fields.map do |field| {name: field, as: "#{[:from]}_#{field}", type: field_type(parent, field)} end end |
.included(base) ⇒ Object
5 6 7 |
# File 'lib/mongoid-denormalize.rb', line 5 def self.included(base) base.extend ClassMethods end |
.parent_class(child_class, from) ⇒ Object
Retrieve parent class
103 104 105 106 |
# File 'lib/mongoid-denormalize.rb', line 103 def self.parent_class(child_class, from) (child_class.relations[from].class_name || child_class.relations[from].name.capitalize) .constantize end |