Module: Datagrid::Utils

Defined in:
lib/datagrid/utils.rb

Overview

:nodoc:

Constant Summary collapse

TRUTH =
[true, 1, "1", "true", "yes", "on"]

Class Method Summary collapse

Class Method Details

.add_html_classes(options, *classes) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/datagrid/utils.rb', line 39

def add_html_classes(options, *classes)
  options = options.clone
  options[:class] ||= ""
  if options[:class].is_a?(Array)
    options[:class] += classes
  else
    # suppose that it is a String
    options[:class] += " " unless options[:class].blank?
    options[:class] += classes.join(" ")
  end
  options
end

.apply_args(*args, &block) ⇒ Object



74
75
76
77
# File 'lib/datagrid/utils.rb', line 74

def apply_args(*args, &block)
  size = block.arity < 0 ? args.size : block.arity
  block.call(*args.slice(0, size))
end

.booleanize(value) ⇒ Object



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

def booleanize(value)
  if value.respond_to?(:downcase)
    value = value.downcase
  end
  TRUTH.include?(value)
end

.callable(value) ⇒ Object



132
133
134
# File 'lib/datagrid/utils.rb', line 132

def callable(value)
  value.respond_to?(:call) ? value.call : value
end

.extract_position_from_options(array, options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/datagrid/utils.rb', line 56

def extract_position_from_options(array, options)
  before, after = options[:before], options[:after]
  if before && after
    raise Datagrid::ConfigurationError, "Options :before and :after can not be used together"
  end
  # Consider as before all
  return 0 if before == true
  if before
    before = before.to_sym
    array.index {|c| c.name.to_sym == before }
  elsif after
    after = after.to_sym
    array.index {|c| c.name.to_sym == after } + 1
  else
    -1
  end
end

.format_date_as_timestamp(value) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/datagrid/utils.rb', line 115

def format_date_as_timestamp(value)
  if !value
    value
  elsif value.is_a?(Array)
    [value.first.try(:beginning_of_day), value.last.try(:end_of_day)]
  elsif value.is_a?(Range)
    (value.first.beginning_of_day..value.last.end_of_day)
  else
    value.beginning_of_day..value.end_of_day
  end
end

.parse_date(value) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/datagrid/utils.rb', line 79

def parse_date(value)
  return nil if value.blank?
  return value if value.is_a?(Range)
  if value.is_a?(String)
    Array(Datagrid.configuration.date_formats).each do |format|
      begin
        return Date.strptime(value, format)
      rescue ::ArgumentError
      end
    end
  end
  return Date.parse(value) if value.is_a?(String)
  return value.to_date if value.respond_to?(:to_date)
  value
rescue ::ArgumentError
  nil
end

.parse_datetime(value) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/datagrid/utils.rb', line 97

def parse_datetime(value)
  return nil if value.blank?
  return value if value.is_a?(Range)
  if value.is_a?(String)
    Array(Datagrid.configuration.datetime_formats).each do |format|
      begin
        return Time.strptime(value, format)
      rescue ::ArgumentError
      end
    end
  end
  return Time.parse(value) if value.is_a?(String)
  return value.to_time if value.respond_to?(:to_time)
  value
rescue ::ArgumentError
  nil
end

.process_availability(grid, if_option, unless_option) ⇒ Object



127
128
129
130
# File 'lib/datagrid/utils.rb', line 127

def process_availability(grid, if_option, unless_option)
  property_availability(grid, if_option, true) &&
    !property_availability(grid, unless_option, false)
end

.string_like?(value) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/datagrid/utils.rb', line 52

def string_like?(value)
  value.is_a?(Symbol) || value.is_a?(String)
end

.translate_from_namespace(namespace, grid_class, key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/datagrid/utils.rb', line 15

def translate_from_namespace(namespace, grid_class, key)

  lookups = []
  namespaced_key = "#{namespace}.#{key}"

  grid_class.ancestors.each do |ancestor|
    if ancestor.respond_to?(:model_name)
      lookups << :"datagrid.#{ancestor.model_name.i18n_key}.#{namespaced_key}"
    end
  end
  lookups << :"datagrid.defaults.#{namespaced_key}"
  lookups << key.to_s.humanize
  I18n.t(lookups.shift, default: lookups).presence
end

.warn_once(message, delay = 5) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/datagrid/utils.rb', line 30

def warn_once(message, delay = 5)
  @warnings ||= {}
  timestamp = @warnings[message]
  return false if timestamp && timestamp >= Time.now - delay
  warn message
  @warnings[message] = Time.now
  true
end