Module: Coupler::Helpers

Defined in:
lib/coupler/helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_javascript(*names) ⇒ Object



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

def add_javascript(*names)
  javascripts.push(*names)
end

#add_stylesheet(*names) ⇒ Object



43
44
45
# File 'lib/coupler/helpers.rb', line 43

def add_stylesheet(*names)
  stylesheets.push(*names)
end


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
# File 'lib/coupler/helpers.rb', line 51

def breadcrumbs
  # This method is fun but silly.
  if @breadcrumbs
    url = ""
    %{<div id="breadcrumbs">} +
      @breadcrumbs.inject([]) do |arr, obj|
        strings =
          case obj
          when String
            [obj]
          when nil
            []
          else
            class_name = obj.class.to_s.split("::")[-1]
            if obj.new?
              ["New #{class_name}"]
            elsif
              url += "/#{class_name.downcase}s/#{obj.id}"
              if obj.respond_to?(:name)
                ["#{class_name}s", %{<a href="#{url}">#{obj.name}</a>}]
              else
                [%{<a href="#{url}">#{class_name} ##{obj.id}</a>}]
              end
            end
          end
        arr.push(*strings.collect { |x| %{<div class="crumb">#{x}</div>} })
      end.join(%{<div class="crumb">/</div>}) +
      %{</div><div class="clear"></div>}
  end
end

#cycle(even, odd) ⇒ Object



117
118
119
# File 'lib/coupler/helpers.rb', line 117

def cycle(even, odd)
  (@_cycle = !@_cycle) ? even : odd
end


47
48
49
# File 'lib/coupler/helpers.rb', line 47

def delete_link(text, url)
  %^<a href="#{url}" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href; var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m); f.submit(); }; return false;">#{text}</a>^
end

#error_messages_for(object) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/coupler/helpers.rb', line 3

def error_messages_for(object)
  return ""   if object.nil? || object.errors.empty?

  retval = "<div class='errors'><h3>Errors detected:</h3><ul>"
  object.errors.each do |(attr, messages)|
    messages.each do |message|
      retval += "<li>"
      retval += attr.to_s.tr("_", " ").capitalize + " " if attr != :base
      retval += "#{message}</li>"
    end
  end
  retval += "</ul></div><div class='clear'></div>"

  retval
end

#field_type(resource, field) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/coupler/helpers.rb', line 121

def field_type(resource, field)
  result = field.final_type
  if !resource.import_id
    result += " (#{field.final_db_type})"
  end
  result
end

#form_tag_for(obj, options) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/coupler/helpers.rb', line 96

def form_tag_for(obj, options)
  base_url = options[:base_url]
  action, method = if obj.new?
                     [base_url, ""]
                   else
                     ["#{base_url}/#{obj.id}", %{<div style="display: none;"><input type="hidden" name="_method" value="put" /></div>}]
                   end
  %{<form action="#{action}" method="post">#{method}}
end

#humanize(string) ⇒ Object



82
83
84
# File 'lib/coupler/helpers.rb', line 82

def humanize(string)
  string.to_s.gsub(/_+/, " ").capitalize
end

#javascript_includesObject



23
24
25
26
27
# File 'lib/coupler/helpers.rb', line 23

def javascript_includes
  javascripts.collect do |name|
    %{<script type="text/javascript" src="/js/#{name}"></script>}
  end.join("\n  ")
end

#javascriptsObject



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

def javascripts
  @javascripts ||= %w{jquery.min.js jquery.timeago.js jquery-ui.min.js application.js}
end

#local_ipObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/coupler/helpers.rb', line 106

def local_ip
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily

  UDPSocket.open do |s|
    s.connect '64.233.187.99', 1
    s.addr.last
  end
ensure
  Socket.do_not_reverse_lookup = orig
end


37
38
39
40
41
# File 'lib/coupler/helpers.rb', line 37

def stylesheet_links
  stylesheets.collect do |name|
    %{<link rel="stylesheet" type="text/css" media="all" href="/css/#{name}" />}
  end.join("\n  ")
end

#stylesheetsObject



33
34
35
# File 'lib/coupler/helpers.rb', line 33

def stylesheets
  @stylesheets ||= %w{reset.css text.css 960.css jquery-ui.css jquery.treeview.css style.css}
end

#timeago(time, klass = nil, tag = "div") ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/coupler/helpers.rb', line 86

def timeago(time, klass = nil, tag = "div")
  if time.nil?
    "Never"
  else
    dt = time.send(:to_datetime)
    klass = "timeago" + (klass.nil? ? "" : " #{klass}")
    %{<#{tag} class="#{klass}" title="#{dt.to_s}">#{time.to_s}</#{tag}>}
  end
end