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
- #convert_array_to_json(id_value_array, selected_id) ⇒ Object
- #lookup_display_value(id_value_array, id_str) ⇒ Object
-
#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 display_text : either overrule the normal display-value, or needed when using loadurl 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 selected : (optional) boolean, text selected on edit callback : (optional) a javascript function that is called after form has been submitted.
Instance Method Details
#convert_array_to_json(id_value_array, selected_id) ⇒ Object
83 84 85 86 87 |
# File 'lib/on_the_spot/on_the_spot_helpers.rb', line 83 def convert_array_to_json(id_value_array, selected_id) result = id_value_array.to_h result['selected'] = selected_id unless selected_id.nil? result.to_json end |
#lookup_display_value(id_value_array, id_str) ⇒ Object
78 79 80 81 |
# File 'lib/on_the_spot/on_the_spot_helpers.rb', line 78 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
display_text : either overrule the normal display-value, or needed when using loadurl
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
selected : (optional) boolean, text selected on edit
callback : (optional) a javascript function that is called after form has been submitted
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 |
# File 'lib/on_the_spot/on_the_spot_helpers.rb', line 22 def on_the_spot_edit(object, field, ={}) .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'} ) [:url].merge!(:raw => true) if [:raw] update_url = url_for([:url]) field_value = object.send(field.to_sym).to_s field_value = field_value.html_safe if [:raw] = { :id => "#{object.class.name.underscore}__#{field}__#{object.id}", :class => 'on_the_spot_editing', :'data-url' => update_url} editable_type = [:type].nil? ? nil : [:type].to_sym [:'data-edittype'] = editable_type.to_s unless editable_type.nil? if editable_type == :select && [:loadurl].nil? # we should find a hash select_data = [:data] raise OnTheSpotMissingParameters.new("Using type select needs either data or loadurl to function!") if select_data.nil? [:'data-select'] = convert_array_to_json(select_data, field_value) elsif editable_type == :textarea [:'data-rows'] = [:rows] [:'data-columns'] = [:columns] end [:'data-ok'] = [:ok_text] [:'data-cancel'] = [:cancel_text] [:'data-tooltip'] = [:tooltip] [:'data-auth'] = form_authenticity_token if defined? form_authenticity_token [:'data-selected'] = [:selected] [:'data-callback'] = [:callback] [:'data-onblur'] = [:onblur] if [:onblur] && ['cancel','submit', 'ignore'].include?([:onblur]) [:'data-loadurl'] = [:loadurl] unless [:loadurl].nil? [:'data-display-method'] = [:display_method] unless [:display_method].nil? if [:'data-display-method'].present? && [:'data-loadurl'].nil? [:'data-loadurl'] = url_for(:action => 'get_attribute_on_the_spot') end content_tag("span", ) do if [:display_text] [:display_text] elsif [:display_method] object.send([:display_method].to_sym).to_s elsif editable_type == :select && [:loadurl].nil? lookup_display_value(select_data, field_value) else field_value end end end |