Module: ChiliProject::Liquid::OverriddenFilters

Defined in:
lib/chili_project/liquid/filters.rb

Instance Method Summary collapse

Instance Method Details

#date(input, format = nil) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chili_project/liquid/filters.rb', line 68

def date(input, format=nil)
  if format.nil?
    return "" unless input
    if Setting.date_format.blank?
      input = super(input, '%Y-%m-%d')
      return ::I18n.l(input.to_date) if input.respond_to?(:to_date)
      input # default return value
    else
      super(input, Setting.date_format)
    end
  else
    super
  end
end

#first(array, count = nil) ⇒ Object

Get the first element(s) of the passed in array Example:

{{ product.images | first | to_img }}

51
52
53
54
55
56
# File 'lib/chili_project/liquid/filters.rb', line 51

def first(array, count=nil)
  return array.first if count.nil? && array.respond_to?(:first)
  if array.respond_to?(:[])
    count.to_i > 0 ? array[0..count.to_i-1] : []
  end
end

#last(array, count = nil) ⇒ Object

Get the last element(s) of the passed in array Example:

{{ product.images | last | to_img }}

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

def last(array, count=nil)
  return array.last if count.nil? && array.respond_to?(:last)
  if array.respond_to?(:[])
    count.to_i > 0 ? array[(count.to_i * -1)..-1] : []
  end
end

#newline_to_br(input) ⇒ Object

Add <br /> tags in front of all newlines in input string


32
33
34
# File 'lib/chili_project/liquid/filters.rb', line 32

def newline_to_br(input)
  input.to_s.gsub(/(\r?\n)/, "<br />\1")
end

#replace(input, string, replacement = '') ⇒ Object

Use the block systax for sub and gsub to prevent interpretation of backreferences See gist.github.com/1491437


39
40
41
# File 'lib/chili_project/liquid/filters.rb', line 39

def replace(input, string, replacement = '')
  input.to_s.gsub(string){replacement}
end

#replace_first(input, string, replacement = '') ⇒ Object

Replace the first occurrences of a string with another


44
45
46
# File 'lib/chili_project/liquid/filters.rb', line 44

def replace_first(input, string, replacement = '')
  input.to_s.sub(string){replacement}
end

#split(input, pattern = nil) ⇒ Object

Split input string into an array of substrings separated by given pattern. Default to whitespace


23
24
25
# File 'lib/chili_project/liquid/filters.rb', line 23

def split(input, pattern=nil)
  input.split(pattern)
end

#strip_newlines(input) ⇒ Object


27
28
29
# File 'lib/chili_project/liquid/filters.rb', line 27

def strip_newlines(input)
  input.to_s.gsub(/\r?\n/, '')
end