Skip to content

Commit

Permalink
fix #1310 [fields marked as required incorrectly based on validators]
Browse files Browse the repository at this point in the history
  • Loading branch information
bbenezech committed Sep 18, 2012
1 parent 3684bd7 commit 9b78f34
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/rails_admin/config/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ def virtual?
#
# @see RailsAdmin::AbstractModel.properties
register_instance_option :required? do
@required ||= !!([name] + children_fields).uniq.find do |column_name|
context = (bindings && bindings[:object].try(:persisted?) ? :update : :create)
(@required ||= {})[context] ||= !!([name] + children_fields).uniq.find do |column_name|
!!abstract_model.model.validators_on(column_name).find do |v|
v.kind == :presence && !v.options[:allow_nil] ||
v.kind == :numericality && !v.options[:allow_nil]
!v.options[:allow_nil] and
[:presence, :numericality].include?(v.kind) and
(v.options[:on] == context or v.options[:on].blank?)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy_app/app/active_record/ball.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Ball < ActiveRecord::Base
attr_accessible :color

validates_presence_of :color
validates_presence_of :color, :on => :create

def to_param
color.present? ? color.downcase.gsub(" ", "-") : id
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/config/fields/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

describe RailsAdmin::Config::Fields::Base do

describe "#required" do

it "should read the :on => :create/:update validate option" do
RailsAdmin.config Ball do
field 'color'
end

RailsAdmin.config('Ball').fields.first.with(:object => Ball.new).should be_required
RailsAdmin.config('Ball').fields.first.with(:object => FactoryGirl.create(:ball)).should_not be_required
end
end

describe "#name" do
it 'should be normalized to Symbol' do
RailsAdmin.config Team do
Expand Down

0 comments on commit 9b78f34

Please sign in to comment.