module Formtastic::Actions::Base

Attributes

builder[RW]
method[RW]
object[RW]
object_name[RW]
options[RW]
template[RW]

Public Class Methods

new(builder, template, object, object_name, method, options) click to toggle source
# File lib/formtastic/actions/base.rb, line 8
def initialize(builder, template, object, object_name, method, options)
  @builder = builder
  @template = template
  @object = object
  @object_name = object_name
  @method = method
  @options = options.dup
  
  check_supported_methods!
end

Public Instance Methods

accesskey() click to toggle source
# File lib/formtastic/actions/base.rb, line 102
def accesskey
  # TODO could be cleaner and separated, remember that nil is an allowed value for all of these
  return options[:accesskey] if options.key?(:accesskey)
  return options[:button_html][:accesskey] if options.key?(:button_html) && options[:button_html].key?(:accesskey)
  # TODO might be different for cancel, etc?
  return builder.default_commit_button_accesskey
end
button_html() click to toggle source
# File lib/formtastic/actions/base.rb, line 86
def button_html
  default_button_html.merge(button_html_from_options || {}).merge(extra_button_html_options)
end
button_html_from_options() click to toggle source
# File lib/formtastic/actions/base.rb, line 90
def button_html_from_options
  options[:button_html]
end
default_button_html() click to toggle source
# File lib/formtastic/actions/base.rb, line 98
def default_button_html
  { :accesskey => accesskey }
end
default_wrapper_classes() click to toggle source
# File lib/formtastic/actions/base.rb, line 49
def default_wrapper_classes
  ["action", "#{options[:as]}_action"]
end
default_wrapper_html_options() click to toggle source
# File lib/formtastic/actions/base.rb, line 38
def default_wrapper_html_options
  {
    :class => wrapper_class,
    :id => wrapper_id
  }
end
default_wrapper_id() click to toggle source
# File lib/formtastic/actions/base.rb, line 71
def default_wrapper_id
  "#{object_name}_#{method}_action"
end
extra_button_html_options() click to toggle source
# File lib/formtastic/actions/base.rb, line 94
def extra_button_html_options
  {}
end
supported_methods() click to toggle source
# File lib/formtastic/actions/base.rb, line 75
def supported_methods
  raise NotImplementedError
end
text() click to toggle source
# File lib/formtastic/actions/base.rb, line 79
def text
  text = options[:label]
  text = (localized_string(i18n_key, text, :action, :model => sanitized_object_name) ||
         Formtastic::I18n.t(i18n_key, :model => sanitized_object_name)) unless text.is_a?(::String)
  text
end
to_html() click to toggle source
# File lib/formtastic/actions/base.rb, line 19
def to_html
  raise NotImplementedError
end
wrapper(&block) click to toggle source
# File lib/formtastic/actions/base.rb, line 23
def wrapper(&block)
  template.content_tag(:li, 
    template.capture(&block), 
    wrapper_html_options
  )
end
wrapper_class() click to toggle source
# File lib/formtastic/actions/base.rb, line 45
def wrapper_class
  (default_wrapper_classes << wrapper_classes_from_options).join(" ")
end
wrapper_classes_from_options() click to toggle source
# File lib/formtastic/actions/base.rb, line 53
def wrapper_classes_from_options
  classes = wrapper_html_options_from_options[:class] || []
  classes = classes.split(" ") if classes.is_a? String
  classes
end
wrapper_html_options() click to toggle source
# File lib/formtastic/actions/base.rb, line 30
def wrapper_html_options
  wrapper_html_options_from_options.merge(default_wrapper_html_options)
end
wrapper_html_options_from_options() click to toggle source
# File lib/formtastic/actions/base.rb, line 34
def wrapper_html_options_from_options
  options[:wrapper_html] || {}
end
wrapper_id() click to toggle source
# File lib/formtastic/actions/base.rb, line 63
def wrapper_id
  wrapper_id_from_options || default_wrapper_id
end
wrapper_id_from_options() click to toggle source
# File lib/formtastic/actions/base.rb, line 67
def wrapper_id_from_options
  wrapper_html_options_from_options[:id]
end

Protected Instance Methods

check_supported_methods!() click to toggle source
# File lib/formtastic/actions/base.rb, line 113
def check_supported_methods!
  raise Formtastic::UnsupportedMethodForAction unless supported_methods.include?(method)
end
i18n_key() click to toggle source
# File lib/formtastic/actions/base.rb, line 117
def i18n_key
  return submit_i18n_key if method == :submit
  method
end
new_or_persisted_object?() click to toggle source
# File lib/formtastic/actions/base.rb, line 130
def new_or_persisted_object?
  object && (object.respond_to?(:persisted?) || object.respond_to?(:new_record?))
end
sanitized_object_name() click to toggle source
# File lib/formtastic/actions/base.rb, line 134
def sanitized_object_name
  if new_or_persisted_object?
    # Deal with some complications with ActiveRecord::Base.human_name and two name models (eg UserPost)
    # ActiveRecord::Base.human_name falls back to ActiveRecord::Base.name.humanize ("Userpost")
    # if there's no i18n, which is pretty crappy.  In this circumstance we want to detect this
    # fall back (human_name == name.humanize) and do our own thing name.underscore.humanize ("User Post")
    if object.class.model_name.respond_to?(:human)
      sanitized_object_name = object.class.model_name.human
    else
      object_human_name = @object.class.human_name                # default is UserPost => "Userpost", but i18n may do better ("User post")
      crappy_human_name = @object.class.name.humanize             # UserPost => "Userpost"
      decent_human_name = @object.class.name.underscore.humanize  # UserPost => "User post"
      sanitized_object_name = (object_human_name == crappy_human_name) ? decent_human_name : object_human_name
    end
  else
    sanitized_object_name = object_name.to_s.send(builder.label_str_method)
  end
  sanitized_object_name
end
submit_i18n_key() click to toggle source
# File lib/formtastic/actions/base.rb, line 122
def submit_i18n_key
  if new_or_persisted_object?
    key = @object.persisted? ? :update : :create
  else
    key = :submit
  end
end