Skip to content

Commit

Permalink
Formtastic reborn
Browse files Browse the repository at this point in the history
  • Loading branch information
David Padilla committed Sep 22, 2011
1 parent 7d1d8f5 commit d84228d
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions integration/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'rails', '~> 3.0.10'
gem 'annotate'
gem 'capybara'
gem 'cucumber-rails'
gem 'formtastic' #, path: '../../formtastic'
gem 'rspec-rails'
gem 'steak'
gem 'database_cleaner'
Expand Down
5 changes: 5 additions & 0 deletions integration/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ GEM
erubis (2.6.6)
abstract (>= 1.0.0)
ffi (1.0.9)
formtastic (1.2.4)
actionpack (>= 2.3.7)
activesupport (>= 2.3.7)
i18n (~> 0.4)
gherkin (2.4.15)
json (>= 1.4.6)
haml (3.1.2)
Expand Down Expand Up @@ -157,6 +161,7 @@ DEPENDENCIES
capybara
cucumber-rails
database_cleaner
formtastic
haml-rails
jquery-rails
launchy
Expand Down
7 changes: 7 additions & 0 deletions integration/app/controllers/formtastics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class FormtasticsController < ApplicationController
autocomplete :brand, :name

def new
@product = Product.new
end
end
9 changes: 9 additions & 0 deletions integration/app/views/formtastics/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%h1 Formtastic Autocomplete

= semantic_form_for @product do |form|
%p
= form.input :name
%p
= form.input :brand_name, :as => :autocomplete, :url => autocomplete_brand_name_formtastics_path


6 changes: 6 additions & 0 deletions integration/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Integration::Application.routes.draw do

get "formtastics/new"

root :to => 'autocomplete#new'
match 'autocomplete/autocomplete_brand_name' => 'autocomplete#autocomplete_brand_name'

Expand Down Expand Up @@ -27,6 +29,10 @@
resources :scoped_autocompletes do
get :autocomplete_brand_name, :on => :collection
end

resources :formtastics do
get :autocomplete_brand_name, :on => :collection
end
end
#== Route Map
# Generated on 25 Apr 2011 09:55
Expand Down
7 changes: 7 additions & 0 deletions integration/features/autocomplete.feature
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ Feature: Autocomplete
And I fill in "Brand name" with "ka"
And I choose "Kappa" in the autocomplete list
Then the "Brand name" field should contain "Kappa"

@javascript
Scenario: Autocomplete with Formtastic
Given I go to the new formtastic page
And I fill in "Brand name" with "al"
And I choose "Alpha" in the autocomplete list
Then the "Brand name" field should contain "Alpha"
12 changes: 12 additions & 0 deletions integration/spec/controllers/formtastics_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec_helper'

describe FormtasticsController do

describe "GET 'new'" do
it "should be successful" do
get 'new'
response.should be_success
end
end

end
5 changes: 5 additions & 0 deletions integration/spec/views/formtastics/new.html.haml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe "formtastics/new.html.haml" do
pending "add some examples to (or delete) #{__FILE__}"
end
2 changes: 1 addition & 1 deletion lib/rails3-jquery-autocomplete/formtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def to_html
#
# Formtastic 1.x
#
class Formtastic::FormBuilder < ActionView::Helpers::FormBuilder
class Formtastic::SemanticFormBuilder < ActionView::Helpers::FormBuilder
include Rails3JQueryAutocomplete::FormtasticPlugin
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/rails3-jquery-autocomplete/formtastic_plugin.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
module Rails3JQueryAutocomplete
module FormtasticPlugin
def autocompleted_input(method, options = {})
def autocomplete_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[:required] = method_required?(method) unless options.key?(:required)
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[: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
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)
Expand All @@ -32,6 +33,8 @@ def autocompleted_input(method, options = {})
return template.content_tag(:li, Formtastic::Util.html_safe(list_item_content), wrapper_html)
end

alias_method :autocompleted_input, :autocomplete_input


protected
def autocompleted_string_input(method, options)
Expand Down

0 comments on commit d84228d

Please sign in to comment.