Skip to content

Commit

Permalink
Change: make bowser validations configurable via DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
dalpo committed Jul 15, 2015
1 parent a16fed4 commit b7d8c64
Show file tree
Hide file tree
Showing 37 changed files with 202 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/rails_admin/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/rails_admin/config/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/rails_admin/config/fields/types/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/rails_admin/config/fields/types/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Text < RailsAdmin::Config::Fields::Base

register_instance_option :html_attributes do
{
required: html_required?,
cols: '48',
rows: '3',
}
Expand Down
7 changes: 4 additions & 3 deletions spec/integration/basic/edit/rails_admin_basic_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/boolean_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/bson_object_id_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/carrierwave_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/rails_admin/config/fields/types/ck_editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@
).to eq @custom_prefix
end
end

it_behaves_like 'a generic field type', :text_field, :ck_editor
end
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/code_mirror_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/color_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/date_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/rails_admin/config/fields/types/datetime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/decimal_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/drangonfly_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/enum_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/file_upload_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/float_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/froala_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/hidden_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/inet_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/integer_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/rails_admin/config/fields/types/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/paperclip_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/password_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/serialized_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/rails_admin/config/fields/types/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/text_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/time_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/rails_admin/config/fields/types/uuid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/rails_admin/config/fields/types/wysihtml5_spec.rb
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions spec/shared_examples/shared_examples_for_field_types.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit b7d8c64

Please sign in to comment.