Skip to content
This repository has been archived by the owner on Nov 19, 2017. It is now read-only.

Commit

Permalink
Updating formtastic
Browse files Browse the repository at this point in the history
  • Loading branch information
jgdavey committed Oct 25, 2009
1 parent be0f497 commit fe93d8a
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 209 deletions.
174 changes: 91 additions & 83 deletions vendor/plugins/formtastic/README.textile

Large diffs are not rendered by default.

103 changes: 60 additions & 43 deletions vendor/plugins/formtastic/lib/formtastic.rb
Expand Up @@ -283,30 +283,55 @@ def buttons(*args, &block)
# #
# The value of the button text can be overridden: # The value of the button text can be overridden:
# #
# <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" /> # <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" class="{create|update|submit}" />
# <%= form.commit_button :label => "Go" %> => <input name="commit" type="submit" value="Go" class="{create|update|submit}" />
# #
# And you can pass html atributes down to the input, with or without the button text: # And you can pass html atributes down to the input, with or without the button text:
# #
# <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" /> # <%= form.commit_button "Go" %> => <input name="commit" type="submit" value="Go" class="{create|update|submit}" />
# <%= form.commit_button :class => "pretty" %> => <input name="commit" type="submit" value="Save Post" class="pretty" /> # <%= form.commit_button :class => "pretty" %> => <input name="commit" type="submit" value="Save Post" class="pretty {create|update|submit}" />
# #
# You can use the actual <button/> html tag instead the default <input type="submit"/>: # You can use the actual <button/> html tag instead the default <input type="submit"/>:
# #
# <%= form.commit_button "Save", :use_button_html => true %> => <button name="commit" type="submit">Go</button> # <%= form.commit_button "Save", :use_button_html => true %> #=> <button name="commit" type="submit">Go</button>
# #
def commit_button(*args) def commit_button(*args)
value = args.first.is_a?(String) ? args.shift : save_or_create_button_text options = args.extract_options!
options = args.shift || {} text = options.delete(:label) || args.shift

if @object
key = @object.new_record? ? :create : :update
object_name = @object.class.human_name

if key == :update
# Note: Fallback on :save-key (deprecated), :update makes more sense in the REST-world.
fallback_text = ::I18n.t(:save, :model => object_name, :default => "Save {{model}}", :scope => [:formtastic])
::ActiveSupport::Deprecation.warn "Formtastic I18n: Key 'formtastic.save' is now deprecated in favor 'formtastic.update'."
end
else
key = :submit
object_name = @object_name.to_s.send(@@label_str_method)
end
fallback_text ||= "#{key.to_s.humanize} {{model}}"

text = (self.localized_string(key, text, :action, :model => object_name) ||
::I18n.t(key, :model => object_name, :default => fallback_text, :scope => [:formtastic])) unless text.is_a?(::String)

button_html = options.delete(:button_html) || {} button_html = options.delete(:button_html) || {}
button_or_input_tag = begin button_html.merge!(:class => [button_html[:class], key].compact.join(' '))
if options.delete(:use_button_tag) element_class = ['commit', options.delete(:class)].compact.join(' ') # TODO: Add class reflecting on form action.

button_content = begin
as = options.delete(:as)
if as && as.to_sym == :button || options.delete(:use_button_tag)
button_html.merge!(:type => 'submit', :name => 'commit') button_html.merge!(:type => 'submit', :name => 'commit')
template.content_tag(:button, value, button_html) template.content_tag(:button, text, button_html)
else else
self.submit(value, button_html) self.submit(text, button_html)
end end
end end
template.content_tag(:li, button_or_input_tag, :class => "commit")
template.content_tag(:li, button_content, :class => "commit")
end end


# A thin wrapper around #fields_for to set :builder => Formtastic::SemanticFormBuilder # A thin wrapper around #fields_for to set :builder => Formtastic::SemanticFormBuilder
Expand Down Expand Up @@ -364,10 +389,12 @@ def label(method, options_or_text=nil, options=nil)
text = options_or_text text = options_or_text
options ||= {} options ||= {}
end end

text = localized_string(method, text, :label) || humanized_attribute_name(method)
text = localized_attribute_string(method, text, :label) || humanized_attribute_name(method)
text += required_or_optional_string(options.delete(:required)) text += required_or_optional_string(options.delete(:required))


# special case for boolean (checkbox) labels, which have a nested input
text = (options.delete(:label_prefix_for_nested_input) || "") + text

input_name = options.delete(:input_name) || method input_name = options.delete(:input_name) || method
if options.delete(:as_span) if options.delete(:as_span)
options[:class] ||= 'label' options[:class] ||= 'label'
Expand Down Expand Up @@ -433,22 +460,6 @@ def set_options(options)
:as, :hint, :input_html, :label_html, :value_as_class) :as, :hint, :input_html, :label_html, :value_as_class)
end end


