Module: Formtastic::Actions::Base

Includes:
LocalizedString
Included in:
ButtonAction, InputAction, LinkAction
Defined in:
lib/formtastic/actions/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LocalizedString

#model_name

Instance Attribute Details

#builderObject

Returns the value of attribute builder.



6
7
8
# File 'lib/formtastic/actions/base.rb', line 6

def builder
  @builder
end

#methodObject

Returns the value of attribute method.



6
7
8
# File 'lib/formtastic/actions/base.rb', line 6

def method
  @method
end

#objectObject

Returns the value of attribute object.



6
7
8
# File 'lib/formtastic/actions/base.rb', line 6

def object
  @object
end

#object_nameObject

Returns the value of attribute object_name.



6
7
8
# File 'lib/formtastic/actions/base.rb', line 6

def object_name
  @object_name
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/formtastic/actions/base.rb', line 6

def options
  @options
end

#templateObject

Returns the value of attribute template.



6
7
8
# File 'lib/formtastic/actions/base.rb', line 6

def template
  @template
end

Instance Method Details

#accesskeyObject



102
103
104
105
106
107
108
# 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_htmlObject



86
87
88
# 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_optionsObject



90
91
92
# File 'lib/formtastic/actions/base.rb', line 90

def button_html_from_options
  options[:button_html]
end

#default_button_htmlObject



98
99
100
# File 'lib/formtastic/actions/base.rb', line 98

def default_button_html
  { :accesskey => accesskey }
end

#default_wrapper_classesObject



49
50
51
# File 'lib/formtastic/actions/base.rb', line 49

def default_wrapper_classes
  ["action", "#{options[:as]}_action"]
end

#default_wrapper_html_optionsObject



38
39
40
41
42
43
# File 'lib/formtastic/actions/base.rb', line 38

def default_wrapper_html_options
  {
    :class => wrapper_class,
    :id => wrapper_id
  }
end

#default_wrapper_idObject



71
72
73
# File 'lib/formtastic/actions/base.rb', line 71

def default_wrapper_id
  "#{object_name}_#{method}_action"
end

#extra_button_html_optionsObject



94
95
96
# File 'lib/formtastic/actions/base.rb', line 94

def extra_button_html_options
  {}
end

#initialize(builder, template, object, object_name, method, options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# 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

#supported_methodsObject

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/formtastic/actions/base.rb', line 75

def supported_methods
  raise NotImplementedError
end

#textObject



79
80
81
82
83
84
# 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_htmlObject

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/formtastic/actions/base.rb', line 19

def to_html
  raise NotImplementedError
end

#wrapper(&block) ⇒ Object



23
24
25
26
27
28
# File 'lib/formtastic/actions/base.rb', line 23

def wrapper(&block)
  template.(:li, 
    template.capture(&block), 
    wrapper_html_options
  )
end

#wrapper_classObject



45
46
47
# File 'lib/formtastic/actions/base.rb', line 45

def wrapper_class
  (default_wrapper_classes << wrapper_classes_from_options).join(" ")
end

#wrapper_classes_from_optionsObject



53
54
55
56
57
# 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_optionsObject



30
31
32
# 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_optionsObject



34
35
36
# File 'lib/formtastic/actions/base.rb', line 34

def wrapper_html_options_from_options
  options[:wrapper_html] || {}
end

#wrapper_idObject



63
64
65
# File 'lib/formtastic/actions/base.rb', line 63

def wrapper_id
  wrapper_id_from_options || default_wrapper_id
end

#wrapper_id_from_optionsObject



67
68
69
# File 'lib/formtastic/actions/base.rb', line 67

def wrapper_id_from_options
  wrapper_html_options_from_options[:id]
end