Created
September 17, 2014 10:22
-
-
Save hsuh/02621c580ec09a57f147 to your computer and use it in GitHub Desktop.
country_select fix for Rails 3.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CountrySelect < InstanceTag | |
include ::CountrySelect::TagHelper | |
def render(options, html_options) | |
@options = options | |
@html_options = html_options | |
if self.respond_to?(:select_content_tag) | |
select_content_tag(country_option_tags, @options, @html_options) | |
else | |
html_options = @html_options.stringify_keys | |
add_default_name_and_id(html_options) | |
options[:include_blank] ||= true unless options[:prompt] || select_not_required?(html_options) | |
value = options.fetch(:selected) { value(object) } | |
content_tag(:select, add_options(country_option_tags, options, value), html_options) | |
end | |
end | |
def value(object) | |
object.send @method_name if object | |
end | |
def select_not_required?(html_options) | |
!html_options["required"] || html_options["multiple"] || html_options["size"].to_i > 1 | |
end | |
def add_options(option_tags, options, value=nil) | |
if options[:include_blank] | |
option_tags = content_tag_string('option', options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, :value => '') + "\n" + option_tags | |
end | |
if value.blank? && options[:prompt] | |
option_tags = content_tag_string('option', prompt_text(options[:prompt]), :value => '') + "\n" + option_tags | |
end | |
option_tags | |
end | |
def prompt_text(text) | |
text.kind_of?(String) ? text : I18n.translate('helpers.select.prompt', default: 'Please select') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment