Module: BestInPlace::Helper

Defined in:
lib/best_in_place/helper.rb

Instance Method Summary collapse

Instance Method Details

#best_in_place(object, field, opts = {}) ⇒ Object



3
4
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
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
# File 'lib/best_in_place/helper.rb', line 3

def best_in_place(object, field, opts = {})

  best_in_place_assert_arguments(opts)
  type = opts[:as] || :input
  field = field.to_s

  options = {}
  options[:data] = HashWithIndifferentAccess.new(opts[:data])
  options[:data]['bip-type'] = type
  options[:data]['bip-attribute'] = field

  real_object = best_in_place_real_object_for object

  display_value = best_in_place_build_value_for(real_object, field, opts)

  value = real_object.send(field)

  if opts[:collection] or type == :checkbox
    collection = opts[:collection]
    value = value.to_s
    collection = best_in_place_default_collection if collection.blank?
    collection = best_in_place_collection_builder(type, collection)
    display_value = collection.flat_map{|a| a[0].to_s == value ? a[1] : nil }.compact[0]
    collection = collection.to_json
    options[:data]['bip-collection'] = html_escape(collection)
  end

  options[:class] = ['best_in_place'] + Array(opts[:class] || opts[:classes])
  options[:id] = opts[:id] || BestInPlace::Utils.build_best_in_place_id(real_object, field)

  pass_through_html_options(opts, options)

  options[:data]['bip-activator'] = opts[:activator].presence

  options[:data]['bip-html-attrs'] = opts[:html_attrs].to_json unless opts[:html_attrs].blank?
  options[:data]['bip-inner-class'] = opts[:inner_class].presence

  options[:data]['bip-placeholder'] = html_escape(opts[:place_holder]).presence

  options[:data]['bip-object'] = opts[:param] || BestInPlace::Utils.object_to_key(real_object)
  options[:data]['bip-ok-button'] = opts[:ok_button].presence
  options[:data]['bip-ok-button-class'] = opts[:ok_button_class].presence
  options[:data]['bip-cancel-button'] = opts[:cancel_button].presence
  options[:data]['bip-cancel-button-class'] = opts[:cancel_button_class].presence
  options[:data]['bip-original-content'] = html_escape(opts[:value] || value).presence

  options[:data]['bip-skip-blur'] = opts.has_key?(:skip_blur) ? opts[:skip_blur].presence : BestInPlace.skip_blur

  options[:data]['bip-url'] = url_for(opts[:url] || object)

  if real_object.respond_to?(:new_record?) and real_object.new_record?
    options[:data]['bip-new-object'] = true
    # collect name => value map of unsaved attributes to be serialized
    options[:data]['bip-extra-payload'] = Hash[real_object.changes.map { |k,v| [k, v[1]] }]
  end

  options[:data]['bip-confirm'] = opts[:confirm].presence
  options[:data]['bip-value'] = html_escape(value).presence

  if opts[:raw]
    options[:data]['bip-raw'] = 'true'
  end

  # delete nil keys only
  options[:data].delete_if { |_, v| v.nil? }
  container = opts[:container] || BestInPlace.container
  (container, display_value, options, opts[:raw].blank?)
end

#best_in_place_if(condition, object, field, opts = {}) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/best_in_place/helper.rb', line 72

def best_in_place_if(condition, object, field, opts = {})
  if condition
    best_in_place(object, field, opts)
  else
    best_in_place_build_value_for best_in_place_real_object_for(object), field, opts
  end
end

#best_in_place_unless(condition, object, field, opts = {}) ⇒ Object



80
81
82
# File 'lib/best_in_place/helper.rb', line 80

def best_in_place_unless(condition, object, field, opts = {})
  best_in_place_if(!condition, object, field, opts)
end