Module: Jipe::Helpers

Defined in:
lib/jipe.rb

Instance Method Summary collapse

Instance Method Details

#jipe_editor(record, field, options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jipe.rb', line 61

def jipe_editor(record, field, options = {})
  options = { :external_control => true,
    :class => record.class.to_s,
    :rows => 1,
    :editing => true,
    :on_complete => nil }.update(options || {})
  rclass = options[:class]
  outstr = <<-ENDDOC
    <span id="#{jipe_id_for(record, field, options)}">
      #{record.send(field).to_s}
    </span>
  ENDDOC
  if options[:editing]
    outstr += <<-ENDDOC
      #{ options[:external_control] ? image_tag("jipe/edit-field.png",
        { :id => "edit_#{jipe_id_for(record, field, options)}" }) : "" }
      #{ jipe_editor_for(record, field, options)}
    ENDDOC
  end
  return outstr.html_safe
end

#jipe_editor_for(record, field, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jipe.rb', line 23

def jipe_editor_for(record, field, options = {})
  options = { :external_control => true,
    :class => record.class.to_s,
    :rows => 1 }.update(options || {})
  rclass = options[:class]
  outstr = <<-ENDDOC
    <script type="text/javascript">
        new Jipe.InPlaceEditor("#{jipe_id_for(record, field, options)}",
        #{rclass}, #{record.id}, #{field.to_json}, {
  ENDDOC
  if options[:external_control]
    outstr << "externalControl: 'edit_#{jipe_id_for(record, field, options)}', "
    options.delete(:external_control)
  end
  if options[:on_complete]
    outstr << "onComplete: #{options[:on_complete]}, "
    options.delete(:on_complete)
  end
  if protect_against_forgery?
    outstr << "authenticityToken: '#{form_authenticity_token}', "
  end
  outstr << "rows: #{options[:rows]},"
  options.delete(:rows)
  options.delete(:id)

  # everything else is ajax options
  outstr << "ajaxOptions: {"
  options.each_pair do |k, v|
    outstr << "'#{escape_javascript k.to_s}': '#{escape_javascript v.to_s}',"
  end
  unless options[:attributes]
    outstr << "'attributes[]': '#{escape_javascript field.to_s}'"
  end

  outstr << "}});\n</script>"
  return outstr.html_safe
end

#jipe_id_for(record, field, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jipe.rb', line 11

def jipe_id_for(record, field, options = {})
  options = {
    :class => record.class.to_s,
    :id => nil }.update(options || {})
  rclass = options[:class]
  if options[:id]
    return options[:id]
  else
    return "#{rclass.downcase}_#{record.id}_#{field}"
  end
end

#jipe_image_toggle(record, field, true_image, false_image, options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/jipe.rb', line 117

def jipe_image_toggle(record, field, true_image, false_image, options = {})
  options = {
    :class => record.class.to_s,
    :on_complete => nil,
  }.update(options || {})
  rclass = options[:class]
  value = record.send(field)
  idprefix = jipe_id_for(record, field, options)

  js_options = {}
  js_options['onComplete'] = options[:on_complete] if options[:on_complete]
  if protect_against_forgery?
    js_options["authenticityToken"] = form_authenticity_token
  end

  outstr = <<-ENDDOC
    #{image_tag true_image, :id => "#{idprefix}_true", 
        :style => (value ? "" : "display: none") }
    #{image_tag false_image, :id => "#{idprefix}_false", 
        :style => (value ? "display: none" : "") }
    <script type="text/javascript">
      new Jipe.ImageToggle("#{idprefix}_true", "#{idprefix}_false",
        #{rclass}, #{record.id}, #{field.to_json},
        #{options_for_javascript js_options});
    </script>
  ENDDOC
  return outstr.html_safe
end

#jipe_select_field(record, field, choices, options = {}, html_options = {}) ⇒ Object



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
115
# File 'lib/jipe.rb', line 83

def jipe_select_field(record, field, choices, options = {}, html_options = {})
  options = {
    :class => record.class.to_s,
    :on_complete => nil,
  }.update(options || {})
  
  rclass = options.delete(:class)
  value = record.send(field)
  id = jipe_id_for(record, field, options)

  js_options = {}
  js_options['onComplete'] = options.delete(:on_complete) if options[:on_complete]
  if protect_against_forgery?
    js_options["authenticityToken"] = form_authenticity_token
  end
  options.each do |k, v|
    if v.nil?
      options.delete(k)
    elsif v.kind_of? String
      options[k] = "'#{escape_javascript(v)}'"
    end
  end
  js_options['ajaxOptions'] = options_for_javascript(options)

  outstr = <<-ENDDOC
  #{select_tag id, options_for_select(choices, value), html_options}
  <script type="text/javascript">
    new Jipe.SelectField('#{id}', #{rclass}, #{record.id}, #{field.to_json},
      #{options_for_javascript js_options});
  </script>
  ENDDOC
  return outstr.html_safe
end