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.

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).



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
# File 'lib/jeditable-rails/helpers/jeditable_helper.rb', line 23

def editable_field(object, property, options={})
  name = "#{object.class.to_s.underscore}[#{property}]"
  value = object.send property
  update_url = options.delete(:update_url) || url_for(object)
  args = {:method => 'PUT', :name => name}.merge(options)
  %{
    <span class="editable" data-id="#{object.id}" data-name="#{name}">#{value}</span>
    <script type="text/javascript">
      (function( $ ){
        $(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-id='#{object.id}'][data-name='#{name}']").editable("#{update_url}", args);
        });
      })( jQuery );
    </script>
  }.html_safe
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
# File 'lib/jeditable-rails/helpers/jeditable_helper.rb', line 5

def editable_field_if(condition, object, property, options={})
  if condition
    editable_field(object, property, options)
  else
    object.send property
  end
end