Module: JeditableHelper

Included in:
ActionView::Base
Defined in:
lib/jeditable-rails/helpers/jeditable_helper.rb

Instance Method Summary collapse

Instance Method Details

#editable_field(object, property, options = {}) ⇒ Object

Creates an editable span for the given property of the given object. When clicked, the

Options

:method

Specify the HTTP method to use: 'PUT' or 'POST'.

:name

The name attribute to be used when the form is posted.

:update_url

The URL to submit the form to. Defaults to url_for(object).



45
46
47
# File 'lib/jeditable-rails/helpers/jeditable_helper.rb', line 45

def editable_field(object, property, options={})
  editable_field_if(true, object, property, options={})
end

#editable_field_if(condition, object, property, options = {}) ⇒ Object

If the condition evaluates to true, an editable field will be created. Otherwise, the value of the property of the object is returned. See #editable_field for options.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jeditable-rails/helpers/jeditable_helper.rb', line 5

def editable_field_if(condition, object, property, options={})
  value = object.send property    
  if condition
    name = "#{object.class.to_s.underscore}[#{property}]"
    update_url = options.delete(:update_url) || url_for(object)
    args = {:method => 'PUT', :name => name}.merge(options)
    %{
      <span class="editable" data-name="#{name}">#{value}</span>
      <script type="text/javascript">
        $(function(){          
          var args = {data: function(value, settings) {
            // Unescape HTML
            var retval = value
              .replace(/&amp;/gi, '&')
              .replace(/&gt;/gi, '>')
              .replace(/&lt;/gi, '<')
              .replace(/&quot;/gi, "\\\"");
            return retval;
          }};
          $.extend(args, #{args.to_json});
          $(".editable[data-name='#{name}']").editable("#{update_url}", args);
        });
      </script>
    }.html_safe
  else
    value
  end
end