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


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

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


90
91
92
# File 'app/models/aker/mapping.rb', line 90

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

#attributesObject


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

def attributes
  {}.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 colums of the table that corresponds to this attribute

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

It would return [:name, :supplier_name]


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

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.include?(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


83
84
85
86
87
# File 'app/models/aker/mapping.rb', line 83

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


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

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 do |column_name|
        update_obj[column_name] = v
      end
    end
  end
end

#model_for_table(_table_name) ⇒ Object

Returns the model instance for the table name


110
111
112
# File 'app/models/aker/mapping.rb', line 110

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


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

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.include?(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


95
96
97
# File 'app/models/aker/mapping.rb', line 95

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
79
80
# 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 do |k, v|
    send(:"#{k}=", v)
  end
  true
end