Module: Spyro::ActionViewExtension::CollectionForHelper::Output::Fields

Included in:
Table
Defined in:
lib/spyro/collections/outputs/fields.rb

Constant Summary collapse

@@fields =

Key => [class => object class ; symbol => db_type, string for pattern matching]

{ [ActiveSupport::TimeWithZone, ""]  => {:method => :datetime},
[Object, /_prct$/]                 => {:method => :prct},
[Float, ""]                        => {:method => :float},
[:boolean, ""]                     => {:method => :boolean},
[:text, /_html$/]                  => {:method => :html},
[:text, /code$/]                   => {:method => :html},
[:text, ""]                        => {:method => :text},
[String, /_url$/]                  => {:method => :url},
#[String, /_email$/]                => {:method => :email},
[String, ""]                       => {:method => :string}, }

Instance Method Summary collapse

Instance Method Details

#format_boolean(value, options) ⇒ Object



53
54
55
# File 'lib/spyro/collections/outputs/fields.rb', line 53

def format_boolean value, options
  value ? "Yes" : "No"
end

#format_classy_enum(value, options) ⇒ Object



33
34
35
# File 'lib/spyro/collections/outputs/fields.rb', line 33

def format_classy_enum value, options
  value.blank? ? "" : value.text
end

#format_cw_file(value, options) ⇒ Object



45
46
47
# File 'lib/spyro/collections/outputs/fields.rb', line 45

def format_cw_file value, options
  link_to value.file.filename, value.url unless value.file.nil?
end

#format_cw_image(value, options) ⇒ Object



41
42
43
# File 'lib/spyro/collections/outputs/fields.rb', line 41

def format_cw_image value, options
  cw_image_tag value, options[:version]
end

#format_datetime(value, options) ⇒ Object



37
38
39
# File 'lib/spyro/collections/outputs/fields.rb', line 37

def format_datetime value, options
  l(value, :format => options[:format] || :datetime)
end

#format_default(value, options) ⇒ Object



29
30
31
# File 'lib/spyro/collections/outputs/fields.rb', line 29

def format_default value, options
  value.to_s
end

#format_email(value, options) ⇒ Object



49
50
51
# File 'lib/spyro/collections/outputs/fields.rb', line 49

def format_email value, options
  mail_to value
end

#format_float(value, options) ⇒ Object



95
96
97
# File 'lib/spyro/collections/outputs/fields.rb', line 95

def format_float value, options
  value ? ("%.2f" % value) : "N/A"
end

#format_html(value, options) ⇒ Object



103
104
105
# File 'lib/spyro/collections/outputs/fields.rb', line 103

def format_html value, options
  "<span class='has-popover' data-html='true' data-content='#{@h.send(:h, value.to_s)}'>#{@h.truncate(@h.strip_tags(HTMLEntities.new.decode(value.to_s)), :length => 40, :separator => ' ')}</span>".html_safe
end

#format_period(values, options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/spyro/collections/outputs/fields.rb', line 57

def format_period values, options
  begin_at, end_at, current = *values
  current ||= Time.zone.now

  total, actual = (end_at.to_i - begin_at.to_i), (current.to_i - begin_at.to_i)
  current_percentage = [0, 100, (actual * 100) / total].sort.second
"      <div class=\"col-md-12\">\n<span class=\"date-before col-md-6 pull-left\">\#{l(begin_at, :format => options[:format] || :datetime)}</span>\n<span class=\"date-after col-md-6 pull-right\">\#{l(end_at, :format => options[:format] || :datetime)}</span>\n<div class=\"col-md-10 col-md-offset-1\">\n\#{progress current_percentage}\n</div>\n        </div>\n"

end

#format_prct(value, options) ⇒ Object



91
92
93
# File 'lib/spyro/collections/outputs/fields.rb', line 91

def format_prct value, options
  value ? ("%.2f%%" % value) : "N/A"
end

#format_progress(values, options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/spyro/collections/outputs/fields.rb', line 75

def format_progress values, options
  actual, total = *values
  current_percentage = [0, 100, (actual * 100) / total].sort.second rescue 0
"      <div class=\"col-md-12\">\n<div class=\"col-md-12 percentage-label\">\n  \#{actual} / \#{total}\n</div>\n<div class=\"col-md-10 col-md-offset-1\">\n  \#{progress current_percentage}\n</div>\n        </div>\n"

end

#format_string(value, options) ⇒ Object



111
112
113
# File 'lib/spyro/collections/outputs/fields.rb', line 111

def format_string value, options
  value.to_s
end

#format_text(value, options) ⇒ Object



107
108
109
# File 'lib/spyro/collections/outputs/fields.rb', line 107

def format_text value, options
  "<span class='has-popover' data-content='#{@h.send(:h, value.to_s)}'>#{@h.truncate(value.to_s, :length => 40, :separator => ' ')}</span>".html_safe
end

#format_url(value, options) ⇒ Object



99
100
101
# File 'lib/spyro/collections/outputs/fields.rb', line 99

def format_url value, options
  link_to value, (value.start_with?('http') ? value : ('http://' + value))
end