diff --git a/lib/rails_admin/config.rb b/lib/rails_admin/config.rb index 6694655ec3..3099e4deac 100644 --- a/lib/rails_admin/config.rb +++ b/lib/rails_admin/config.rb @@ -50,6 +50,9 @@ class << self # hide blank fields in show view if true attr_accessor :compact_show_view + # Enable HTML5 browser validation + attr_accessor :browser_validations + # Set the max width of columns in list view before a new set is created attr_accessor :total_columns_width @@ -254,6 +257,7 @@ def models # @see RailsAdmin::Config.registry def reset @compact_show_view = true + @browser_validations = true @yell_for_non_accessible_fields = true @authenticate = nil @authorize = nil diff --git a/lib/rails_admin/config/fields/base.rb b/lib/rails_admin/config/fields/base.rb index a692d6acdd..5104df6575 100644 --- a/lib/rails_admin/config/fields/base.rb +++ b/lib/rails_admin/config/fields/base.rb @@ -123,8 +123,14 @@ def virtual? (@help ||= {})[::I18n.locale] ||= generic_field_help end + def html_required? + RailsAdmin::Config.browser_validations && required + end + register_instance_option :html_attributes do - {} + { + required: html_required?, + } end register_instance_option :default_value do diff --git a/lib/rails_admin/config/fields/types/string.rb b/lib/rails_admin/config/fields/types/string.rb index 31aa38d008..d7d3988ab2 100644 --- a/lib/rails_admin/config/fields/types/string.rb +++ b/lib/rails_admin/config/fields/types/string.rb @@ -7,10 +7,15 @@ module Types class String < RailsAdmin::Config::Fields::Base RailsAdmin::Config::Fields::Types.register(self) + def input_size + [50, length.to_i].reject(&:zero?).min + end + register_instance_option :html_attributes do { + required: html_required?, maxlength: length, - size: [50, length.to_i].reject(&:zero?).min, + size: input_size, } end diff --git a/lib/rails_admin/config/fields/types/text.rb b/lib/rails_admin/config/fields/types/text.rb index 9c1abb87cc..84d9872746 100644 --- a/lib/rails_admin/config/fields/types/text.rb +++ b/lib/rails_admin/config/fields/types/text.rb @@ -28,6 +28,7 @@ class Text < RailsAdmin::Config::Fields::Base register_instance_option :html_attributes do { + required: html_required?, cols: '48', rows: '3', } diff --git a/spec/integration/basic/edit/rails_admin_basic_edit_spec.rb b/spec/integration/basic/edit/rails_admin_basic_edit_spec.rb index 7395f5b18b..c0da01785d 100644 --- a/spec/integration/basic/edit/rails_admin_basic_edit_spec.rb +++ b/spec/integration/basic/edit/rails_admin_basic_edit_spec.rb @@ -5,6 +5,7 @@ describe 'edit' do before do + RailsAdmin::Config.browser_validations = true @player = FactoryGirl.create :player visit edit_path(model_name: 'player', id: @player.id) end @@ -25,12 +26,12 @@ end it 'checks required fields to have required attribute set' do - expect(find_field('player_name')[:required].nil?).to eq(false) - expect(find_field('player_number')[:required].nil?).to eq(false) + expect(find_field('player_name')[:required]).to be_present + expect(find_field('player_number')[:required]).to be_present end it 'checks optional fields to not have required attribute set' do - expect(find_field('player_position')[:required].nil?).to eq(true) + expect(find_field('player_position')[:required]).to be_blank end end diff --git a/spec/rails_admin/config/fields/types/belongs_to_association_spec.rb b/spec/rails_admin/config/fields/types/belongs_to_association_spec.rb new file mode 100644 index 0000000000..c9bdd79646 --- /dev/null +++ b/spec/rails_admin/config/fields/types/belongs_to_association_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::BelongsToAssociation do + it_behaves_like 'a generic field type', :integer_field, :belongs_to_association +end diff --git a/spec/rails_admin/config/fields/types/boolean_spec.rb b/spec/rails_admin/config/fields/types/boolean_spec.rb new file mode 100644 index 0000000000..e3b4839d88 --- /dev/null +++ b/spec/rails_admin/config/fields/types/boolean_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Boolean do + it_behaves_like 'a generic field type', :boolean_field, :boolean +end diff --git a/spec/rails_admin/config/fields/types/bson_object_id_spec.rb b/spec/rails_admin/config/fields/types/bson_object_id_spec.rb new file mode 100644 index 0000000000..24ed287ce5 --- /dev/null +++ b/spec/rails_admin/config/fields/types/bson_object_id_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::BsonObjectId do + it_behaves_like 'a generic field type', :string_field, :bson_object_id +end diff --git a/spec/rails_admin/config/fields/types/carrierwave_spec.rb b/spec/rails_admin/config/fields/types/carrierwave_spec.rb new file mode 100644 index 0000000000..08eb4d4d4e --- /dev/null +++ b/spec/rails_admin/config/fields/types/carrierwave_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Carrierwave do + it_behaves_like 'a generic field type', :string_field, :carrierwave +end diff --git a/spec/rails_admin/config/fields/types/ck_editor_spec.rb b/spec/rails_admin/config/fields/types/ck_editor_spec.rb index 043e261be3..2b0130a1b3 100644 --- a/spec/rails_admin/config/fields/types/ck_editor_spec.rb +++ b/spec/rails_admin/config/fields/types/ck_editor_spec.rb @@ -21,4 +21,6 @@ ).to eq @custom_prefix end end + + it_behaves_like 'a generic field type', :text_field, :ck_editor end diff --git a/spec/rails_admin/config/fields/types/code_mirror_spec.rb b/spec/rails_admin/config/fields/types/code_mirror_spec.rb new file mode 100644 index 0000000000..a62f0445f4 --- /dev/null +++ b/spec/rails_admin/config/fields/types/code_mirror_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::CodeMirror do + it_behaves_like 'a generic field type', :text_field, :code_mirror +end diff --git a/spec/rails_admin/config/fields/types/color_spec.rb b/spec/rails_admin/config/fields/types/color_spec.rb new file mode 100644 index 0000000000..7c341f8ad1 --- /dev/null +++ b/spec/rails_admin/config/fields/types/color_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Color do + it_behaves_like 'a generic field type', :string_field, :color +end diff --git a/spec/rails_admin/config/fields/types/date_spec.rb b/spec/rails_admin/config/fields/types/date_spec.rb new file mode 100644 index 0000000000..f40e34ea18 --- /dev/null +++ b/spec/rails_admin/config/fields/types/date_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Date do + it_behaves_like 'a generic field type', :date_field, :date +end diff --git a/spec/rails_admin/config/fields/types/datetime_spec.rb b/spec/rails_admin/config/fields/types/datetime_spec.rb index 9cd5920605..da97141a19 100644 --- a/spec/rails_admin/config/fields/types/datetime_spec.rb +++ b/spec/rails_admin/config/fields/types/datetime_spec.rb @@ -15,4 +15,6 @@ expect(RailsAdmin.config(FieldTest).fields.detect { |f| f.name == :datetime_field }.with(object: FieldTest.new).formatted_date_value).to eq 'January 01, 2012' end end + + it_behaves_like 'a generic field type', :datetime_field, :datetime end diff --git a/spec/rails_admin/config/fields/types/decimal_spec.rb b/spec/rails_admin/config/fields/types/decimal_spec.rb new file mode 100644 index 0000000000..d69a91272c --- /dev/null +++ b/spec/rails_admin/config/fields/types/decimal_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Decimal do + it_behaves_like 'a generic field type', :decimal_field, :decimal +end diff --git a/spec/rails_admin/config/fields/types/drangonfly_spec.rb b/spec/rails_admin/config/fields/types/drangonfly_spec.rb new file mode 100644 index 0000000000..f431d5786e --- /dev/null +++ b/spec/rails_admin/config/fields/types/drangonfly_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Dragonfly do + it_behaves_like 'a generic field type', :string_field, :dragonfly +end diff --git a/spec/rails_admin/config/fields/types/enum_spec.rb b/spec/rails_admin/config/fields/types/enum_spec.rb new file mode 100644 index 0000000000..d597a2df30 --- /dev/null +++ b/spec/rails_admin/config/fields/types/enum_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Enum do + it_behaves_like 'a generic field type', :string_field, :enum +end diff --git a/spec/rails_admin/config/fields/types/file_upload_spec.rb b/spec/rails_admin/config/fields/types/file_upload_spec.rb new file mode 100644 index 0000000000..dbbc2ae368 --- /dev/null +++ b/spec/rails_admin/config/fields/types/file_upload_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::FileUpload do + it_behaves_like 'a generic field type', :string_field, :file_upload +end diff --git a/spec/rails_admin/config/fields/types/float_spec.rb b/spec/rails_admin/config/fields/types/float_spec.rb new file mode 100644 index 0000000000..3fcb47d89f --- /dev/null +++ b/spec/rails_admin/config/fields/types/float_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Float do + it_behaves_like 'a generic field type', :float_field, :float +end diff --git a/spec/rails_admin/config/fields/types/froala_spec.rb b/spec/rails_admin/config/fields/types/froala_spec.rb new file mode 100644 index 0000000000..eba209ce27 --- /dev/null +++ b/spec/rails_admin/config/fields/types/froala_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Froala do + it_behaves_like 'a generic field type', :text_field, :froala +end diff --git a/spec/rails_admin/config/fields/types/has_and_belongs_to_many_association_spec.rb b/spec/rails_admin/config/fields/types/has_and_belongs_to_many_association_spec.rb new file mode 100644 index 0000000000..831b3755db --- /dev/null +++ b/spec/rails_admin/config/fields/types/has_and_belongs_to_many_association_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::HasAndBelongsToManyAssociation do + it_behaves_like 'a generic field type', :integer_field, :has_and_belongs_to_many_association +end diff --git a/spec/rails_admin/config/fields/types/has_many_association_spec.rb b/spec/rails_admin/config/fields/types/has_many_association_spec.rb new file mode 100644 index 0000000000..e8fb5a9067 --- /dev/null +++ b/spec/rails_admin/config/fields/types/has_many_association_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::HasManyAssociation do + it_behaves_like 'a generic field type', :integer_field, :has_many_association +end diff --git a/spec/rails_admin/config/fields/types/has_one_association_spec.rb b/spec/rails_admin/config/fields/types/has_one_association_spec.rb new file mode 100644 index 0000000000..e3e05204f6 --- /dev/null +++ b/spec/rails_admin/config/fields/types/has_one_association_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::HasOneAssociation do + it_behaves_like 'a generic field type', :integer_field, :has_one_association +end diff --git a/spec/rails_admin/config/fields/types/hidden_spec.rb b/spec/rails_admin/config/fields/types/hidden_spec.rb new file mode 100644 index 0000000000..cba94f43a4 --- /dev/null +++ b/spec/rails_admin/config/fields/types/hidden_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Hidden do + it_behaves_like 'a generic field type', :integer_field, :hidden +end diff --git a/spec/rails_admin/config/fields/types/inet_spec.rb b/spec/rails_admin/config/fields/types/inet_spec.rb new file mode 100644 index 0000000000..6d2fe48563 --- /dev/null +++ b/spec/rails_admin/config/fields/types/inet_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Inet do + it_behaves_like 'a generic field type', :string_field, :inet +end diff --git a/spec/rails_admin/config/fields/types/integer_spec.rb b/spec/rails_admin/config/fields/types/integer_spec.rb new file mode 100644 index 0000000000..b13b70778f --- /dev/null +++ b/spec/rails_admin/config/fields/types/integer_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Integer do + it_behaves_like 'a generic field type', :integer_field, :integer +end diff --git a/spec/rails_admin/config/fields/types/json_spec.rb b/spec/rails_admin/config/fields/types/json_spec.rb index 68fd6e9ded..9b517ae349 100644 --- a/spec/rails_admin/config/fields/types/json_spec.rb +++ b/spec/rails_admin/config/fields/types/json_spec.rb @@ -35,4 +35,6 @@ expect(field.class).to eq RailsAdmin::Config::Fields::Types::Json end end + + it_behaves_like 'a generic field type', :text, :json end diff --git a/spec/rails_admin/config/fields/types/paperclip_spec.rb b/spec/rails_admin/config/fields/types/paperclip_spec.rb new file mode 100644 index 0000000000..8975cacac0 --- /dev/null +++ b/spec/rails_admin/config/fields/types/paperclip_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Paperclip do + it_behaves_like 'a generic field type', :string_field, :paperclip +end diff --git a/spec/rails_admin/config/fields/types/password_spec.rb b/spec/rails_admin/config/fields/types/password_spec.rb new file mode 100644 index 0000000000..da278f3f0c --- /dev/null +++ b/spec/rails_admin/config/fields/types/password_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Password do + it_behaves_like 'a generic field type', :string_field, :password +end diff --git a/spec/rails_admin/config/fields/types/serialized_spec.rb b/spec/rails_admin/config/fields/types/serialized_spec.rb new file mode 100644 index 0000000000..a13104ce09 --- /dev/null +++ b/spec/rails_admin/config/fields/types/serialized_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Serialized do + it_behaves_like 'a generic field type', :text_field, :serialized +end diff --git a/spec/rails_admin/config/fields/types/string_spec.rb b/spec/rails_admin/config/fields/types/string_spec.rb index 647d1f9e2c..c4bb72d94c 100644 --- a/spec/rails_admin/config/fields/types/string_spec.rb +++ b/spec/rails_admin/config/fields/types/string_spec.rb @@ -22,4 +22,6 @@ expect(string_field.html_attributes[:size]).to_not be_zero end end + + it_behaves_like 'a generic field type', :string_field, :string end diff --git a/spec/rails_admin/config/fields/types/text_spec.rb b/spec/rails_admin/config/fields/types/text_spec.rb new file mode 100644 index 0000000000..d281570cf8 --- /dev/null +++ b/spec/rails_admin/config/fields/types/text_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Text do + it_behaves_like 'a generic field type', :text_field, :text +end diff --git a/spec/rails_admin/config/fields/types/time_spec.rb b/spec/rails_admin/config/fields/types/time_spec.rb new file mode 100644 index 0000000000..1fcfce2862 --- /dev/null +++ b/spec/rails_admin/config/fields/types/time_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Time do + it_behaves_like 'a generic field type', :time_field, :time +end diff --git a/spec/rails_admin/config/fields/types/uuid_spec.rb b/spec/rails_admin/config/fields/types/uuid_spec.rb index dab49dbc30..7c6b98ec02 100644 --- a/spec/rails_admin/config/fields/types/uuid_spec.rb +++ b/spec/rails_admin/config/fields/types/uuid_spec.rb @@ -24,4 +24,6 @@ it 'handles uuid string' do expect(@field.value).to eq uuid end + + it_behaves_like 'a generic field type', :string_field, :uuid end diff --git a/spec/rails_admin/config/fields/types/wysihtml5_spec.rb b/spec/rails_admin/config/fields/types/wysihtml5_spec.rb new file mode 100644 index 0000000000..075c534051 --- /dev/null +++ b/spec/rails_admin/config/fields/types/wysihtml5_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe RailsAdmin::Config::Fields::Types::Wysihtml5 do + it_behaves_like 'a generic field type', :text_field, :wysihtml5 +end diff --git a/spec/shared_examples/shared_examples_for_field_types.rb b/spec/shared_examples/shared_examples_for_field_types.rb new file mode 100644 index 0000000000..84c437229e --- /dev/null +++ b/spec/shared_examples/shared_examples_for_field_types.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +RSpec.shared_examples 'a generic field type' do |column_name, field_type| + context 'with browser_validations' do + before do + RailsAdmin.config FieldTest do + field column_name, field_type do + required true + end + end + end + + let :rails_admin_field do + RailsAdmin.config('FieldTest').fields.detect do |f| + f.name == column_name + end.with(object: FieldTest.new) + end + + context 'enabled' do + before :each do + RailsAdmin::Config.browser_validations = true + end + + describe '#html_attributes' do + it 'should contain a required attribute with the string "required" as value' do + expect(rails_admin_field.html_attributes[:required]).to be_truthy + end + end + end + + context 'disabled' do + before :each do + RailsAdmin::Config.browser_validations = false + end + + describe '#html_attributes' do + it 'should contain a required attribute with the string "required" as value' do + expect(rails_admin_field.html_attributes[:required]).to be_falsey + end + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7128c9788c..7e8559a54b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,6 +23,8 @@ require 'database_cleaner' require "orm/#{CI_ORM}" +Dir[File.expand_path('../shared_examples/**/*.rb', __FILE__)].each { |f| require f } + ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true ActionMailer::Base.default_url_options[:host] = 'example.com'