Permalink
Browse files

Merge branch 'master' of git://github.com/plataformatec/simple_form

  • Loading branch information...
Aditya Sanghi
Aditya Sanghi committed Dec 20, 2010
2 parents eab4692 + 965d449 commit e8b1a23b76a063d6bfe7ce9667af33b451812e80
View
@@ -1,3 +1,10 @@
+* bug fix
+ * Only add the "required" HTML 5 attribute for valid inputs, disable in selects (not allowed)
+ * Fix error when using hints without an attribute (by github.com/butsjoh and github.com/rafaelfranca)
+ * Fix messy html output for hint, error and label components (by github.com/butsjoh and github.com/rafaelfranca)
+
+== 1.3.0
+
* enhancements
* Allow collection input to accept a collection of symbols
* Add default css class to button
View
13 Gemfile
@@ -1,10 +1,13 @@
source "http://rubygems.org"
gem "rails", "~> 3.0.0"
-gem "mocha"
-if RUBY_VERSION < "1.9"
- gem "ruby-debug"
-else
- gem "test-unit"
+group :test do
+ gem "mocha", :require => false
+
+ if RUBY_VERSION < "1.9"
+ gem "ruby-debug", :require => false
+ else
+ gem "test-unit", :require => false
+ end
end
View
@@ -28,20 +28,20 @@ GEM
activemodel (= 3.0.3)
activesupport (= 3.0.3)
activesupport (3.0.3)
- arel (2.0.4)
+ arel (2.0.6)
builder (2.1.2)
columnize (0.3.2)
erubis (2.6.6)
abstract (>= 1.0.0)
- i18n (0.4.2)
+ i18n (0.5.0)
linecache (0.43)
- mail (2.2.10)
+ mail (2.2.12)
activesupport (>= 2.3.6)
- i18n (~> 0.4.1)
+ i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
- mocha (0.9.9)
+ mocha (0.9.10)
rake
polyglot (0.3.1)
rack (1.2.1)
View
@@ -22,23 +22,4 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
-end
-
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |s|
- s.name = "simple_form"
- s.version = SimpleForm::VERSION.dup
- s.summary = "Forms made easy!"
- s.email = "contact@plataformatec.com.br"
- s.homepage = "http://github.com/plataformatec/simple_form"
- s.description = "Forms made easy!"
- s.authors = ['José Valim', 'Carlos Antônio']
- s.files = FileList["[A-Z]*(.rdoc)", "{generators,lib}/**/*", "init.rb"]
- s.test_files.exclude("test/support/country_select")
- end
-
- Jeweler::GemcutterTasks.new
-rescue LoadError
- puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
-end
+end
View
@@ -1,2 +0,0 @@
-* Add unobstrusive javascript validations support
-* Add mask support (same as hint, but with JS)
@@ -6,8 +6,8 @@
prohibited this <%= singular_name %> from being saved:
%ul
- - @<%= singular_name %>.errors.full_messages.each do |msg|
- %li= msg
+ - @<%= singular_name %>.errors.full_messages.each do |msg|
+ %li= msg
.inputs
<%- attributes.each do |attribute| -%>
@@ -52,7 +52,7 @@ def required_label_text #:nodoc:
attribute_required? ? self.class.translate_required_html.dup : ''
end
- # First check human attribute name and then labels.
+ # First check labels translation and then human attribute name.
def label_translation #:nodoc:
translate(:labels) || translate(:label, '', :helpers) || if object.class.respond_to?(:human_attribute_name)
object.class.human_attribute_name(reflection_or_attribute_name.to_s)
@@ -62,4 +62,4 @@ def label_translation #:nodoc:
end
end
end
-end
+end
@@ -1,6 +1,6 @@
module SimpleForm
class FormBuilder < ActionView::Helpers::FormBuilder
- attr_reader :template, :object_name, :object, :reflection, :options
+ attr_reader :template, :object_name, :object
extend MapType
include SimpleForm::Inputs
@@ -79,8 +79,8 @@ class FormBuilder < ActionView::Helpers::FormBuilder
# given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
#
def input(attribute_name, options={}, &block)
- column = find_attribute_column(attribute_name)
- input_type = default_input_type(attribute_name, column, options)
+ column = find_attribute_column(attribute_name)
+ input_type = default_input_type(attribute_name, column, options)
if block_given?
SimpleForm::Inputs::BlockInput.new(self, attribute_name, column, input_type, options, &block).render
@@ -123,28 +123,28 @@ def association(association, options={}, &block)
raise ArgumentError, "Association cannot be used in forms not associated with an object" unless @object
+ reflection = find_association_reflection(association)
+ raise "Association #{association.inspect} not found" unless reflection
+
options[:as] ||= :select
- @reflection = find_association_reflection(association)
- raise "Association #{association.inspect} not found" unless @reflection
+ options[:collection] ||= reflection.klass.all(reflection.options.slice(:conditions, :order))
- case @reflection.macro
+ attribute = case reflection.macro
when :belongs_to
- attribute = @reflection.options[:foreign_key] || :"#{@reflection.name}_id"
+ reflection.options[:foreign_key] || :"#{reflection.name}_id"
when :has_one
raise ":has_one association are not supported by f.association"
else
- attribute = :"#{@reflection.name.to_s.singularize}_ids"
-
if options[:as] == :select
html_options = options[:input_html] ||= {}
html_options[:size] ||= 5
html_options[:multiple] = true unless html_options.key?(:multiple)
end
- end
- options[:collection] ||= @reflection.klass.all(@reflection.options.slice(:conditions, :order))
+ :"#{reflection.name.to_s.singularize}_ids"
+ end
- input(attribute, options).tap { @reflection = nil }
+ input(attribute, options.merge(:reflection => reflection))
end
# Creates a button:
@@ -175,7 +175,7 @@ def button(type, *args, &block)
# f.error :name, :id => "cool_error"
#
def error(attribute_name, options={})
- options[:error_html] = options
+ options[:error_html] = options.dup
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).error
@@ -192,7 +192,7 @@ def error(attribute_name, options={})
# f.hint "Don't forget to accept this"
#
def hint(attribute_name, options={})
- options[:hint_html] = options
+ options[:hint_html] = options.dup
if attribute_name.is_a?(String)
options[:hint] = attribute_name
attribute_name, column, input_type = nil, nil, nil
@@ -219,8 +219,8 @@ def hint(attribute_name, options={})
def label(attribute_name, *args)
return super if args.first.is_a?(String)
options = args.extract_options!
+ options[:label_html] = options.dup
options[:label] = options.delete(:label)
- options[:label_html] = options
options[:required] = options.delete(:required)
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
@@ -15,18 +15,20 @@ class Base
include SimpleForm::Components::Placeholders
include SimpleForm::Components::Wrapper
- attr_reader :attribute_name, :column, :input_type, :options, :input_html_options
+ attr_reader :attribute_name, :column, :input_type, :reflection,
+ :options, :input_html_options
- delegate :template, :object, :object_name, :reflection, :to => :@builder
+ delegate :template, :object, :object_name, :to => :@builder
def initialize(builder, attribute_name, column, input_type, options = {})
@builder = builder
@attribute_name = attribute_name
@column = column
@input_type = input_type
+ @reflection = options.delete(:reflection)
@options = options
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
- o[:required] = true if attribute_required?
+ o[:required] = true if has_required?
o[:disabled] = true if disabled?
end
end
@@ -69,8 +71,13 @@ def attribute_required?
end
end
+ # Whether this input is valid for HTML 5 required attribute.
+ def has_required?
+ attribute_required?
+ end
+
def has_validators?
- object.class.respond_to?(:validators_on)
+ attribute_name && object.class.respond_to?(:validators_on)
end
def attribute_validators
@@ -13,10 +13,10 @@ def self.boolean_collection
end
def input
- collection = (options[:collection] || self.class.boolean_collection).to_a
- detect_collection_methods(collection, options)
- @builder.send(:"collection_#{input_type}", attribute_name, collection, options[:value_method],
- options[:label_method], input_options, input_html_options)
+ label_method, value_method = detect_collection_methods
+
+ @builder.send(:"collection_#{input_type}", attribute_name, collection,
+ value_method, label_method, input_options, input_html_options)
end
def input_options
@@ -27,6 +27,15 @@ def input_options
protected
+ def collection
+ @collection ||= (options.delete(:collection) || self.class.boolean_collection).to_a
+ end
+
+ # Select components does not allow the required html tag.
+ def has_required?
+ super && input_type != :select
+ end
+
# Check if :include_blank must be included by default.
def skip_include_blank?
(options.keys & [:prompt, :include_blank, :default, :selected]).any? ||
@@ -38,20 +47,41 @@ def skip_include_blank?
# on default label and value methods that can be configured through
# SimpleForm.collection_label_methods and
# SimpleForm.collection_value_methods.
- def detect_collection_methods(collection, options)
- sample = collection.first || collection.last
-
- case sample
- when Array
- label, value = :first, :last
- when Integer
- label, value = :to_s, :to_i
- when String, Symbol, NilClass
- label, value = :to_s, :to_s
+ def detect_collection_methods
+ label, value = options.delete(:label_method), options.delete(:value_method)
+
+ unless label && value
+ common_method_for = detect_common_display_methods
+ label ||= common_method_for[:label]
+ value ||= common_method_for[:value]
end
- options[:label_method] ||= label || SimpleForm.collection_label_methods.find { |m| sample.respond_to?(m) }
- options[:value_method] ||= value || SimpleForm.collection_value_methods.find { |m| sample.respond_to?(m) }
+ [label, value]
+ end
+
+ def detect_common_display_methods
+ collection_classes = detect_collection_classes
+
+ if collection_classes.include?(Array)
+ { :label => :first, :value => :last }
+ elsif collection_includes_basic_objects?(collection_classes)
+ { :label => :to_s, :value => :to_s }
+ else
+ sample = collection.first || collection.last
+
+ { :label => SimpleForm.collection_label_methods.find { |m| sample.respond_to?(m) },
+ :value => SimpleForm.collection_value_methods.find { |m| sample.respond_to?(m) } }
+ end
+ end
+
+ def detect_collection_classes
+ collection.map { |e| e.class }.uniq
+ end
+
+ def collection_includes_basic_objects?(collection_classes)
+ (collection_classes & [
+ String, Integer, Fixnum, Bignum, Float, NilClass, Symbol, TrueClass, FalseClass
+ ]).any?
end
end
end
@@ -7,6 +7,10 @@ def input
private
+ def has_required?
+ false
+ end
+
def label_target
case input_type
when :date, :datetime
@@ -12,9 +12,13 @@ def input_priority
protected
+ def has_required?
+ false
+ end
+
def skip_include_blank?
super || input_priority.present?
end
end
end
-end
+end
@@ -1,3 +1,3 @@
module SimpleForm
- VERSION = "1.2.2".freeze
+ VERSION = "1.3.0".freeze
end
Oops, something went wrong.

0 comments on commit e8b1a23

Please sign in to comment.