From 6dfdbae3741fd6c6628744fe3885781ff5b51fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 3 Sep 2011 11:02:58 +0200 Subject: [PATCH] Create PasswordInput. --- lib/simple_form/form_builder.rb | 3 ++- lib/simple_form/inputs.rb | 1 + lib/simple_form/inputs/base.rb | 5 +++++ lib/simple_form/inputs/password_input.rb | 20 ++++++++++++++++++++ lib/simple_form/inputs/string_input.rb | 15 ++------------- test/inputs_test.rb | 19 ------------------- 6 files changed, 30 insertions(+), 33 deletions(-) create mode 100644 lib/simple_form/inputs/password_input.rb diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 2a2d2cfe..e91ca5dc 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -6,7 +6,8 @@ module SimpleForm include SimpleForm::Inputs map_type :text, :file, :to => SimpleForm::Inputs::MappingInput - map_type :string, :password, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput + 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 map_type :select, :radio, :check_boxes, :to => SimpleForm::Inputs::CollectionInput map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput diff --git a/lib/simple_form/inputs.rb b/lib/simple_form/inputs.rb index 6bb85759..f1be7b2e 100644 --- a/lib/simple_form/inputs.rb +++ b/lib/simple_form/inputs.rb @@ -8,6 +8,7 @@ module SimpleForm 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 :StringInput, 'simple_form/inputs/string_input' end diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index 740ee18c..9749ac50 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -16,6 +16,11 @@ module SimpleForm include SimpleForm::Components::Wrapper include SimpleForm::Components::Maxlength + # Enables certain components support to the given input. + def self.enable(*args) + args.each { |m| class_eval "def has_#{m}?; true; end" } + end + attr_reader :attribute_name, :column, :input_type, :reflection, :options, :input_html_options diff --git a/lib/simple_form/inputs/password_input.rb b/lib/simple_form/inputs/password_input.rb new file mode 100644 index 00000000..7a396ccc --- /dev/null +++ b/lib/simple_form/inputs/password_input.rb @@ -0,0 +1,20 @@ +module SimpleForm + module Inputs + class PasswordInput < Base + 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 diff --git a/lib/simple_form/inputs/string_input.rb b/lib/simple_form/inputs/string_input.rb index 728242e4..1f4cc16d 100644 --- a/lib/simple_form/inputs/string_input.rb +++ b/lib/simple_form/inputs/string_input.rb @@ -1,18 +1,11 @@ module SimpleForm module Inputs class StringInput < Base - extend MapType - - map_type :string, :email, :search, :tel, :url, :to => :text_field - map_type :password, :to => :password_field - def input input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min input_html_options[:pattern] ||= pattern_validator if validate_pattern? - if password? || SimpleForm.html5 - input_html_options[:type] ||= input_type unless string? - end - @builder.send(input_method, attribute_name, input_html_options) + input_html_options[:type] ||= input_type if SimpleForm.html5 && !string? + @builder.text_field(attribute_name, input_html_options) end def input_html_classes @@ -33,10 +26,6 @@ module SimpleForm input_type == :string end - def password? - input_type == :password - end - def validate_pattern? return unless has_validators? diff --git a/test/inputs_test.rb b/test/inputs_test.rb index 076ad34a..65d26b60 100644 --- a/test/inputs_test.rb +++ b/test/inputs_test.rb @@ -340,25 +340,6 @@ class InputTest < ActionView::TestCase assert_select 'input[pattern="\w+"]' end - test 'input should infer pattern from attributes when it is present as a password' do - with_input_for @other_validating_user, :country, :password - assert_select 'input[pattern="\w+"]' - end - - test 'input should not add pattern from attributes when html5 are turned off' do - swap SimpleForm, :html5 => false do - with_input_for @other_validating_user, :country, :password - assert_no_select 'input[pattern="\w+"]' - end - end - - test 'input should not add pattern from attributes when browser validations are turned off' do - swap SimpleForm, :browser_validations => false do - with_input_for @other_validating_user, :country, :password - assert_no_select 'input[pattern="\w+"]' - end - end - test 'input should have step value of any except for integer attribute' do with_input_for @validating_user, :age, :float assert_select 'input[step="any"]'