module RedmineCrm::Liquid::Filters::Base

Public Instance Methods

attachment(input, file_name) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 113
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) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 94
def call_method(input, method_name)
  if input.respond_to?(method_name)
    input.method(method_name).call
  end
end
ceil(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 82
def ceil(input)
  to_number(input).ceil.to_i
end
concat(input, *args) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 128
def concat(input, *args)
  result = input.to_s
  args.flatten.each { |a| result << a.to_s }
  result
end
currency(input, currency_code = nil) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 90
def currency(input, currency_code = nil)
  price_to_currency(input, currency_code || container_currency, :converted => false)
end
custom_field(input, field_name) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 100
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 }
    custom_value.custom_field.format.formatted_custom_value(nil, custom_value)
  end
end
custom_fields(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 107
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) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 25
def dasherize(input)
  input.to_s.gsub(' ', '-').gsub('/', '-').dasherize
end
date_range(input, distanse) click to toggle source

example:

{{ today | date_range: '2015-12-12' }}
# File lib/redmine_crm/liquid/filters/base.rb, line 59
def date_range(input, distanse)
  return '' if input.nil?
  (input.to_date - distanse.to_date).to_i rescue 'Invalid date'
end
default(input, value) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 17
def default(input, value)
  input.blank? ? value : input
end
encode(input) click to toggle source

example:

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

=> http%3A%3A%2F%2Fwww.example.com%3Fkey%3Dhello+world
# File lib/redmine_crm/liquid/filters/base.rb, line 45
def encode(input)
  ::Rack::Utils.escape(input)
end
floor(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 86
def floor(input)
  to_number(input).floor.to_i
end
ljust(input, integer, padstr = '') click to toggle source

left justify and padd a string

# File lib/redmine_crm/liquid/filters/base.rb, line 140
def ljust(input, integer, padstr = '')
  input.to_s.ljust(integer, padstr)
end
md5(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 37
def md5(input)
  Digest::MD5.hexdigest(input) unless input.blank?
end
modulo(input, operand) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 71
def modulo(input, operand)
  apply_operation(input, operand, :%)
end
multi_line(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 124
def multi_line(input)
  input.to_s.gsub("\n", '<br/>').html_safe
end
plus_days(input, distanse) click to toggle source

example:

{{ today | plus_days: 2 }}
# File lib/redmine_crm/liquid/filters/base.rb, line 51
def plus_days(input, distanse)
  return '' if input.nil?
  days = distanse.to_i
  input.to_date + days.days rescue 'Invalid date'
end
random(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 33
def random(input)
  rand(input.to_i)
end
rjust(input, integer, padstr = '') click to toggle source

right justify and padd a string

# File lib/redmine_crm/liquid/filters/base.rb, line 135
def rjust(input, integer, padstr = '')
  input.to_s.rjust(integer, padstr)
end
round(input, n = 0) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 75
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) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 29
def shuffle(array)
  array.to_a.shuffle
end
textile(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 144
def textile(input)
  ::RedCloth3.new(input).to_html
end
textilize(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 13
def textilize(input)
  RedCloth3.new(input).to_html
end
underscore(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 21
def underscore(input)
  input.to_s.gsub(' ', '_').gsub('/', '_').underscore
end
utc(input) click to toggle source

example:

{{ now | utc }}
# File lib/redmine_crm/liquid/filters/base.rb, line 66
def utc(input)
  return '' if input.nil?
  input.to_time.utc rescue 'Invalid date'
end

Protected Instance Methods

args_to_options(*args) click to toggle source

Convert an array of properties ('key:value') into a hash Ex: ['width:50', 'height:100'] => { :width => '50', :height => '100' }

# File lib/redmine_crm/liquid/filters/base.rb, line 152
def args_to_options(*args)
  options = {}
  args.flatten.each do |a|
    if (a =~ /^(.*):(.*)$/)
      options[$1.to_sym] = $2
    end
  end
  options
end
as_liquid(item) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 216
def as_liquid(item)
  case item
  when Hash
    pairs = item.map { |k, v| as_liquid([k, v]) }
    Hash[pairs]
  when Array
    item.map { |i| as_liquid(i) }
  else
    if item.respond_to?(:to_liquid)
      liquidated = item.to_liquid
      # prevent infinite recursion for simple types (which return `self`)
      if liquidated == item
        item
      else
        as_liquid(liquidated)
      end
    else
      item
    end
  end
end
container() click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 238
def container
  @container ||= @context.registers[:container]
end
container_currency() click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 242
def container_currency
  container.currency if container.respond_to?(:currency)
end
groupable?(element) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 200
def groupable?(element)
  element.respond_to?(:group_by)
end
inline_options(options = {}) click to toggle source

Write options (Hash) into a string according to the following pattern: <key1>=“<value1>”, <key2>=“<value2”, …etc

# File lib/redmine_crm/liquid/filters/base.rb, line 164
def inline_options(options = {})
  return '' if options.empty?
  (options.stringify_keys.sort.to_a.collect { |a, b| "#{a}=\"#{b}\"" }).join(' ') << ' '
end
item_property(item, property) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 204
def item_property(item, property)
  if item.respond_to?(:to_liquid)
    property.to_s.split(".").reduce(item.to_liquid) do |subvalue, attribute|
      subvalue[attribute]
    end
  elsif item.respond_to?(:data)
    item.data[property.to_s]
  else
    item[property.to_s]
  end
end
sort_input(input, property, order) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 169
def sort_input(input, property, order)
  input.sort do |apple, orange|
    apple_property = item_property(apple, property)
    orange_property = item_property(orange, property)

    if !apple_property.nil? && orange_property.nil?
      - order
    elsif apple_property.nil? && !orange_property.nil?
      + order
    else
      apple_property <=> orange_property
    end
  end
end
time(input) click to toggle source
# File lib/redmine_crm/liquid/filters/base.rb, line 184
def time(input)
  case input
  when Time
    input.clone
  when Date
    input.to_time
  when String
    Time.parse(input) rescue Time.at(input.to_i)
  when Numeric
    Time.at(input)
  else
    raise Errors::InvalidDateError,
      "Invalid Date: '#{input.inspect}' is not a valid datetime."
  end.localtime
end