Module: ActiverecordDataImporter::ClassMethods

Defined in:
lib/activerecord_data_importer.rb

Instance Method Summary collapse

Instance Method Details

#divide_attributes(row_data) ⇒ Object



19
20
21
# File 'lib/activerecord_data_importer.rb', line 19

def divide_attributes(row_data)
  row_data.partition { |k, _v| self.attribute_names.include? k }.map(&:to_h)
end

#divide_relation_key(another_attributes) ⇒ Object



23
24
25
26
# File 'lib/activerecord_data_importer.rb', line 23

def divide_relation_key(another_attributes)
  relation_keys = self.reflect_on_all_associations.map(&:name).map(&:to_s)
  another_attributes.select { |key, value| relation_keys.include? key }
end

#get_latest_versionObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/activerecord_data_importer.rb', line 28

def get_latest_version
  # note
  # This method is published full scan query.
  # If you think you undesirable this, this method should be overridden to get the latest version in a different way.
  # ex)
  # * using cache store latest version.
  # * using model for version information.
  if self.attribute_names.include? "version"
    self.maximum("version").to_i
  else
    0
  end
end

#import_from_csv(filename) ⇒ Object



9
10
11
12
# File 'lib/activerecord_data_importer.rb', line 9

def import_from_csv(filename)
  csv_data = ActiverecordDataImporter::CsvConverter.convert_csv_to_hash(filename)
  self.import_data csv_data
end

#import_from_json(filename) ⇒ Object



14
15
16
17
# File 'lib/activerecord_data_importer.rb', line 14

def import_from_json(filename)
  json_data = self.load_json filename
  self.import_data json_data
end