Skip to content

Commit

Permalink
Break up mapping input and also provide an enable API.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 3, 2011
1 parent 6dfdbae commit 5d6d977
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 80 deletions.
12 changes: 6 additions & 6 deletions lib/simple_form/components/maxlength.rb
Expand Up @@ -2,17 +2,17 @@ module SimpleForm
module Components
module Maxlength
def maxlength
if SimpleForm.html5 && has_maxlength?
input_html_options[:maxlength] ||= maximum_length_from_validation || limit
end

# This components is disabled by default.
nil
end

private

def has_maxlength?
false
def active_maxlength
if SimpleForm.html5
input_html_options[:maxlength] ||= maximum_length_from_validation || limit
end
nil
end

def maximum_length_from_validation
Expand Down
11 changes: 7 additions & 4 deletions lib/simple_form/components/placeholders.rb
Expand Up @@ -2,20 +2,23 @@ module SimpleForm
module Components
module Placeholders
def placeholder
input_html_options[:placeholder] ||= placeholder_text if has_placeholder?
# This components is disabled by default.
nil
end

def has_placeholder?
false
private

def active_placeholder
input_html_options[:placeholder] ||= placeholder_text if placeholder_present?
nil
end

def placeholder_present?
options[:placeholder] != false && placeholder_text.present?
end

def placeholder_text
@placeholder ||= options[:placeholder] || translate(:placeholders)
@placeholder_text ||= options[:placeholder] || translate(:placeholders)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/simple_form/form_builder.rb
Expand Up @@ -5,7 +5,8 @@ class FormBuilder < ActionView::Helpers::FormBuilder
extend MapType
include SimpleForm::Inputs

map_type :text, :file, :to => SimpleForm::Inputs::MappingInput
map_type :text, :to => SimpleForm::Inputs::TextInput
map_type :file, :to => SimpleForm::Inputs::FileInput
map_type :string, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
map_type :password, :to => SimpleForm::Inputs::PasswordInput
map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
Expand Down
3 changes: 2 additions & 1 deletion lib/simple_form/inputs.rb
Expand Up @@ -5,11 +5,12 @@ module Inputs
autoload :BooleanInput, 'simple_form/inputs/boolean_input'
autoload :CollectionInput, 'simple_form/inputs/collection_input'
autoload :DateTimeInput, 'simple_form/inputs/date_time_input'
autoload :FileInput, 'simple_form/inputs/file_input'
autoload :HiddenInput, 'simple_form/inputs/hidden_input'
autoload :MappingInput, 'simple_form/inputs/mapping_input'
autoload :NumericInput, 'simple_form/inputs/numeric_input'
autoload :PasswordInput, 'simple_form/inputs/password_input'
autoload :PriorityInput, 'simple_form/inputs/priority_input'
autoload :TextInput, 'simple_form/inputs/text_input'
autoload :StringInput, 'simple_form/inputs/string_input'
end
end
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/base.rb
Expand Up @@ -18,7 +18,7 @@ class Base

# Enables certain components support to the given input.
def self.enable(*args)
args.each { |m| class_eval "def has_#{m}?; true; end" }
args.each { |m| class_eval "def #{m}; active_#{m}; end" }
end

attr_reader :attribute_name, :column, :input_type, :reflection,
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/boolean_input.rb
Expand Up @@ -9,7 +9,7 @@ def label_input
input + (options[:label] == false ? "" : label)
end

protected
private

# Booleans are not required by default because in most of the cases
# it makes no sense marking them as required. The only exception is
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/collection_input.rb
Expand Up @@ -24,7 +24,7 @@ def input_options
options
end

protected
private

def collection
@collection ||= (options.delete(:collection) || self.class.boolean_collection).to_a
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/date_time_input.rb
Expand Up @@ -5,7 +5,7 @@ def input
@builder.send(:"#{input_type}_select", attribute_name, input_options, input_html_options)
end

private
private

def has_required?
false
Expand Down
9 changes: 9 additions & 0 deletions lib/simple_form/inputs/file_input.rb
@@ -0,0 +1,9 @@
module SimpleForm
module Inputs
class FileInput < Base
def input
@builder.file_field(attribute_name, input_html_options)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/hidden_input.rb
Expand Up @@ -6,7 +6,7 @@ def render
end
alias :input :render

protected
private

def attribute_required?
false
Expand Down
29 changes: 0 additions & 29 deletions lib/simple_form/inputs/mapping_input.rb

This file was deleted.

10 changes: 3 additions & 7 deletions lib/simple_form/inputs/numeric_input.rb
@@ -1,6 +1,8 @@
module SimpleForm
module Inputs
class NumericInput < Base
enable :placeholder

def input
input_html_options[:type] ||= "number" if SimpleForm.html5
input_html_options[:size] ||= nil
Expand All @@ -13,11 +15,7 @@ def input_html_classes
super.unshift("numeric")
end

protected

def has_placeholder?
placeholder_present?
end
private

def infer_attributes_from_validations!
return unless has_validators?
Expand Down Expand Up @@ -53,8 +51,6 @@ def find_numericality_validator
attribute_validators.find { |v| ActiveModel::Validations::NumericalityValidator === v }
end

private

def evaluate_validator_option(option)
return option if option.is_a?(Numeric)
return object.send(option) if option.is_a?(Symbol)
Expand Down
12 changes: 2 additions & 10 deletions lib/simple_form/inputs/password_input.rb
@@ -1,20 +1,12 @@
module SimpleForm
module Inputs
class PasswordInput < Base
enable :maxlength, :placeholder

def input
input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
@builder.password_field(attribute_name, input_html_options)
end

protected

def has_maxlength?
true
end

def has_placeholder?
true
end
end
end
end
11 changes: 2 additions & 9 deletions lib/simple_form/inputs/string_input.rb
@@ -1,6 +1,8 @@
module SimpleForm
module Inputs
class StringInput < Base
enable :maxlength, :placeholder

def input
input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
input_html_options[:pattern] ||= pattern_validator if validate_pattern?
Expand All @@ -14,21 +16,12 @@ def input_html_classes

protected

def has_maxlength?
true
end

def has_placeholder?
placeholder_present?
end

def string?
input_type == :string
end

def validate_pattern?
return unless has_validators?

SimpleForm.html5 && SimpleForm.browser_validations && find_pattern_validator.present?
end

Expand Down
11 changes: 11 additions & 0 deletions lib/simple_form/inputs/text_input.rb
@@ -0,0 +1,11 @@
module SimpleForm
module Inputs
class TextInput < Base
enable :maxlength, :placeholder

def input
@builder.text_area(attribute_name, input_html_options)
end
end
end
end
8 changes: 0 additions & 8 deletions test/inputs_test.rb
Expand Up @@ -449,14 +449,6 @@ def with_input_for(object, attribute_name, type, options={})
assert_no_select 'input[placeholder]'
end

test 'mapping input should generate an error if type is not found' do
with_concat_form_for(@user) do |f|
assert_raise(RuntimeError, "Could not find method for nil") do
SimpleForm::Inputs::MappingInput.new(f, "unknown", nil, nil, {}).input
end
end
end

# HiddenInput
test 'input should generate a hidden field' do
with_input_for @user, :name, :hidden
Expand Down

0 comments on commit 5d6d977

Please sign in to comment.