Module: Mongoid::Denormalize

Defined in:
lib/mongoid-denormalize.rb,
lib/mongoid-denormalize/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.3.2"

Class Method Summary collapse

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, options)
  from = options[:from].to_s

  child_class.send(options[: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
# File 'lib/mongoid-denormalize.rb', line 67

def self.add_hook_to_parent(child_class, fields, options)
  from = options[: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'
      if (document = send(relation.name))
        document.collection.update_one({_id: document._id}, {'$set' => attributes})
      end
    when 'Mongoid::Relations::Referenced::Many'
      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



107
108
109
# File 'lib/mongoid-denormalize.rb', line 107

def self.field_type(klass, field)
  klass.fields[field.to_s].options[: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, options)
  parent = parent_class(child_class, options[:from].to_s)

  if options.include?(:as)
    options[:as] = [options[:as]] unless options[:as].is_a?(Array)

    unless fields.size == options[: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: options[:as][index], type: field_type(parent, field)}
    end
  elsif options.include?(:prefix)
    return fields.map do |field|
      {name: field, as: "#{options[:prefix]}_#{field}", type: field_type(parent, field)}
    end
  end

  fields.map do |field|
    {name: field, as: "#{options[: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



101
102
103
104
# File 'lib/mongoid-denormalize.rb', line 101

def self.parent_class(child_class, from)
  (child_class.relations[from].class_name || child_class.relations[from].name.capitalize)
    .constantize
end