Module: HasHelpers::ActiveRecordUtils

Included in:
ActiveRecord
Defined in:
lib/has_helpers/utils/active_record_utils.rb

Overview

typed: false frozen_string_literal: true

Instance Method Summary collapse

Instance Method Details

#key_with_prefix(key, prefix = "", digit = nil) ⇒ Object



18
19
20
# File 'lib/has_helpers/utils/active_record_utils.rb', line 18

def key_with_prefix(key, prefix = "", digit = nil)
  prefix.present? ? [prefix, digit, key].compact.join("_") : key
end

#metadata_columsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/has_helpers/utils/active_record_utils.rb', line 22

def 
  [
    "created_at",
    "created_by",
    "created_by_id",
    "created_by_username",
    "express_step",
    "id",
    "import_key",
    "last_viewed_at",
    "organization",
    "organization_id",
    "updated_at",
    "updated_by",
    "updated_by_id",
    "updated_by_username"
  ]
end

#remove_prefix(klass, data, prefix = nil) ⇒ Object

TODO: move this to has_data_imports



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/has_helpers/utils/active_record_utils.rb', line 5

def remove_prefix(klass, data, prefix = nil)
  data_without_prefix = {}
  prefix ||= klass.to_s.downcase
  if prefix.present?
    klass.lookup_attributes_precedence.each do |key, column_name|
      value = data[key_with_prefix(key, prefix)] # in case params is a hash and we need to search by prefix/digit -> advisor_id, advisor_import_key, advisor_one_id. advisor_two_id
      value = value.send("normalize_#{key}") if value.respond_to?("normalize_#{key}") # normalizing the value if the method is available and needed.
      data_without_prefix[column_name] = value if value.present?
    end
  end
  data_without_prefix
end