# Create a default button text. If the form is working with a object, it
# defaults to "Create {{model}}" or "Save {{model}}" depending if we are working
# with a new_record or not.
#
# When not working with models, it defaults to "Submit object".
#
def save_or_create_button_text(prefix='Submit') #:nodoc:
if @object
prefix = @object.new_record? ? 'Create' : 'Save'
object_name = @object.class.human_name
else
object_name = @object_name.to_s.send(@@label_str_method)
end
I18n.t(prefix.downcase, :model => object_name, :default => "#{prefix} {{model}}", :scope => [:formtastic])
end

# Determins if the attribute (eg :title) should be considered required or not. # Determins if the attribute (eg :title) should be considered required or not.
# #
# * if the :required option was provided in the options hash, the true/false value will be # * if the :required option was provided in the options hash, the true/false value will be
Expand Down Expand Up @@ -937,8 +948,12 @@ def boolean_input(method, options)


input = self.check_box(method, set_options(options).merge(html_options), input = self.check_box(method, set_options(options).merge(html_options),
options.delete(:checked_value) || '1', options.delete(:unchecked_value) || '0') options.delete(:checked_value) || '1', options.delete(:unchecked_value) || '0')

options = options_for_label(options)
self.label(method, input << self.label(method, options_for_label(options)), options_for_label(options))
# the label() method will insert this nested input into the label at the last minute
options[:label_prefix_for_nested_input] = input

self.label(method, options)
end end


# Generates an input for the given method using the type supplied with :as. # Generates an input for the given method using the type supplied with :as.
Expand All @@ -961,7 +976,7 @@ def inline_input_for(method, options)
# Generates hints for the given method using the text supplied in :hint. # Generates hints for the given method using the text supplied in :hint.
# #
def inline_hints_for(method, options) #:nodoc: def inline_hints_for(method, options) #:nodoc:
options[:hint] = localized_attribute_string(method, options[:hint], :hint) options[:hint] = localized_string(method, options[:hint], :hint)
return if options[:hint].blank? return if options[:hint].blank?
template.content_tag(:p, options[:hint], :class => 'inline-hints') template.content_tag(:p, options[:hint], :class => 'inline-hints')
end end
Expand Down Expand Up @@ -1013,7 +1028,7 @@ def required_or_optional_string(required) #:nodoc:
# #
def field_set_and_list_wrapping(html_options, contents='', &block) #:nodoc: def field_set_and_list_wrapping(html_options, contents='', &block) #:nodoc:
html_options[:name] ||= html_options.delete(:title) html_options[:name] ||= html_options.delete(:title)
html_options[:name] = localized_attribute_string(html_options[:name], html_options[:name], :title) if html_options[:name].is_a?(Symbol) html_options[:name] = localized_string(html_options[:name], html_options[:name], :title) if html_options[:name].is_a?(Symbol)


legend = html_options.delete(:name).to_s legend = html_options.delete(:name).to_s
legend %= parent_child_index(html_options[:parent]) if html_options[:parent] legend %= parent_child_index(html_options[:parent]) if html_options[:parent]
Expand Down Expand Up @@ -1244,16 +1259,18 @@ def humanized_attribute_name(method)
# #
# NOTE: Generic, but only used for form input labels/hints. # NOTE: Generic, but only used for form input labels/hints.
# #
def localized_attribute_string(attr_name, attr_value, i18n_key) def localized_string(key, value, type, options = {})
if attr_value.is_a?(String) key = value if value.is_a?(::Symbol)
attr_value
if value.is_a?(::String)
value
else else
use_i18n = attr_value.nil? ? @@i18n_lookups_by_default : (attr_value != false) use_i18n = value.nil? ? @@i18n_lookups_by_default : (value != false)

if use_i18n if use_i18n
model_name = @object.class.name.underscore model_name = (@object ? @object.class.name : @object_name.to_s.send(@@label_str_method)).underscore
action_name = template.params[:action].to_s rescue '' action_name = template.params[:action].to_s rescue ''
attribute_name = attr_name.to_s attribute_name = key.to_s


defaults = I18N_SCOPES.collect do |i18n_scope| defaults = I18N_SCOPES.collect do |i18n_scope|
i18n_path = i18n_scope.dup i18n_path = i18n_scope.dup
Expand All @@ -1265,8 +1282,8 @@ def localized_attribute_string(attr_name, attr_value, i18n_key)
end end
defaults << '' defaults << ''


i18n_value = ::I18n.t(defaults.shift, :default => defaults, i18n_value = ::I18n.t(defaults.shift, options.merge(:default => defaults,
:scope => "formtastic.#{i18n_key.to_s.pluralize}") :scope => :"formtastic.#{type.to_s.pluralize}"))
i18n_value.blank? ? nil : i18n_value i18n_value.blank? ? nil : i18n_value
end end
end end
Expand Down

0 comments on commit fe93d8a

Please sign in to comment.