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

#all_modelsObject



8
9
10
11
12
13
# File 'lib/ab_admin/utils.rb', line 8

def all_models
  Kernel.suppress_warnings do
    Dir.glob(Rails.root.to_s + '/app/models/**/*.rb').reject { |path| path =~ /concerns|shared/ }.each { |file| require file }
  end
  ActiveRecord::Base.direct_descendants
end

#bm(message = 'Benchmarking', options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/ab_admin/utils.rb', line 21

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



121
122
123
124
# File 'lib/ab_admin/utils.rb', line 121

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



117
118
119
# File 'lib/ab_admin/utils.rb', line 117

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



152
153
154
# File 'lib/ab_admin/utils.rb', line 152

def friendly_token(n=10)
  SecureRandom.base64(n * 2).tr('+/=', 'xyz').first(n).downcase
end

#full_url(path, protocol = 'http://') ⇒ Object



76
77
78
79
80
81
# File 'lib/ab_admin/utils.rb', line 76

def full_url(path, protocol='http://')
  return path if path =~ %r{^(http|//)}
  duo = Rails.application.config.action_mailer.default_url_options
  host = AbAdmin.base_url || [duo[:host], duo[:port]].compact.join(':') || 'www.example.com'
  "#{protocol}#{host}#{path}"
end

#js_date_dataObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/ab_admin/utils.rb', line 83

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



54
55
56
57
# File 'lib/ab_admin/utils.rb', line 54

def l_path(locale=nil)
  locale ||= I18n.locale
  locale == I18n.default_locale ? '' : "/#{locale}"
end

#load_files!(base_path = 'lib/utils') ⇒ Object



15
16
17
18
19
# File 'lib/ab_admin/utils.rb', line 15

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>”



67
68
69
70
# File 'lib/ab_admin/utils.rb', line 67

def normalize_html(raw_html, options = {}, &block)
  @@sanitizer ||= Sanitizer.new(options)
  @@sanitizer.normalize_html(raw_html, options[:sanitize] || {}, &block)
end

#pretty(raw_data) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/ab_admin/utils.rb', line 28

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ab_admin/utils.rb', line 131

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

#rss_text(raw_html) ⇒ Object



61
62
63
# File 'lib/ab_admin/utils.rb', line 61

def rss_text(raw_html)
  Rack::Utils.escape_html(raw_html.no_html.squish)
end

#safe_display_name(resource) ⇒ Object



126
127
128
129
# File 'lib/ab_admin/utils.rb', line 126

def safe_display_name(resource)
  return unless display_name_method_for(resource)
  display_name(resource)
end

#test_env?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/ab_admin/utils.rb', line 148

def test_env?
  Rails.env.test? || Rails.env.cucumber?
end

#truncate_text(raw_text, size = 200) ⇒ Object



49
50
51
52
# File 'lib/ab_admin/utils.rb', line 49

def truncate_text(raw_text, size=200)
  text = raw_text.to_s.gsub(/&quot;|&laquo;|&raquo;|&#x27;/, '\'').gsub(/&nbsp;/, ' ').gsub(/&mdash;/, '-').no_html.squish
  text.truncate(size, separator: ' ')
end

#url_helpersObject



72
73
74
# File 'lib/ab_admin/utils.rb', line 72

def url_helpers
  Rails.application.routes.url_helpers
end

#val_to_array(val, zero = false) ⇒ Object



38
39
40
41
42
# File 'lib/ab_admin/utils.rb', line 38

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



44
45
46
47
# File 'lib/ab_admin/utils.rb', line 44

def val_to_array_s(val)
  return [] unless val
  val.is_a?(Array) ? val : val.split(',').map(&:to_s).without(0)
end