Module: Redmineup::Liquid::Filters::Base

Includes:
MoneyHelper
Defined in:
lib/redmineup/liquid/filters/base.rb

Instance Method Summary collapse

Methods included from MoneyHelper

#all_currencies, #collection_for_currencies_select, #deal_currency_icon, #object_price, #price_to_currency, #prices_collection_by_currency

Instance Method Details

#attachment(input, file_name) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/redmineup/liquid/filters/base.rb', line 128

def attachment(input, file_name)
  if input.respond_to?(:attachments)
    if input.attachments.is_a?(Hash)
      attachment = input.attachments[file_name]
    else
      attachment = input.attachments.detect{|a| a.file_name == file_name}
    end
    AttachmentDrop.new attachment if attachment
  end
end

#call_method(input, method_name) ⇒ Object



104
105
106
107
108
# File 'lib/redmineup/liquid/filters/base.rb', line 104

def call_method(input, method_name)
  if input.respond_to?(method_name)
    input.method(method_name).call
  end
end

#ceil(input) ⇒ Object



92
93
94
# File 'lib/redmineup/liquid/filters/base.rb', line 92

def ceil(input)
  to_number(input).ceil.to_i
end

#concat(input, *args) ⇒ Object



143
144
145
146
147
# File 'lib/redmineup/liquid/filters/base.rb', line 143

def concat(input, *args)
  result = input.to_s
  args.flatten.each { |a| result << a.to_s }
  result
end

#currency(input, currency_code = nil) ⇒ Object



100
101
102
# File 'lib/redmineup/liquid/filters/base.rb', line 100

def currency(input, currency_code = nil)
  price_to_currency(input, currency_code || container_currency, :converted => false)
end

#custom_field(input, field_name) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/redmineup/liquid/filters/base.rb', line 110

def custom_field(input, field_name)
  if input.respond_to?(:custom_field_values)
    custom_value = input.custom_field_values.detect { |cfv| cfv.custom_field.name == field_name }
    if custom_value
      result = custom_value.custom_field.format.formatted_custom_value(nil, custom_value)
      return result if result.respond_to?(:to_liquid)
      drop_class = "Redmineup::Liquid::#{result.class.name}Drop"
      Object.const_defined?(drop_class) ? drop_class.constantize.new(result) : result
    end
  end
end

#custom_fields(input) ⇒ Object



122
123
124
125
126
# File 'lib/redmineup/liquid/filters/base.rb', line 122

def custom_fields(input)
  if input.respond_to?(:custom_field_values)
    input.custom_field_values.map { |cfv| cfv.custom_field.name }
  end
end

#dasherize(input) ⇒ Object



25
26
27
# File 'lib/redmineup/liquid/filters/base.rb', line 25

def dasherize(input)
  input.to_s.gsub(' ', '-').gsub('/', '-').dasherize
end

#date_range(input, distanse) ⇒ Object

example:

{{ today | date_range: '2015-12-12' }}


69
70
71
72
# File 'lib/redmineup/liquid/filters/base.rb', line 69

def date_range(input, distanse)
  return '' if input.nil?
  (input.to_date - distanse.to_date).to_i rescue 'Invalid date'
end

#default(input, value) ⇒ Object



17
18
19
# File 'lib/redmineup/liquid/filters/base.rb', line 17

def default(input, value)
  input.blank? ? value : input
end

#encode(input) ⇒ Object

example:

{{ "http:://www.example.com?key=hello world" | encode }}

=> http%3A%3A%2F%2Fwww.example.com%3Fkey%3Dhello+world


55
56
57
# File 'lib/redmineup/liquid/filters/base.rb', line 55

def encode(input)
  ::Rack::Utils.escape(input)
end

#floor(input) ⇒ Object



96
97
98
# File 'lib/redmineup/liquid/filters/base.rb', line 96

def floor(input)
  to_number(input).floor.to_i
end

#ljust(input, integer, padstr = '') ⇒ Object

left justify and padd a string



155
156
157
# File 'lib/redmineup/liquid/filters/base.rb', line 155

def ljust(input, integer, padstr = '')
  input.to_s.ljust(integer, padstr)
end

#md5(input) ⇒ Object



37
38
39
# File 'lib/redmineup/liquid/filters/base.rb', line 37

def md5(input)
  Digest::MD5.hexdigest(input) unless input.blank?
end

#modulo(input, operand) ⇒ Object



81
82
83
# File 'lib/redmineup/liquid/filters/base.rb', line 81

def modulo(input, operand)
  apply_operation(input, operand, :%)
end

#multi_line(input) ⇒ Object



139
140
141
# File 'lib/redmineup/liquid/filters/base.rb', line 139

def multi_line(input)
  input.to_s.gsub("\n", '<br/>').html_safe
end

#plus_days(input, distanse) ⇒ Object

example:

{{ today | plus_days: 2 }}


61
62
63
64
65
# File 'lib/redmineup/liquid/filters/base.rb', line 61

def plus_days(input, distanse)
  return '' if input.nil?
  days = distanse.to_i
  input.to_date + days.days rescue 'Invalid date'
end

#random(input) ⇒ Object



33
34
35
# File 'lib/redmineup/liquid/filters/base.rb', line 33

def random(input)
  rand(input.to_i)
end

#regex_replace(str, regex_search, value_replace) ⇒ Object



41
42
43
44
# File 'lib/redmineup/liquid/filters/base.rb', line 41

def regex_replace(str, regex_search, value_replace)
  regex = /#{regex_search}/
  return str.gsub(regex, value_replace)
end

#regex_replace_once(str, regex_search, value_replace) ⇒ Object



46
47
48
49
# File 'lib/redmineup/liquid/filters/base.rb', line 46

def regex_replace_once(str, regex_search, value_replace)
  regex = /#{regex_search}/
  return str.sub(regex, value_replace)
end

#rjust(input, integer, padstr = '') ⇒ Object

right justify and padd a string



150
151
152
# File 'lib/redmineup/liquid/filters/base.rb', line 150

def rjust(input, integer, padstr = '')
  input.to_s.rjust(integer, padstr)
end

#round(input, n = 0) ⇒ Object



85
86
87
88
89
90
# File 'lib/redmineup/liquid/filters/base.rb', line 85

def round(input, n = 0)
  result = to_number(input).round(to_number(n))
  result = result.to_f if result.is_a?(BigDecimal)
  result = result.to_i if n == 0
  result
end

#shuffle(array) ⇒ Object



29
30
31
# File 'lib/redmineup/liquid/filters/base.rb', line 29

def shuffle(array)
  array.to_a.shuffle
end

#textile(input) ⇒ Object



159
160
161
# File 'lib/redmineup/liquid/filters/base.rb', line 159

def textile(input)
  ::RedCloth3.new(input).to_html
end

#textilize(input) ⇒ Object



13
14
15
# File 'lib/redmineup/liquid/filters/base.rb', line 13

def textilize(input)
  RedCloth3.new(input).to_html
end

#underscore(input) ⇒ Object



21
22
23
# File 'lib/redmineup/liquid/filters/base.rb', line 21

def underscore(input)
  input.to_s.gsub(' ', '_').gsub('/', '_').underscore
end

#utc(input) ⇒ Object

example:

{{ now | utc }}


76
77
78
79
# File 'lib/redmineup/liquid/filters/base.rb', line 76

def utc(input)
  return '' if input.nil?
  input.to_time.utc rescue 'Invalid date'
end