Class: Aker::Mapping

Inherits:
Object
  • Object
show all
Defined in:
app/models/aker/mapping.rb

Overview

To add a new mapping field from Aker:

  1. Add the field name from aker as a value inside the corresponding list for the key with the SS table name

    in MAP_SS_TABLES_WITH_AKER
    
  2. Add the field name from aker as a key linked with a column name for SS in MAP_AKER_WITH_SS_COLUMNS

After this, if we want to update a new property from Aker into SS models we have to add the field name from aker inside the list UPDATABLE_ATTRS_FROM_AKER_INTO_SS.

If we want to update a change in SS into the properties of Aker in the biomaterial service we have to add the field name from aker inside the list UPDATABLE_ATTRS_FROM_SS_INTO_AKER.

SS updates will occur on update() calls Aker updates will happen on job completion, because the job message for the material is generated from the attributes() method of this class.

Direct Known Subclasses

Material

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.


35
36
37
# File 'app/models/aker/mapping.rb', line 35

def config
  @config
end

Instance Method Details

#_each_model_and_setting_attrs_for(attrs) ⇒ Object

Given a hash of attributes to update, it will generate the list of model instances to update and the corresponding specific attributes in each instance


99
100
101
102
103
104
105
# File 'app/models/aker/mapping.rb', line 99

def _each_model_and_setting_attrs_for(attrs)
  table_names_to_update(attrs).each do |table_name|
    model = model_for_table(table_name)
    setting_attrs = mapped_setting_attributes_for_table(table_name, attrs)
    yield model, setting_attrs
  end
end

#attribute_names_for_column(table_name, column_name) ⇒ Object

Given a table+column, it returns the list of Aker attribute names it maps to


88
89
90
# File 'app/models/aker/mapping.rb', line 88

def attribute_names_for_column(table_name, column_name)
  config[:map_ss_columns_with_aker][table_name][column_name]
end

#attributesObject

rubocop:todo Metrics/MethodLength


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/aker/mapping.rb', line 52

def attributes # rubocop:todo Metrics/MethodLength
  {}.tap do |obj|
    config[:updatable_columns_from_ss_into_aker].each do |table_name, column_names|
      model = model_for_table(table_name)
      column_names.each do |column_name|
        attribute_names_for_column(table_name, column_name).each do |attribute_name|
          raise 'Aker clash config problem' if obj[attribute_name]

          obj[attribute_name] = get_value_for(model, column_name)
        end
      end
    end
  end
end

#columns_for_table_from_field(table_name, field_name) ⇒ Object

Given a table name and an attribute name, it returns a list of colunms of the table that corresponds to this attribute

Example:
  well.name         <= supplier_name
  well.supplier_name <= supplier_name

It would return [:name, :supplier_name]


127
128
129
130
131
132
133
# File 'app/models/aker/mapping.rb', line 127

def columns_for_table_from_field(table_name, field_name)
  [].tap do |list|
    config[:map_ss_columns_with_aker][table_name].each do |column_name, field_names|
      list.push(column_name) if field_names.include?(field_name) && list.exclude?(column_name)
    end
  end
end

#configObject


67
68
69
# File 'app/models/aker/mapping.rb', line 67

def config
  self.class.config
end

#get_value_for(model, column_name) ⇒ Object

Gets the value of an attribute name for a model


81
82
83
84
85
# File 'app/models/aker/mapping.rb', line 81

def get_value_for(model, column_name)
  return model.send(column_name) unless model.nil?

  send(column_name)
end

#mapped_setting_attributes_for_table(table_name, attrs) ⇒ Object

Given a table name and a list of attributes, it returns a subset of attributes that will correspond to the update of this table name


137
138
139
140
141
142
143
144
145
# File 'app/models/aker/mapping.rb', line 137

def mapped_setting_attributes_for_table(table_name, attrs)
  {}.tap do |update_obj|
    attrs.each do |k, v|
      next unless config[:updatable_attrs_from_aker_into_ss].include?(k)

      columns_for_table_from_field(table_name, k).each { |column_name| update_obj[column_name] = v }
    end
  end
end

#model_for_table(_table_name) ⇒ Object

Returns the model instance for the table name


108
109
110
# File 'app/models/aker/mapping.rb', line 108

def model_for_table(_table_name)
  raise 'Not implemented'
end

#table_names_for_attr(attr_name) ⇒ Object

Given an attribute name, it returns all the available table names that this attribute can update


113
114
115
116
117
118
119
# File 'app/models/aker/mapping.rb', line 113

def table_names_for_attr(attr_name)
  [].tap do |list|
    config[:map_ss_columns_with_aker].each do |table_name, column_object|
      list.push(table_name) if column_object.values.flatten.include?(attr_name) && list.exclude?(table_name)
    end
  end
end

#table_names_to_update(attrs) ⇒ Object

Given a hash of attributes, it generates a list of table names that will be affected by the update


93
94
95
# File 'app/models/aker/mapping.rb', line 93

def table_names_to_update(attrs)
  attrs.keys.map { |attr_name| table_names_for_attr(attr_name) }.flatten.uniq
end

#update(attrs) ⇒ Object


38
39
40
41
42
43
44
# File 'app/models/aker/mapping.rb', line 38

def update(attrs)
  val = true
  _each_model_and_setting_attrs_for(attrs.symbolize_keys) do |model, setting_attrs|
    val &&= update_model(model, setting_attrs)
  end
  val
end

#update!(attrs) ⇒ Object


46
47
48
49
50
# File 'app/models/aker/mapping.rb', line 46

def update!(attrs)
  raise 'Error while saving attributes' unless update(attrs)

  true
end

#update_model(model, setting_attrs) ⇒ Object

Gets a model instance and a hash of attributes and performs the update of fields on it If no model is provided, it will suppose it is self


73
74
75
76
77
78
# File 'app/models/aker/mapping.rb', line 73

def update_model(model, setting_attrs)
  return model.update(setting_attrs) unless model.nil?

  setting_attrs.each_pair { |k, v| send(:"#{k}=", v) }
  true
end