Module: X::Editable::Rails::ViewHelpers

Defined in:
lib/x-editable-rails/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#editable(object, method, options = {}) ⇒ Object

Options determine how the HTML tag is rendered and the remaining options are converted to data-* attributes.

options:

tag:   tag name of element returned
class: "class" attribute on element
placeholder: "placeholder" attribute on element
title: "title" attribute on element (defaults to placeholder)
data:  "data-*" attributes on element
  source: a Hash of friendly display values used by input elements based on (object) value
    - boolean shorthand ['Enabled', 'Disabled'] becomes { '1' => 'Enabled', '0' => 'Disabled' }
    - enumerable shorthand ['Yes', 'No', 'Maybe'] becomes { 'Yes' => 'Yes', 'No' => 'No', 'Maybe' => 'Maybe' }
  classes: a Hash of classes to add based on the value (same format and shorthands as source)
value: override the object's value


22
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/x-editable-rails/view_helpers.rb', line 22

def editable(object, method, options = {})
  options = Configuration.method_options_for(object, method).deep_merge(options).with_indifferent_access
  # merge data attributes for backwards-compatibility
  options.merge! options.delete(:data){ Hash.new }

  url     = options.delete(:url){ polymorphic_path(object) }
  object  = object.last if object.kind_of?(Array)
  value   = options.delete(:value){ object.send(method) }
  source  = options[:source] ? format_source(options.delete(:source), value) : default_source_for(value)
  classes = format_source(options.delete(:classes), value)
  error   = options.delete(:e)
  html_options = options.delete(:html){ Hash.new }

  if xeditable?(object)
    model   = object.class.model_name.param_key
    nid     = options.delete(:nid)
    nested  = options.delete(:nested)
    title   = options.delete(:title) do
      klass = nested ? object.class.const_get(nested.to_s.classify) : object.class
      klass.human_attribute_name(method)
    end

    output_value = output_value_for(value)
    css_list = options.delete(:class).to_s.split(/\s+/).unshift('editable')
    css_list << classes[output_value] if classes
    type = options.delete(:type){ default_type_for(value) }
    css   = css_list.compact.uniq.join(' ')
    tag   = options.delete(:tag){ 'span' }
    placeholder = options.delete(:placeholder){ title }

    # any remaining options become data attributes
    data  = {
      type:   type,
      model:  model,
      name:   method,
      value:  ( type == 'wysihtml5' ? Base64.encode64(output_value) : output_value ), 
      placeholder: placeholder,
      classes: classes,
      source: source,
      url:    url,
      nested: nested,
      nid:    nid
    }.merge(options.symbolize_keys)

    data.reject!{|_, value| value.nil?}

    html_options.update({
      class: css,
      title: title,
      data: data
    })

     tag, html_options do
      if %w(select checklist).include?(data[:type].to_s) && !source.is_a?(String)
        source = normalize_source(source)
        content = source.detect { |t| output_value == output_value_for(t[0]) }
        content.present? ? content[1] : ""
      else
        safe_join(source_values_for(value, source), tag(:br))
      end
    end
  else
    error || safe_join(source_values_for(value, source), tag(:br))
  end
end