Class: ImpExp::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
app/services/imp_exp/models/base.rb

Instance Method Summary collapse

Instance Method Details

#active_record_classObject



6
7
8
# File 'app/services/imp_exp/models/base.rb', line 6

def active_record_class
  "::#{self.class.name.demodulize}".constantize
end

#attribute_namesObject



22
23
24
# File 'app/services/imp_exp/models/base.rb', line 22

def attribute_names
  attributes.map(&:first)
end

#attribute_options(attribute_name) ⇒ Object



83
84
85
# File 'app/services/imp_exp/models/base.rb', line 83

def attribute_options(attribute_name)
  attributes.find { |attribute| attribute[0] == attribute_name.to_sym }&.slice(1) || {}
end

#attribute_to_import_namesObject



30
31
32
# File 'app/services/imp_exp/models/base.rb', line 30

def attribute_to_import_names
  attribute_names.reject { |attribute_name| attribute_options(attribute_name)[:imported] == false }
end

#attribute_typesObject



26
27
28
# File 'app/services/imp_exp/models/base.rb', line 26

def attribute_types
  attribute_names.map { active_record_class.column_for_attribute(_1)&.type }
end

#attributesObject

Raises:

  • (NotImplementedError)


18
19
20
# File 'app/services/imp_exp/models/base.rb', line 18

def attributes
  raise NotImplementedError
end

#extra_attributes_imported_at_runtime(scoping_parent, _record, _row) ⇒ Object



34
35
36
37
38
39
40
41
# File 'app/services/imp_exp/models/base.rb', line 34

def extra_attributes_imported_at_runtime(scoping_parent, _record, _row)
  scoping_parent_fk = scoping_parent_foreign_key_column_name(scoping_parent)
  if scoping_parent && active_record_class.attribute_names.include?(scoping_parent_fk)
    return { scoping_parent_fk.to_sym => scoping_parent.id }
  end

  {}
end

#file_attribute_namesObject



47
48
49
# File 'app/services/imp_exp/models/base.rb', line 47

def file_attribute_names
  file_attributes.map(&:first)
end

#file_attributesObject



43
44
45
# File 'app/services/imp_exp/models/base.rb', line 43

def file_attributes
  []
end

#filename(active_storage_file) ⇒ Object



135
136
137
# File 'app/services/imp_exp/models/base.rb', line 135

def filename(active_storage_file)
  "#{active_storage_file.name}-#{active_storage_file.record.code}#{file_extension(active_storage_file)}"
end

#files(scoping_parent) ⇒ Object



125
126
127
128
129
# File 'app/services/imp_exp/models/base.rb', line 125

def files(scoping_parent)
  scoped(scoping_parent).flat_map do |record|
    record.slice(file_attribute_names).values.select(&:attached?)
  end
end

#files_directory_pathObject



131
132
133
# File 'app/services/imp_exp/models/base.rb', line 131

def files_directory_path
  active_record_class.model_name.element
end

#find_record(attrs, scoping_parent) ⇒ Object



10
11
12
# File 'app/services/imp_exp/models/base.rb', line 10

def find_record(attrs, scoping_parent)
  scoped(scoping_parent).find_by(code: attrs[:code])
end

#headersObject



95
96
97
# File 'app/services/imp_exp/models/base.rb', line 95

def headers
  attribute_names + relation_names + file_attribute_names
end

#headers_translatedObject



87
88
89
90
91
92
93
# File 'app/services/imp_exp/models/base.rb', line 87

def headers_translated
  (attributes + relations).map do |name, opts = {}|
    i18n_path = opts[:i18n_path].presence || "#{active_record_class.model_name.i18n_key}.#{name}"
    key = "activerecord.attributes.#{i18n_path}"
    I18n.t(key, raise: true)
  end
end

#model_nameObject



75
76
77
# File 'app/services/imp_exp/models/base.rb', line 75

def model_name
  self.class.name.demodulize
end

#order(collection) ⇒ Object



71
72
73
# File 'app/services/imp_exp/models/base.rb', line 71

def order(collection)
  collection.order(:created_at)
end

#preload_associations(collection) ⇒ Object



67
68
69
# File 'app/services/imp_exp/models/base.rb', line 67

def preload_associations(collection)
  collection
end

#relation_namesObject



55
56
57
# File 'app/services/imp_exp/models/base.rb', line 55

def relation_names
  relations.map(&:first)
end

#relation_options(relation_name) ⇒ Object



79
80
81
# File 'app/services/imp_exp/models/base.rb', line 79

def relation_options(relation_name)
  relations.find { |relation| relation[0] == relation_name.to_sym }&.slice(1) || {}
end

#relationsObject



51
52
53
# File 'app/services/imp_exp/models/base.rb', line 51

def relations
  []
end

#row(record) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/services/imp_exp/models/base.rb', line 107

def row(record)
  data = record.slice(attribute_names)
  attribute_names.each do |attribute_name|
    serializer = attribute_options(attribute_name)[:serializer]
    data[attribute_name] = serializer.dump(record, attribute_name) if serializer
  end
  data = data.values
  relation_names.each do |relation_name|
    serializer = relation_options(relation_name)[:serializer]
    data << serializer.dump(record, relation_name)
  end
  data.concat(file_attribute_names.map do |file_attribute_name|
    file = record.send(file_attribute_name)
    "#{files_directory_path}/#{filename(file)}" if file.attached?
  end)
  data
end

#rows(scoping_parent) ⇒ Object



99
100
101
102
103
104
105
# File 'app/services/imp_exp/models/base.rb', line 99

def rows(scoping_parent)
  rows = []
  preload_associations(order(scoped(scoping_parent))).each do |record|
    rows << row(record)
  end
  rows
end

#scoped(scoping_parent) ⇒ Object



63
64
65
# File 'app/services/imp_exp/models/base.rb', line 63

def scoped(scoping_parent)
  active_record_class.rewhere({ scoping_parent_foreign_key_column_name(scoping_parent) => scoping_parent.id })
end

#scoping_parent_foreign_key_column_name(scoping_parent) ⇒ Object



59
60
61
# File 'app/services/imp_exp/models/base.rb', line 59

def scoping_parent_foreign_key_column_name(scoping_parent)
  "#{scoping_parent.model_name.singular}_id"
end

#strict_find_by_attributeObject



14
15
16
# File 'app/services/imp_exp/models/base.rb', line 14

def strict_find_by_attribute
  nil
end