module InlineFormsHelper # load form elements. Each element goes into a separate file # and defines a _show, _edit and _update method. # INLINE_FORMS_PATH = File.dirname(__FILE__) + "/form_elements/" Dir[INLINE_FORMS_PATH + "*.rb"].each do |form_element| require form_element end private def inline_forms_version InlineForms::VERSION end def has_validations(object, attribute) "has_validations " unless object.class.validators_on(attribute).empty? end def validation_help_as_list_for(object, attribute) "" and return if object.class.validators_on(attribute).empty? content_tag(:ul, validation_help_for(object, attribute).map { |help_message| content_tag(:li, help_message ) }.to_s.html_safe ) end # validation_help_for(object, attribute) extracts the help messages for # attribute of object.class (in an Array) def validation_help_for(object, attribute) "" and return if object.class.validators_on(attribute).empty? object.class.validators_on(attribute).map do |v| v.help_message if v.respond_to?(:help_message) end.compact end # close link def close_link( object, update_span ) link_to image_tag( 'close.png', :class => "close_icon", :title => 'close' ), send( object.class.to_s.underscore + '_path', object, :update => update_span, :close => true ), :remote => true end # destroy link def link_to_destroy( msg, object, update_span ) if cancan_disabled? || ( can? :destroy, object ) link_to image_tag( 'trash.png', :class => "trash_icon", :title => 'trash' ), send( object.class.to_s.underscore + '_path', object, :update => update_span ), :method => :delete, :remote => true end end # undo link def link_to_undo_destroy(msg, object, update_span ) link_to("undo", revert_version_path(object.versions.scoped.last), :method => :post) end # link_to_inline_edit def link_to_inline_edit(object, attribute, attribute_value='') attribute_value = h(attribute_value) spaces = attribute_value.length > 40 ? 0 : 40 - attribute_value.length attribute_value << " ".html_safe * spaces css_class_id = "#{object.class.to_s.underscore}_#{object.id}_#{attribute}" if cancan_disabled? || ( can? :update, object ) link_to attribute_value, send( 'edit_' + object.class.to_s.underscore + '_path', object, :attribute => attribute.to_s, :form_element => calling_method.sub(/_[a-z]+$/,''), :update => css_class_id ), :remote => true else attribute_value end end # link to new record def link_to_new_record(text, model, path_to_new, update_span, parent_class, parent_id) out = "" out << "