Module: X::Editable::Rails::ViewHelpers
- Defined in:
- lib/x-editable-rails/view_helpers.rb
Instance Method Summary collapse
-
#editable(object, method, options = {}) ⇒ Object
Options determine how the HTML tag is rendered and the remaining options are converted to data-* attributes.
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, = {}) = Configuration.(object, method).deep_merge().with_indifferent_access # merge data attributes for backwards-compatibility .merge! .delete(:data){ Hash.new } url = .delete(:url){ polymorphic_path(object) } object = object.last if object.kind_of?(Array) value = .delete(:value){ object.send(method) } source = [:source] ? format_source(.delete(:source), value) : default_source_for(value) classes = format_source(.delete(:classes), value) error = .delete(:e) = .delete(:html){ Hash.new } if xeditable?(object) model = object.class.model_name.param_key nid = .delete(:nid) nested = .delete(:nested) title = .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 = .delete(:class).to_s.split(/\s+/).unshift('editable') css_list << classes[output_value] if classes type = .delete(:type){ default_type_for(value) } css = css_list.compact.uniq.join(' ') tag = .delete(:tag){ 'span' } placeholder = .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(.symbolize_keys) data.reject!{|_, value| value.nil?} .update({ class: css, title: title, data: data }) content_tag tag, 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 |