Module: Helpers::General

Defined in:
lib/helpers/general.rb

Instance Method Summary collapse

Instance Method Details

Since:

  • 0.0.1



7
8
9
10
# File 'lib/helpers/general.rb', line 7

def copyright(from)
  now = Time.now.year
  now.eql?(from) ? now : "#{from} - #{now}"
end

mail_to “[email protected]

> “<a href=‘[email protected]’>[email protected]</a>”

mail_to “[email protected]”, “Title”

> “<a href=‘[email protected]’>Title</a>”

Since:

  • 0.0.2



28
29
30
31
# File 'lib/helpers/general.rb', line 28

def email_link(mail, text = mail)
  mail.gsub!("@" "&#x40;")
  Tag.new(:a, text, href: "mailto:#{mail}")
end

#error_messages_for(model_instance) ⇒ Object

Since:

  • 0.0.2



34
35
36
37
38
39
# File 'lib/helpers/general.rb', line 34

def error_messages_for(model_instance)
  Tag.new(:ul) do
    messages = model_instance.errors.full_messages
    messages.map { |message| tag :li, message }
  end
end

Since:

  • 0.0.2



19
20
21
# File 'lib/helpers/general.rb', line 19

def link_item(name, url)
  Tag.new(:li, link_to(name, url))
end

Since:

  • 0.0.2



13
14
15
16
# File 'lib/helpers/general.rb', line 13

def link_to(name, url, options = Hash.new)
  default = {href: URI.escape(url), title: name.to_s.gsub(/'/, '&apos;')}
  Tag.new(:a, name, default.merge(options))
end

#truncate(text, *args) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/helpers/general.rb', line 41

def truncate(text, *args)
  options = args.extract_options!
  unless args.empty?
    options[:size]     = args[0] || 75
    options[:omission] = args[1] || "..."
  end
  options = {size: 75, omission: "..."}.merge(options)
  text.scan(/(\S+)(\s+)/)[0..options[:size]].flatten.join << options[:omission] if text
end