Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/active_record.rb

Overview

Add Edgarj specific methods.

Constant Summary collapse

@@_edgarj_cache =

cache several kind of information for each (class x kind) during runtime, for example, result of edgarj_address? method.

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._edgarj_cacheObject

just for dump purpose



98
99
100
# File 'lib/core_ext/active_record.rb', line 98

def self._edgarj_cache
  @@_edgarj_cache
end

.belongs_to_AR(column) ⇒ Object

return AR class which the column belongs to. nil if not exist



38
39
40
41
42
43
44
# File 'lib/core_ext/active_record.rb', line 38

def self.belongs_to_AR(column)
  if (parent_assoc = belongs_to_AR_assoc(column))
    parent_assoc.klass
  else
    nil
  end
end

.belongs_to_AR_assoc(column) ⇒ Object

return AR assoc which the column belongs to. nil if not exist



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/core_ext/active_record.rb', line 21

def self.belongs_to_AR_assoc(column)
  parent_assoc_cache = edgarj_cache(:parent_assoc)
  if (parent_assoc = parent_assoc_cache[column])
    return parent_assoc
  end

  for parent_assoc in reflect_on_all_associations(:belongs_to) do
    if !parent_assoc.options[:polymorphic] &&
        parent_assoc.foreign_key.to_sym == column.name.to_sym
      parent_assoc_cache[column] = parent_assoc
      return parent_assoc
    end
  end
  nil
end

.edgarj_cache(kind) ⇒ Object

initialize cache per (class x kind) on the fly



91
92
93
94
95
# File 'lib/core_ext/active_record.rb', line 91

def self.edgarj_cache(kind)
  @@_edgarj_cache[self] = {}       if !@@_edgarj_cache[self]
  @@_edgarj_cache[self][kind] = {} if !@@_edgarj_cache[self][kind]
  @@_edgarj_cache[self][kind]
end

.get_belongs_to_name(col_or_sym) ⇒ Object

get belongs_to association name from column.

Example: get_assoc_name(:adrs_id) -> :adrs



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/core_ext/active_record.rb', line 74

def self.get_belongs_to_name(col_or_sym)
  cache     = edgarj_cache(:belongs_to_name)
  col_name  = get_column_name(col_or_sym).to_s
  if !cache[col_name].nil?
    return cache[col_name]
  end

  for reflection in reflect_on_all_associations(:belongs_to) do
    if reflection.foreign_key.to_sym == col_name.to_sym
      cache[col_name] = reflection.name
      return reflection.name
    end
  end
  nil
end

.get_column_name(col_or_sym) ⇒ Object

get column name from column object or symbol



103
104
105
106
107
108
109
110
# File 'lib/core_ext/active_record.rb', line 103

def self.get_column_name(col_or_sym)
  case col_or_sym
  when String, Symbol
    col_or_sym
  else
    col_or_sym.name
  end
end

.human_const_name(_module, const) ⇒ Object

Human name for ‘const’ in namespace of the ‘_module’.

I18n fallbacks is as follows (Question::Priority::HIGH as example):

  1. t(‘activerecord.enums.question/priority.HIGH’)

  2. t(‘high’)

  3. ‘High’



54
55
56
57
58
59
60
61
# File 'lib/core_ext/active_record.rb', line 54

def self.human_const_name(_module, const)
  lower = const.downcase
  I18n.t(const,
      scope:    "activerecord.enums.#{_module.name.underscore}",
      default:  I18n.t(lower,
          scope:    'edgarj.default',
          default:  lower.to_s.humanize))
end

.human_nameObject

Fallback is as follows (Author model is an example):

  1. t(‘activerecord.author’)

  2. t(‘edgarj.default.author’)

  3. human()



11
12
13
14
15
16
17
18
# File 'lib/core_ext/active_record.rb', line 11

def self.human_name
  name = self.model_name
  I18n.t(name.i18n_key,
      scope:    :activerecord,
      default:  I18n.t(name.i18n_key,
          scope:   'edgarj.default',
          default: name.human))
end

Instance Method Details

#belongs_to_AR(column) ⇒ Object

return AR object which the column belongs to. return nil if the column is not ‘belongs_to’

See Also:

  • ActiveRecord::Base.selfself.belongs_to_AR


116
117
118
119
120
121
122
# File 'lib/core_ext/active_record.rb', line 116

def belongs_to_AR(column)
  if (parent_assoc = self.class.belongs_to_AR_assoc(column))
    send(parent_assoc.name)
  else
    nil
  end
end

#err_on(attr, message_key) ⇒ Object

short-cut of:

errors.add(attr, I18n.t(message_key, :scope=>'activerecord.errors.messages')


65
66
67
68
69
# File 'lib/core_ext/active_record.rb', line 65

def err_on(attr, message_key)
  errors.add(
      attr,
      I18n.t(message_key, :scope=>'activerecord.errors.messages'))
end