forked from activeadmin/activeadmin
/
naming.rb
72 lines (59 loc) · 2.18 KB
/
naming.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module ActiveAdmin
class Resource
module Naming
# Returns the user facing name. Example: "Bank Account"
def resource_name
@resource_name ||= @options[:as]
@resource_name ||= singular_human_name
@resource_name ||= safe_resource_name
end
# Returns the plural version of the user facing name. Example: "Bank Accounts"
def plural_resource_name
@plural_resource_name ||= @options[:as].pluralize if @options[:as]
@plural_resource_name ||= plural_human_name
@plural_resource_name ||= resource_name.pluralize
end
# A name used internally to uniquely identify this resource
def resource_key
camelized_resource_name
end
def safe_resource_name
@safe_resource_name ||= @options[:as]
@safe_resource_name ||= resource_class.name.gsub('::',' ')
end
def plural_safe_resource_name
safe_resource_name.pluralize
end
# A camelized safe representation for this resource
def camelized_resource_name
safe_resource_name.titleize.gsub(' ', '')
end
def plural_camelized_resource_name
plural_safe_resource_name.titleize.gsub(' ', '')
end
# An underscored safe representation internally for this resource
def underscored_resource_name
camelized_resource_name.underscore
end
# Returns the plural and underscored version of this resource. Useful for element id's.
def plural_underscored_resource_name
plural_camelized_resource_name.underscore
end
private
# @return [String] Titleized human name via ActiveRecord I18n or nil
def singular_human_name
return nil unless resource_class.respond_to?(:model_name)
resource_class.model_name.human.titleize
end
# @return [String] Titleized plural human name via ActiveRecord I18n or nil
def plural_human_name
return nil unless resource_class.respond_to?(:model_name)
begin
I18n.translate!("activerecord.models.#{resource_class.model_name.underscore}.other").titleize
rescue I18n::MissingTranslationData
nil
end
end
end
end
end