Module: OnTheSpot::Helpers

Defined in:
lib/on_the_spot/on_the_spot_helpers.rb

Defined Under Namespace

Classes: OnTheSpotMissingParameters

Constant Summary collapse

EDIT_TYPE_TEXTAREA =
'textarea'
EDIT_TYPE_SELECT =
'select'

Instance Method Summary collapse

Instance Method Details

#convert_array_to_json(id_value_array, selected_id) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/on_the_spot/on_the_spot_helpers.rb', line 79

def convert_array_to_json(id_value_array, selected_id)
  conv_arr = id_value_array.map{|idv| "'#{idv[0]}':'#{idv[1]}'" }
  result = "{ #{conv_arr.join', '}"
  result += ", 'selected':'#{ selected_id.to_s}'" unless selected_id.nil?
  result += "}"
  result
end

#lookup_display_value(id_value_array, id_str) ⇒ Object



74
75
76
77
# File 'lib/on_the_spot/on_the_spot_helpers.rb', line 74

def lookup_display_value(id_value_array, id_str)
  found_pair = id_value_array.select{ |idv| idv[0].to_s == id_str.to_s }
  found_pair.size == 0 ? '' : found_pair[0][1]
end

#on_the_spot_edit(object, field, options = {}) ⇒ Object

Possible options:

ok_text     : the ok-button text
cancel_text : the cancel-button text
tooltip     : the tooltip to show
type        : {'textarea' || 'select' }
rows        : nr of rows for textarea
columns     : nr of columns for textarea
loadurl     : (for select) an url that will return the json for the select
data        : (for select) an array of options in the form [id, value]
url         : (optional) URL to post to if you don't want to use the standard routes


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
71
72
# File 'lib/on_the_spot/on_the_spot_helpers.rb', line 19

def on_the_spot_edit(object, field, options={})
  #!!! to do: translate options to data-fields
  # Possible fields:
  #  type: textarea or not
  #  button-translations ok-Text, cancel-Text
  #



  options.reverse_merge!(:ok_text     => t('on_the_spot.ok'),
                         :cancel_text => t('on_the_spot.cancel'),
                         :tooltip     => t('on_the_spot.tooltip'),
                         :rows        => 5,
                         :columns     => 40,
                         :url         => {:action => 'update_attribute_on_the_spot'}
                         
                        )

  update_url = url_for(options[:url])

  field_value =  object.send(field.to_sym).to_s

  html_options = { :id => "#{object.class.name.underscore}__#{field}__#{object.id}",
                   :class => 'on_the_spot_editing',
                   :'data-url' => update_url}

  editable_type = options[:type].nil? ? nil : options[:type].to_sym
  html_options[:'data-edittype']    = editable_type.to_s unless editable_type.nil?
  if editable_type == :select
    # we need either a data or loadurl
    unless options[:loadurl].nil?
      html_options[:'data-loadurl'] = options[:loadurl]
    else
      # we should find a hash
      select_data = options[:data]
      raise OnTheSpotMissingParameters.new("Using type select needs either data or loadurl to function!") if select_data.nil?
      html_options[:'data-select']  = convert_array_to_json(select_data, field_value)
    end
  elsif editable_type == :textarea
    html_options[:'data-rows']      = options[:rows]
    html_options[:'data-columns']   = options[:columns]
  end
  html_options[:'data-ok']          = options[:ok_text]
  html_options[:'data-cancel']      = options[:cancel_text]
  html_options[:'data-tooltip']     = options[:tooltip]

  ("span", html_options) do
    if editable_type == :select
      lookup_display_value(select_data, field_value)
    else
      field_value
    end
  end
end