Module: Supplejack::Controllers::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/supplejack/controllers/helpers.rb

Instance Method Summary collapse

Instance Method Details

#attribute(record, attributes, options = {}) ⇒ String

Displays a record attribute with a label and allows you to customize the HTML markup generated. It will not display anything if the value is blank or nil.

Options Hash (options):

  • :label (true, false)

    Display the attribute name

  • :label_tag (String)

    HTML tag to surround label

  • :label_class (String)

    CSS class to apply to label_tag

  • :limit (Integer)

    Number of charachters to truncate or number of values (when multivalue field)

  • :delimiter (Integer)

    Used to separate multi value attributes

  • :link_path (String)

    The method name of a routes path when provided it will generate links for every value

  • :tag (Symbol)

    HTML tag to wrap the label and value

  • :link (true, false, String)

    When true will try to make the value into a link, When is a string it will try to find a {value} within the string and replace it with the value.

  • :extra_html (String)

    HTML which will be included inside the tag at the end.

  • :tag_class (Symbol)

    The class for the attribute tag



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/supplejack/controllers/helpers.rb', line 43

def attribute(record, attributes, options={})
  options.reverse_merge!(:label => true, :label_inline => true, :limit => nil, :delimiter => ", ",
                          :link_path => false, :tag => Supplejack.attribute_tag, :label_tag => Supplejack.label_tag,
                          :label_class => Supplejack.label_class, :trans_key => nil, :link => false,
                          :extra_html => nil, :tag_class => nil)

  value = []
  attributes = [attributes] unless attributes.is_a?(Array)
  attributes.each do |attribute|
    if attribute.is_a?(String) && attribute.match(/\./)
      object, attr = attribute.split(".")
      v = record.try(object.to_sym).try(attr.to_sym) if object && attr
    else
      v = record.send(attribute)
    end

    if v.is_a?(Array)
      value += v.compact
    else
      value << v if v.present?
    end
  end

  value = value.first if value.size == 1
  attribute = attributes.first

  if value.is_a?(Array)
    if options[:limit] && options[:limit].to_i > 0
      value = value[0..(options[:limit].to_i-1)]
    end

    if options[:link_path]
      value = value.map {|v| link_to(v, send(options[:link_path], {:i => {attribute => v}})) }
    end

    if options[:link]
      value = value.map do |v|
        attribute_link_replacement(v, options[:link])
      end
    end

    value = value.join(options[:delimiter]).html_safe
    value = truncate(value, :length => options[:limit]) if options[:limit].to_i > 20
    value
  else
    if options[:limit] && options[:limit].to_i > 0
      value = truncate(value, :length => options[:limit].to_i)
    end

    if options[:link]
      value = attribute_link_replacement(value, options[:link])
    end
  end

  content = ""
  if options[:label]
    if options[:trans_key].present?
      translation = I18n.t(options[:trans_key], :default => attribute.to_s.capitalize) + ": "
    else
      i18n_class_name = record.class.to_s.tableize.downcase.gsub(/\//, "_")
      translation = "#{I18n.t("#{i18n_class_name}.#{attribute}", :default => attribute.to_s.capitalize)}: "
    end
    content = (options[:label_tag], translation, :class => options[:label_class]).html_safe
    content << "<br/>".html_safe unless options[:label_inline]
  end

  content << value.to_s
  content << options[:extra_html] if options[:extra_html]
  if value.present? and value != "Not specified"
    options[:tag] ? (options[:tag], content.html_safe, :class => options[:tag_class]) : content.html_safe
  end
end


116
117
118
119
120
121
122
123
124
# File 'lib/supplejack/controllers/helpers.rb', line 116

def attribute_link_replacement(value, link_pattern)
  if link_pattern.is_a?(String)
    link_pattern = URI.decode(link_pattern)
    url = link_pattern.gsub("{{value}}", value)
    link_to(value, url)
  else
    auto_link value
  end
end

#search(special_params = nil) ⇒ Object



15
16
17
18
19
# File 'lib/supplejack/controllers/helpers.rb', line 15

def search(special_params=nil)
  return @supplejack_search if @supplejack_search
  klass = Supplejack.search_klass ? Supplejack.search_klass.classify.constantize : Supplejack::Search
  @supplejack_search = klass.new(special_params || params[:search] || params)
end