Module: BestInPlaceMongoid::BestInPlaceMongoidHelpers

Defined in:
lib/best_in_place_mongoid/helper.rb

Instance Method Summary collapse

Instance Method Details

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



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

def best_in_place(object, field, opts = {})
  if opts[:display_as] && opts[:display_with]
    raise ArgumentError, "Can't use both 'display_as' and 'display_with' options at the same time"
  end

  if opts[:display_with] && !opts[:display_with].is_a?(Proc) && !ViewHelpers.respond_to?(opts[:display_with])
    raise ArgumentError, "Can't find helper #{opts[:display_with]}"
  end
  
  opts[:type] ||= :input
  opts[:collection] ||= []
  field = field.to_s
  value = build_value_for(object, field, opts)
  collection = nil
  if opts[:type] == :select && !opts[:collection].blank?
    v = object.send(field)
    value = Hash[opts[:collection]][!!(v =~ /^[0-9]+$/) ? v.to_i : v]
    collection = opts[:collection].to_json
  end
  if opts[:type] == :checkbox
    fieldValue = !!object.send(field)
    if opts[:collection].blank? || opts[:collection].size != 2
      opts[:collection] = ["No", "Yes"]
    end
    value = fieldValue ? opts[:collection][1] : opts[:collection][0]
    collection = opts[:collection].to_json
  end
  out = "<span class='best_in_place'"
  out << " id='#{BestInPlaceMongoid::Utils.build_best_in_place_id(object, field)}'"
  out << " data-url='#{opts[:path].blank? ? url_for(object).to_s : url_for(opts[:path])}'"
  out << " data-object='#{object.class.to_s.gsub("::", "_").underscore}'"
  out << " data-collection='#{collection}'" unless collection.blank?
  out << " data-attribute='#{field}'"
  out << " data-activator='#{opts[:activator]}'" unless opts[:activator].blank?
  out << " data-nil='#{opts[:nil].to_s}'" unless opts[:nil].blank?
  out << " data-type='#{opts[:type].to_s}'"
  out << " data-inner-class='#{opts[:inner_class].to_s}'" if opts[:inner_class]
  out << " data-html-attrs='#{opts[:html_attrs].to_json}'" unless opts[:html_attrs].blank?
  if !opts[:sanitize].nil? && !opts[:sanitize]
    out << " data-sanitize='false'>"
    out << sanitize(value.to_s, :tags => %w(b i u s a strong em p h1 h2 h3 h4 h5 ul li ol hr pre span img br), :attributes => %w(id class href))
  else
    out << ">#{sanitize(value.to_s, :tags => nil, :attributes => nil)}"
  end
  out << "</span>"
  raw out
end

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



52
53
54
55
56
57
58
# File 'lib/best_in_place_mongoid/helper.rb', line 52

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