Permalink
Browse files

better separation of form builder support

  • Loading branch information...
kristianmandrup committed May 10, 2012
1 parent 5c155aa commit ff2a1db2fc523efe803c638241ad8d75c61224ee
@@ -0,0 +1,64 @@
+module ActionView
+ module Helpers
+
+ module FormOptionsHelper
+
+ # Return select and option tags for the given object and method, using +localized_language_options_for_select+
+ # to generate the list of option tags. Uses <b>language code</b>, not name as option +value+.
+ # Language codes listed as an array of symbols in +priority_languages+ argument will be listed first
+ # TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
+ def localized_language_select(object, method, priority_languages = nil, options = {}, html_options = {})
+ InstanceTag.new(object, method, self, options.delete(:object)).
+ to_localized_language_select_tag(priority_languages, options, html_options)
+ end
+ alias_method :language_select, :localized_language_select
+
+ # Return "named" select and option tags according to given arguments.
+ # Use +selected_value+ for setting initial value
+ # It behaves likes older object-binded brother +localized_language_select+ otherwise
+ # TODO : Implement pseudo-named args with a hash, not the "somebody said PHP?" multiple args sillines
+ def localized_language_select_tag(name, selected_value = nil, priority_languages = nil, html_options = {})
+ content_tag :select,
+ localized_language_options_for_select(selected_value, priority_languages).html_safe,
+ { "name" => name, "id" => name }.update(html_options.stringify_keys)
+ end
+ alias_method :language_select_tag, :localized_language_select_tag
+
+ # Returns a string of option tags for languages according to locale. Supply the language code in lower-case ('en', 'de')
+ # as +selected+ to have it marked as the selected option tag.
+ # Language codes listed as an array of symbols in +priority_languages+ argument will be listed first
+ def localized_language_options_for_select(selected = nil, priority_languages = nil, options = {})
+ language_options = ""
+ if priority_languages
+ language_options += options_for_select(LocalizedLanguageSelect::priority_languages_array(priority_languages), selected)
+ language_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
+ end
+ return language_options + options_for_select(LocalizedLanguageSelect::localized_languages_array(options), selected)
+ end
+ alias_method :language_options_for_select, :localized_language_options_for_select
+
+ end
+
+ class InstanceTag
+ def to_localized_language_select_tag(priority_languages, options, html_options)
+ html_options = html_options.stringify_keys
+ add_default_name_and_id(html_options)
+ value = value(object)
+ content_tag("select",
+ add_options(
+ localized_language_options_for_select(value, priority_languages, options).html_safe,
+ options, value
+ ), html_options
+ )
+ end
+ end
+
+ class FormBuilder
+ def localized_language_select(method, priority_languages = nil, options = {}, html_options = {})
+ @template.localized_language_select(@object_name, method, priority_languages, options.merge(:object => @object), html_options)
+ end
+ alias_method :language_select, :localized_language_select
+ end
+
+ end
+end
@@ -0,0 +1,19 @@
+module Formtastic
+ module Inputs
+ class LanguageInput
+ include Base
+
+ def to_html
+ raise "To use the :language input, please install a language_select plugin, like this one: https://github.com/jeanmartin/localized_language_select/blob/master/lib/localized_language_select.rb" unless builder.respond_to?(:language_select)
+ input_wrapping do
+ label_html <<
+ builder.language_select(method, priority_languages, input_options, input_html_options)
+ end
+ end
+
+ def priority_languages
+ options[:priority_languages] || []#builder.priority_languages
+ end
+ end
+ end
+end
@@ -0,0 +1,11 @@
+ class LanguageInput < Formtastic::Inputs::SelectInput
+ def collection
+ result = Array.new
+ priority_languages = input_options.delete(:priority_languages) || nil
+ if priority_languages
+ result += LocalizedLanguageSelect::priority_languages_array(priority_languages)
+ result << ["----------", ""]
+ end
+ result += LocalizedLanguageSelect::localized_languages_array(options)
+ end
+ end
@@ -0,0 +1,4 @@
+raise "Formtastic not defined" unless defined?(Formtastic)
+
+require "localized_language_select/formtastic/v#{Formtastic.VERSION[0]}"
+
@@ -0,0 +1,21 @@
+# encoding: utf-8
+
+# TODO !!! ("stolen" from Formtastic, but can't seem to make it work... waiting for engines to do this magic OOTB !)
+
+# module LocalizedLanguageSelect
+# module I18n
+# DEFAULT_SCOPE = [:languages].freeze
+# DEFAULT_VALUES = YAML.load_file(File.expand_path("../../../locale/en.yml", __FILE__))["en"]["languages"].freeze
+#
+# class << self
+# def translate(*args)
+# key = args.shift.to_sym
+# options = args.extract_options!
+# options.reverse_merge!(:default => DEFAULT_VALUES[key])
+# options[:scope] = [DEFAULT_SCOPE, options[:scope]].flatten.compact
+# ::I18n.translate(key, *(args << options))
+# end
+# alias :t :translate
+# end
+# end
+# end

0 comments on commit ff2a1db

Please sign in to comment.