Module: AbAdmin::Utils
- Included in:
- AbAdmin
- Defined in:
- lib/ab_admin.rb,
lib/ab_admin/utils.rb,
lib/ab_admin/utils/mysql.rb,
lib/ab_admin/utils/logger.rb,
lib/ab_admin/utils/csv_document.rb,
lib/ab_admin/utils/eval_helpers.rb,
lib/ab_admin/utils/xls_document.rb
Defined Under Namespace
Modules: EvalHelpers, Logger, Mysql
Classes: CsvDocument, Default, Sanitizer, XlsDocument
Constant Summary
collapse
- @@display_name_methods_cache =
{}
Instance Method Summary
collapse
Instance Method Details
#bm(message = 'Benchmarking', options = {}) ⇒ Object
14
15
16
17
18
19
|
# File 'lib/ab_admin/utils.rb', line 14
def bm(message = 'Benchmarking', options = {})
result = nil
ms = Benchmark.ms { result = yield }
(options[:logger] || Rails.logger).info '%s (%.3fms)' % [message, ms]
result
end
|
#display_name(resource) ⇒ Object
113
114
115
116
|
# File 'lib/ab_admin/utils.rb', line 113
def display_name(resource)
return unless resource
resource.send(display_name_method_for(resource)).to_s.no_html
end
|
#display_name_method_for(resource) ⇒ Object
109
110
111
|
# File 'lib/ab_admin/utils.rb', line 109
def display_name_method_for(resource)
@@display_name_methods_cache[resource.class.name] ||= AbAdmin.display_name_methods.find { |method| resource.respond_to? method }
end
|
#friendly_token(n = 10) ⇒ Object
144
145
146
|
# File 'lib/ab_admin/utils.rb', line 144
def friendly_token(n=10)
SecureRandom.base64(n * 2).tr('+/=', 'xyz').first(n)
end
|
#full_url(path) ⇒ Object
69
70
71
72
73
|
# File 'lib/ab_admin/utils.rb', line 69
def full_url(path)
return path if path =~ %r{^(http|//)}
host = AbAdmin.base_url || Rails.application.config.action_mailer.default_url_options[:host] || 'www.example.com'
"http://#{host}#{path}"
end
|
#js_date_data ⇒ Object
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/ab_admin/utils.rb', line 75
def js_date_data
{
formats: I18n.t('date.formats'),
day_names: I18n.t('date.day_names'),
abbr_day_names: I18n.t('date.common_abbr_day_names'),
month_names: I18n.t('date.common_month_names'),
standalone_month_names: I18n.t('date.standalone_month_names'),
abbr_month_names: I18n.t('date.abbr_month_names')
}
end
|
#l_path(locale = nil) ⇒ Object
Also known as:
locale_path
47
48
49
50
|
# File 'lib/ab_admin/utils.rb', line 47
def l_path(locale=nil)
locale ||= I18n.locale
locale == I18n.default_locale ? '' : "/#{locale}"
end
|
#load_files!(base_path = 'lib/utils') ⇒ Object
8
9
10
11
12
|
# File 'lib/ab_admin/utils.rb', line 8
def load_files!(base_path = 'lib/utils')
Dir[Rails.root.join("#{base_path}/**/*.rb")].each do |path|
require_dependency path
end
end
|
#normalize_html(raw_html, options = {}, &block) ⇒ Object
html like: ‘<!– html comment –><script>script content</script><div>div content</div><p>p content</p>’ normalized to: “<p>div content</p><p>p content</p>”
60
61
62
63
|
# File 'lib/ab_admin/utils.rb', line 60
def normalize_html(raw_html, options = {}, &block)
@@sanitizer ||= Sanitizer.new(options)
@@sanitizer.normalize_html(raw_html, options[:sanitize] || {}, &block)
end
|
#pretty(raw_data) ⇒ Object
21
22
23
24
25
26
27
28
29
|
# File 'lib/ab_admin/utils.rb', line 21
def pretty(raw_data)
data = case raw_data
when Hash
raw_data
when String
MultiJson.decode(raw_data)
end
JSON.pretty_generate data
end
|
#pretty_data(object) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/ab_admin/utils.rb', line 123
def pretty_data(object)
case object
when String, Integer, BigDecimal, Float
object
when TrueClass
'+'
when FalseClass
'-'
when Date, DateTime, Time, ActiveSupport::TimeWithZone
I18n.l(object, format: :long)
when NilClass
''
else
AbAdmin.safe_display_name(object) || object
end
end
|
54
55
56
|
# File 'lib/ab_admin/utils.rb', line 54
def (raw_html)
Rack::Utils.escape_html(raw_html.no_html.squish)
end
|
#safe_display_name(resource) ⇒ Object
118
119
120
121
|
# File 'lib/ab_admin/utils.rb', line 118
def safe_display_name(resource)
return unless display_name_method_for(resource)
display_name(resource)
end
|
#test_env? ⇒ Boolean
140
141
142
|
# File 'lib/ab_admin/utils.rb', line 140
def test_env?
Rails.env.test? || Rails.env.cucumber?
end
|
#truncate_text(raw_text, size = 200) ⇒ Object
42
43
44
45
|
# File 'lib/ab_admin/utils.rb', line 42
def truncate_text(raw_text, size=200)
text = raw_text.to_s.gsub(/"|«|»|'/, '\'').gsub(/ /, ' ').gsub(/—/, '-').no_html.squish
text.truncate(size, separator: ' ')
end
|
#url_helpers ⇒ Object
65
66
67
|
# File 'lib/ab_admin/utils.rb', line 65
def url_helpers
Rails.application.routes.url_helpers
end
|
#val_to_array(val, zero = false) ⇒ Object
31
32
33
34
35
|
# File 'lib/ab_admin/utils.rb', line 31
def val_to_array(val, zero=false)
return [] unless val
a = val.is_a?(Array) ? val : val.to_s.split(',').map(&:to_i)
zero ? a : a.without(0)
end
|
#val_to_array_s(val) ⇒ Object
37
38
39
40
|
# File 'lib/ab_admin/utils.rb', line 37
def val_to_array_s(val)
return [] unless val
val.is_a?(Array) ? val : val.split(',').map(&:to_s).without(0)
end
|