Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/core_ext/active_record.rb
Overview
Add Edgarj specific methods.
Direct Known Subclasses
Edgarj::ModelPermission, Edgarj::PageInfo, Edgarj::Sssn, Edgarj::UserGroup, Edgarj::UserGroupUser
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
-
._edgarj_cache ⇒ Object
just for dump purpose.
-
.belongs_to_AR(column) ⇒ Object
return AR class which the column belongs to.
-
.belongs_to_AR_assoc(column) ⇒ Object
return AR assoc which the column belongs to.
-
.edgarj_cache(kind) ⇒ Object
initialize cache per (class x kind) on the fly.
-
.get_belongs_to_name(col_or_sym) ⇒ Object
get belongs_to association name from column.
-
.get_column_name(col_or_sym) ⇒ Object
get column name from column object or symbol.
-
.human_const_name(_module, const) ⇒ Object
Human name for ‘const’ in namespace of the ‘_module’.
-
.human_name ⇒ Object
Fallback is as follows (Author model is an example): 1.
Instance Method Summary collapse
-
#belongs_to_AR(column) ⇒ Object
return AR object which the column belongs to.
-
#err_on(attr, message_key) ⇒ Object
short-cut of: errors.add(attr, I18n.t(message_key, :scope=>‘activerecord.errors.messages’).
Class Method Details
._edgarj_cache ⇒ Object
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.[: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):
-
t(‘activerecord.enums.question/priority.HIGH’)
-
t(‘high’)
-
‘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_name ⇒ Object
Fallback is as follows (Author model is an example):
-
t(‘activerecord.author’)
-
t(‘edgarj.default.author’)
-
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’
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, ) errors.add( attr, I18n.t(, :scope=>'activerecord.errors.messages')) end |