Class: ImpExp::Models::Base
- Inherits:
-
Object
- Object
- ImpExp::Models::Base
- Defined in:
- app/services/imp_exp/models/base.rb
Instance Method Summary collapse
- #active_record_class ⇒ Object
- #after_save(_record) ⇒ Object
- #attribute_names ⇒ Object
- #attribute_options(attribute_name) ⇒ Object
- #attribute_to_import_names ⇒ Object
- #attribute_types ⇒ Object
- #attributes ⇒ Object
- #extra_attributes_imported_at_runtime(scoping_parent, _record, _row) ⇒ Object
- #file_attribute_names ⇒ Object
- #file_attributes ⇒ Object
- #filename(active_storage_file) ⇒ Object
- #files(scoping_parent) ⇒ Object
- #files_directory_path ⇒ Object
- #find_record(attrs, scoping_parent) ⇒ Object
- #headers ⇒ Object
- #headers_translated ⇒ Object
- #model_name ⇒ Object
- #order(collection) ⇒ Object
- #preload_associations(collection) ⇒ Object
- #relation_names ⇒ Object
- #relation_options(relation_name) ⇒ Object
- #relations ⇒ Object
- #row(record) ⇒ Object
- #rows(scoping_parent) ⇒ Object
- #scoped(scoping_parent) ⇒ Object
- #scoping_parent_foreign_key_column_name(scoping_parent) ⇒ Object
- #skip_record?(_record) ⇒ Boolean
- #strict_find_by_attribute ⇒ Object
Instance Method Details
#active_record_class ⇒ Object
6 7 8 |
# File 'app/services/imp_exp/models/base.rb', line 6 def active_record_class "::#{self.class.name.demodulize}".constantize end |
#after_save(_record) ⇒ Object
146 |
# File 'app/services/imp_exp/models/base.rb', line 146 def after_save(_record); end |
#attribute_names ⇒ Object
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_name) attributes.find { |attribute| attribute[0] == attribute_name.to_sym }&.slice(1) || {} end |
#attribute_to_import_names ⇒ Object
30 31 32 |
# File 'app/services/imp_exp/models/base.rb', line 30 def attribute_to_import_names attribute_names.reject { |attribute_name| (attribute_name)[:imported] == false } end |
#attribute_types ⇒ Object
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 |
#attributes ⇒ Object
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_names ⇒ Object
47 48 49 |
# File 'app/services/imp_exp/models/base.rb', line 47 def file_attribute_names file_attributes.map(&:first) end |
#file_attributes ⇒ Object
43 44 45 |
# File 'app/services/imp_exp/models/base.rb', line 43 def file_attributes [] end |
#filename(active_storage_file) ⇒ Object
142 143 144 |
# File 'app/services/imp_exp/models/base.rb', line 142 def filename(active_storage_file) "#{active_storage_file.name}-#{active_storage_file.record.code}#{file_extension(active_storage_file)}" end |
#files(scoping_parent) ⇒ Object
132 133 134 135 136 |
# File 'app/services/imp_exp/models/base.rb', line 132 def files(scoping_parent) scoped(scoping_parent).flat_map do |record| record.slice(file_attribute_names).values.select(&:attached?) end end |
#files_directory_path ⇒ Object
138 139 140 |
# File 'app/services/imp_exp/models/base.rb', line 138 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 |
#headers ⇒ Object
100 101 102 |
# File 'app/services/imp_exp/models/base.rb', line 100 def headers attribute_names + relation_names + file_attribute_names end |
#headers_translated ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# 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}" begin I18n.t("activerecord.attributes.#{i18n_path}", raise: true) rescue I18n::MissingTranslationData raise if opts[:i18n_path].present? I18n.t("attributes.#{name}", raise: true) end end end |
#model_name ⇒ Object
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_names ⇒ Object
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_name) relations.find { |relation| relation[0] == relation_name.to_sym }&.slice(1) || {} end |
#relations ⇒ Object
51 52 53 |
# File 'app/services/imp_exp/models/base.rb', line 51 def relations [] end |
#row(record) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/services/imp_exp/models/base.rb', line 110 def row(record) data = record.slice(attribute_names) attribute_names.each do |attribute_name| serializer = (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_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
104 105 106 107 108 |
# File 'app/services/imp_exp/models/base.rb', line 104 def rows(scoping_parent) preload_associations(order(scoped(scoping_parent))).map do |record| row(record) end 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 |
#skip_record?(_record) ⇒ Boolean
128 129 130 |
# File 'app/services/imp_exp/models/base.rb', line 128 def skip_record?(_record) false end |
#strict_find_by_attribute ⇒ Object
14 15 16 |
# File 'app/services/imp_exp/models/base.rb', line 14 def strict_find_by_attribute nil end |