Permalink
Browse files

First attempt to fix the deeply nested form: change the reject_if to …

…only consider the real attribute fields of an object, not its empty hashes of empty children
  • Loading branch information...
1 parent 53d3a80 commit 4a0e67f7ce6307bf4124509c1eae0b3d6fde0970 @timriley committed Oct 15, 2009
Showing with 18 additions and 3 deletions.
  1. +1 −1 app/models/assignment.rb
  2. +1 −1 app/models/project.rb
  3. +1 −1 app/models/task.rb
  4. +2 −0 config/environment.rb
  5. +13 −0 lib/nested_attributes.rb
View
@@ -2,5 +2,5 @@ class Assignment < ActiveRecord::Base
belongs_to :task
has_many :steps, :dependent => :destroy
validates_presence_of :owner
- accepts_nested_attributes_for :steps, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true
+ accepts_nested_attributes_for :steps, :reject_if => lambda { |a| NestedAttributes.new(a).all_blank? }, :allow_destroy => true
end
View
@@ -1,5 +1,5 @@
class Project < ActiveRecord::Base
has_many :tasks, :dependent => :destroy
validates_presence_of :name
- accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true
+ accepts_nested_attributes_for :tasks, :reject_if => lambda { |a| NestedAttributes.new(a).all_blank? }, :allow_destroy => true
end
View
@@ -2,5 +2,5 @@ class Task < ActiveRecord::Base
belongs_to :project
has_many :assignments, :dependent => :destroy
validates_presence_of :name
- accepts_nested_attributes_for :assignments, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true
+ accepts_nested_attributes_for :assignments, :reject_if => lambda { |a| NestedAttributes.new(a).all_blank? }, :allow_destroy => true
end
View
@@ -52,3 +52,5 @@
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
end
+
+require 'nested_attributes'
View
@@ -0,0 +1,13 @@
+class NestedAttributes
+ def initialize(attrs)
+ @attrs = attrs
+ end
+
+ def all_blank?
+ blank = true
+ @attrs.keys.select { |k| !k.match(/_attributes$/) }.each do |key|
+ blank = false if !@attrs[key].blank?
+ end
+ blank
+ end
+end

0 comments on commit 4a0e67f

Please sign in to comment.