From 2b5d4176d9bd2086340c117940fa9ed621d0e92a Mon Sep 17 00:00:00 2001 From: Olivier Lacan Date: Sat, 2 Jul 2016 11:06:49 -0400 Subject: [PATCH 1/4] Simplify check for decimal_or_float? This simplification should make it compatible with Rails 5's new Attributes API without requiring a major refactor of the FormBuilder class. The motivation for this commit originated in #1342. @rafaelfranca mentioned that we should be using the new type_for_attribute interface to determine the precise type of a column instead. At first I used the interface directly like this: def decimal_or_float? if object.respond_to?(:type_for_attribute) type = object.type_for_attribute(column.name.to_s).type type == :float || :decimal else column.number? && column.type != :integer end end I then realized that we're already asking the column object directly for the type, so why not simple check for the two types we care about and implement the method in a much more straightforward *and* backward-compatible way? I ended up with this: def decimal_or_float? column.type == :float || :decimal end I could be missing something obvious but this is green in my Rails 5.0.0 app. The tests mock out the now removed number? ActiveModel method so I had to remove that: method: https://github.com/plataformatec/simple_form/blob/1c389f65594db90a1244d93a00a388b4aea4bcc6/test/support/models.rb#L3-L8 --- .travis.yml | 1 + gemfiles/Gemfile.rails-5-0-stable | 15 +++++++++++++++ lib/simple_form/inputs/base.rb | 2 +- test/support/models.rb | 4 ---- 4 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 gemfiles/Gemfile.rails-5-0-stable diff --git a/.travis.yml b/.travis.yml index 3f5148ec..5df068cf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ gemfile: - gemfiles/Gemfile.rails-4-0-stable - gemfiles/Gemfile.rails-4-1-stable - gemfiles/Gemfile.rails-4-2-stable + - gemfiles/Gemfile.rails-5-0-stable - Gemfile matrix: allow_failures: diff --git a/gemfiles/Gemfile.rails-5-0-stable b/gemfiles/Gemfile.rails-5-0-stable new file mode 100644 index 00000000..94a23da3 --- /dev/null +++ b/gemfiles/Gemfile.rails-5-0-stable @@ -0,0 +1,15 @@ +source 'https://rubygems.org' + +gemspec :path => '..' + +platforms :rbx do + gem 'rubysl', '~> 2.0' + gem 'rubinius-developer_tools' +end + +gem 'country_select', '~> 1.1.1' +gem 'railties', github: 'rails/rails', branch: '5-0-stable' +gem 'activemodel', github: 'rails/rails', branch: '5-0-stable' +gem 'actionpack', github: 'rails/rails', branch: '5-0-stable' +gem 'rake' +gem 'tzinfo' diff --git a/lib/simple_form/inputs/base.rb b/lib/simple_form/inputs/base.rb index 65fd1e91..5aa4b68f 100644 --- a/lib/simple_form/inputs/base.rb +++ b/lib/simple_form/inputs/base.rb @@ -115,7 +115,7 @@ module SimpleForm end def decimal_or_float? - column.number? && column.type != :integer + column.type == :float || column.type == :decimal end def nested_boolean_style? diff --git a/test/support/models.rb b/test/support/models.rb index 8904ae6b..50af4aa0 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -1,10 +1,6 @@ Association = Struct.new(:klass, :name, :macro, :scope, :options) Column = Struct.new(:name, :type, :limit) do - # Returns +true+ if the column is either of type integer, float or decimal. - def number? - type == :integer || type == :float || type == :decimal - end end Relation = Struct.new(:records) do From 50623e8acdc07e9f83a45267eed2d0bbbc2e7f74 Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Tue, 26 Jul 2016 14:47:04 -0300 Subject: [PATCH 2/4] Use the `ActiveModel::Type` API when available --- lib/simple_form/form_builder.rb | 4 +++- test/form_builder/general_test.rb | 6 +++++- test/support/models.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 0147d784..ed1ef4b1 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -549,7 +549,9 @@ module SimpleForm end def find_attribute_column(attribute_name) - if @object.respond_to?(:column_for_attribute) && @object.has_attribute?(attribute_name) + if @object.respond_to?(:type_for_attribute) && @object.has_attribute?(attribute_name) + @object.type_for_attribute(attribute_name) + elsif @object.respond_to?(:column_for_attribute) && @object.has_attribute?(attribute_name) @object.column_for_attribute(attribute_name) end end diff --git a/test/form_builder/general_test.rb b/test/form_builder/general_test.rb index aa5f283e..41da7971 100644 --- a/test/form_builder/general_test.rb +++ b/test/form_builder/general_test.rb @@ -139,7 +139,11 @@ class FormBuilderTest < ActionView::TestCase test 'builder generates uuid fields for uuid columns' do with_form_for @user, :uuid - assert_select 'form input#user_uuid.string.uuid' + if defined? ActiveModel::Type + assert_select 'form input#user_uuid.string.string' + else + assert_select 'form input#user_uuid.string.uuid' + end end test 'builder generates password fields for columns that matches password' do diff --git a/test/support/models.rb b/test/support/models.rb index 50af4aa0..e39dc52c 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -126,6 +126,33 @@ class User Column.new(attribute, column_type, limit) end + begin + require 'active_model/type' + def type_for_attribute(attribute) + column_type, limit = case attribute.to_sym + when :name, :status, :password then [:string, 100] + when :description then [:text, 200] + when :age then :integer + when :credit_limit then [:decimal, 15] + when :active then :boolean + when :born_at then :date + when :delivery_time then :time + when :created_at then :datetime + when :updated_at then :datetime + when :lock_version then :integer + when :home_picture then :string + when :amount then :integer + when :attempts then :integer + when :action then :string + when :credit_card then :string + when :uuid then :string + end + + ActiveModel::Type.lookup(column_type, limit: limit) + end + rescue LoadError + end + def has_attribute?(attribute) case attribute.to_sym when :name, :status, :password, :description, :age, From 1c575cda751e42c0ae8f91d2ebfb4c72c14d57df Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Fri, 12 Aug 2016 16:32:57 -0300 Subject: [PATCH 3/4] Exclude the `5-0-stable` Gemfile with unsupported version of Ruby on Travis. --- .travis.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.travis.yml b/.travis.yml index 5df068cf..8e25c89c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,14 @@ matrix: gemfile: Gemfile - rvm: jruby gemfile: Gemfile + - rvm: 1.9.3 + gemfile: gemfiles/Gemfile.rails-5-0-stable + - rvm: 2.0.0 + gemfile: gemfiles/Gemfile.rails-5-0-stable + - rvm: 2.1.5 + gemfile: gemfiles/Gemfile.rails-5-0-stable + - rvm: jruby + gemfile: gemfiles/Gemfile.rails-5-0-stable notifications: email: false slack: From 23728c765e5cea9d224de2a742b1806842c84f5f Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Fri, 12 Aug 2016 17:02:06 -0300 Subject: [PATCH 4/4] Update `country_select` dependency on `5-0-stable` Gemfile. --- gemfiles/Gemfile.rails-5-0-stable | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gemfiles/Gemfile.rails-5-0-stable b/gemfiles/Gemfile.rails-5-0-stable index 94a23da3..6402f701 100644 --- a/gemfiles/Gemfile.rails-5-0-stable +++ b/gemfiles/Gemfile.rails-5-0-stable @@ -7,7 +7,7 @@ platforms :rbx do gem 'rubinius-developer_tools' end -gem 'country_select', '~> 1.1.1' +gem 'country_select', '~> 2.5.2' gem 'railties', github: 'rails/rails', branch: '5-0-stable' gem 'activemodel', github: 'rails/rails', branch: '5-0-stable' gem 'actionpack', github: 'rails/rails', branch: '5-0-stable'