require 'jipe/version'
module Jipe
class Engine < Rails::Engine
initializer 'jipe.extend_helpers' do
ActionView::Base.send :include, Jipe::Helpers
end
end
module Helpers
def jipe_id_for(record, field, options = {})
options = {
:class => record.class.to_s,
:id => nil }.update(options || {})
rclass = options[:class]
if options[:id]
return options[:id]
else
return "#{rclass.downcase}_#{record.id}_#{field}"
end
end
def jipe_editor_for(record, field, options = {})
options = { :external_control => true,
:class => record.class.to_s,
:rows => 1 }.update(options || {})
rclass = options[:class]
outstr = <<-ENDDOC
"
return outstr.html_safe
end
def jipe_editor(record, field, options = {})
options = { :external_control => true,
:class => record.class.to_s,
:rows => 1,
:editing => true,
:on_complete => nil }.update(options || {})
rclass = options[:class]
outstr = <<-ENDDOC
#{record.send(field).to_s}
ENDDOC
if options[:editing]
outstr += <<-ENDDOC
#{ options[:external_control] ? image_tag("jipe/edit-field.png",
{ :id => "edit_#{jipe_id_for(record, field, options)}" }) : "" }
#{ jipe_editor_for(record, field, options)}
ENDDOC
end
return outstr.html_safe
end
def jipe_select_field(record, field, choices, options = {}, html_options = {})
options = {
:class => record.class.to_s,
:on_complete => nil,
}.update(options || {})
rclass = options.delete(:class)
value = record.send(field)
id = jipe_id_for(record, field, options)
js_options = {}
js_options['onComplete'] = options.delete(:on_complete) if options[:on_complete]
if protect_against_forgery?
js_options["authenticityToken"] = form_authenticity_token.to_json
end
options.each do |k, v|
if v.nil?
options.delete(k)
elsif v.kind_of? String
options[k] = "'#{escape_javascript(v)}'"
end
end
js_options['ajaxOptions'] = options_for_javascript(options)
outstr = <<-ENDDOC
#{select_tag id, options_for_select(choices, value), html_options}
ENDDOC
return outstr.html_safe
end
def jipe_image_toggle(record, field, true_image, false_image, options = {})
options = {
:class => record.class.to_s,
:on_complete => nil,
}.update(options || {})
rclass = options[:class]
value = record.send(field)
idprefix = jipe_id_for(record, field, options)
js_options = {}
js_options['onComplete'] = options[:on_complete] if options[:on_complete]
if protect_against_forgery?
js_options["authenticityToken"] = form_authenticity_token.to_json
end
outstr = <<-ENDDOC
#{image_tag true_image, :id => "#{idprefix}_true",
:style => (value ? "" : "display: none") }
#{image_tag false_image, :id => "#{idprefix}_false",
:style => (value ? "display: none" : "") }
ENDDOC
return outstr.html_safe
end
end
end