Skip to content

Commit

Permalink
Fix Formtastic 1.x compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
David Padilla committed Sep 21, 2011
1 parent f6a26b6 commit 7d1d8f5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/rails3-jquery-autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
require 'rails3-jquery-autocomplete/autocomplete'

module Rails3JQueryAutocomplete
autoload :Orm, 'rails3-jquery-autocomplete/orm'
autoload :Orm , 'rails3-jquery-autocomplete/orm'
autoload :FormtasticPlugin , 'rails3-jquery-autocomplete/formtastic_plugin'

unless ::Rails.version < "3.1"
require 'rails3-jquery-autocomplete/rails/engine'
Expand Down
33 changes: 25 additions & 8 deletions lib/rails3-jquery-autocomplete/formtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@
# Load the formtastic plugin if using Formtastic
begin
require 'formtastic'
module Formtastic
module Inputs
class AutocompleteInput
include Base
include Base::Stringish

def to_html
input_wrapping do
label_html <<
if Formtastic.constants.include?(:VERSION)

#
# Formtastic 2.0
#


module Formtastic
module Inputs
class AutocompleteInput
include Base
include Base::Stringish

def to_html
input_wrapping do
label_html <<
builder.autocomplete_field(method, options.delete(:url), input_html_options)
end
end
end
end
end
else

#
# Formtastic 1.x
#
class Formtastic::FormBuilder < ActionView::Helpers::FormBuilder
include Rails3JQueryAutocomplete::FormtasticPlugin
end
end
rescue LoadError
end
41 changes: 41 additions & 0 deletions lib/rails3-jquery-autocomplete/formtastic_plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Rails3JQueryAutocomplete
module FormtasticPlugin
def autocompleted_input(method, options = {})
if options.key?(:selected) || options.key?(:checked) || options.key?(:default)
::ActiveSupport::Deprecation.warn(
"The :selected, :checked (and :default) options are deprecated in Formtastic and will be removed from 1.0. " <<
"Please set default values in your models (using an after_initialize callback) or in your controller set-up. " <<
"See http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html for more information.", caller)
end

options[:as] ||= "autocompleted_string"

html_class = [ options[:as], (options[:required] ? :required : :optional) ]
html_class << 'error' if @object && @object.respond_to?(:errors) && !@object.errors[method.to_sym].blank?

wrapper_html = options.delete(:wrapper_html) || {}
# wrapper_html[:id] ||= generate_html_id(method)
wrapper_html[:class] = (html_class << wrapper_html[:class]).flatten.compact.join(' ')

if options[:input_html] && options[:input_html][:id]
options[:label_html] ||= {}
options[:label_html][:for] ||= options[:input_html][:id]
end

# input_parts = self.class.inline_order.dup
# input_parts = input_parts - [:errors, :hints] if options[:as] == :hidden

list_item_content = input_parts.map do |type|
send(:"inline_#{type}_for", method, options)
end.compact.join("\n")

return template.content_tag(:li, Formtastic::Util.html_safe(list_item_content), wrapper_html)
end


protected
def autocompleted_string_input(method, options)
self.label(method, options_for_label(options)) << autocomplete_field(method, options.delete(:url), options)
end
end
end

0 comments on commit 7d1d8f5

Please sign in to comment.