Module: AutoGenerator

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'backend/app/model/mixins/auto_generator.rb', line 3

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'backend/app/model/mixins/auto_generator.rb', line 7

def update_from_json(json, opts = {}, apply_nested_records = true)
  self.class.properties_to_auto_generate.each do |generate_opts|
    next if generate_opts[:only_if] and not generate_opts[:only_if].call(json)

    if generate_opts[:only_on_create]
      # force the value back to the original value from the DB
      json[generate_opts[:property]] = self.send(generate_opts[:property])
    else
      next if generate_opts[:only_if_nil] and not json[generate_opts[:property]].nil?

      # generate a new value
      json[generate_opts[:property]] = generate_opts[:generator].call(json)
      mark_as_system_modified
    end
  end

  super
end