Skip to content

Commit

Permalink
Merge pull request #1276 from brewster1134/master
Browse files Browse the repository at this point in the history
max_length check for string data type
  • Loading branch information
mshibuya committed Aug 10, 2012
2 parents 662ec14 + cb7002a commit 536cd22
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
10 changes: 6 additions & 4 deletions lib/rails_admin/config/fields/types/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class String < RailsAdmin::Config::Fields::Base
else
max_length = [length, valid_length[:maximum] || nil].compact.min
min_length = [0, valid_length[:minimum] || nil].compact.max
if min_length == 0
text += "#{I18n.translate("admin.form.char_length_up_to").capitalize} #{max_length}."
else
text += "#{I18n.translate("admin.form.char_length_of").capitalize} #{min_length}-#{max_length}."
if max_length
if min_length == 0
text += "#{I18n.translate("admin.form.char_length_up_to").capitalize} #{max_length}."
else
text += "#{I18n.translate("admin.form.char_length_of").capitalize} #{min_length}-#{max_length}."
end
end
end
text
Expand Down
70 changes: 41 additions & 29 deletions spec/integration/config/edit/rails_admin_config_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class HelpTest < Tableless
column :name, 'string(50)'
column :division, :string
end
RailsAdmin.config.included_models = [HelpTest]
RailsAdmin.config.included_models = [HelpTest, Team]
end

after(:each) do
Expand All @@ -163,6 +163,41 @@ class HelpTest < Tableless
HelpTest.reset_callbacks(:validate)
end

context "using mongoid", :skip_active_record => true do
it "should use the db column size for the maximum length" do
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length up to 255.")
end

it "should return nil for the maximum length" do
visit new_path(:model_name => "team")
find("#team_custom_field_field .help-block").should_not have_content("Length")
end
end

context "using active_record", :skip_mongoid => true do
it "should use the db column size for the maximum length" do
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length up to 50.")
end

it "should use the :minimum setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :minimum => 1
end
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length of 1-50.")
end

it "should use the minimum of db column size or :maximum setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :maximum => 51
end
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length up to 50.")
end
end

it "should show help section if present" do
RailsAdmin.config HelpTest do
edit do
Expand Down Expand Up @@ -215,35 +250,12 @@ class HelpTest < Tableless
find("#help_test_name_field .help-block").should have_content("Length of 3.")
end

describe "using ORM column size", :skip_mongoid => true do
it "should use the db column size for the maximum length" do
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length up to 50.")
end

it "should use the :minimum setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :minimum => 1
end
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length of 1-50.")
end

it "should use the :maximum setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :maximum => 49
end
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length up to 49.")
end

it "should use the minimum of db column size or :maximum setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :maximum => 51
end
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length up to 50.")
it "should use the :maximum setting from the validation" do
HelpTest.class_eval do
validates_length_of :name, :maximum => 49
end
visit new_path(:model_name => "help_test")
find("#help_test_name_field .help-block").should have_content("Length up to 49.")
end

it "should use the :minimum and :maximum from the validation" do
Expand Down

0 comments on commit 536cd22

Please sign in to comment.