Permalink
Browse files

raise ArgumentError when accepts_nested_attributes_for is missing

  • Loading branch information...
1 parent 55be760 commit 9c54ac35f0a2cf61747f4ec7c32f4d23e7e0dbfb @lest lest committed Oct 28, 2012
Showing with 23 additions and 12 deletions.
  1. +1 −1 README.md
  2. +10 −6 lib/nested_form/builder_mixin.rb
  3. +2 −0 spec/dummy/app/models/project.rb
  4. +10 −5 spec/nested_form/builder_spec.rb
View
@@ -39,7 +39,7 @@ You can then include the generated JavaScript in your layout.
## Usage
-Imagine you have a `Project` model that `has_many :tasks`. To be able to use this gem, you'll need to add `accepts_nested_attributes_for :tasks` to your Project model. If you wish to allow the nested objects to be destroyed, then add the `:allow_destroy => true` option to that declaration. If you don't have the `accepts_nested_attributes_for :tasks` you'll get a Missing Block Error. See the [accepts_nested_attributes_for documentation](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for) for details on all available options.
+Imagine you have a `Project` model that `has_many :tasks`. To be able to use this gem, you'll need to add `accepts_nested_attributes_for :tasks` to your Project model. If you wish to allow the nested objects to be destroyed, then add the `:allow_destroy => true` option to that declaration. See the [accepts_nested_attributes_for documentation](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for) for details on all available options.
This will create a `tasks_attributes=` method, so you may need to add it to the `attr_accessible` array (`attr_accessible :tasks_attributes`).
@@ -20,17 +20,21 @@ def link_to_add(*args, &block)
options = args.extract_options!.symbolize_keys
association = args.pop
- unless (reflection = object.class.reflect_on_association(association))
- raise ArgumentError, "Failed to find #{object.class.name} association by name \"#{association}\""
+ unless object.respond_to?("#{association}_attributes=")
+ raise ArgumentError, "Invalid association. Make sure that accepts_nested_attributes_for is used for #{association.inspect} association."
+ end
+
+ model_object = options.delete(:model_object) do
+ reflection = object.class.reflect_on_association(association)
+ reflection.klass.new
end
- model_object = options.delete(:model_object) || reflection.klass.new
options[:class] = [options[:class], "add_nested_fields"].compact.join(" ")
options["data-association"] = association
options["data-blueprint-id"] = fields_blueprint_id = fields_blueprint_id_for(association)
args << (options.delete(:href) || "javascript:void(0)")
args << options
-
+
@fields ||= {}
@template.after_nested_form(fields_blueprint_id) do
blueprint = {:id => fields_blueprint_id, :style => 'display: none'}
@@ -56,12 +60,12 @@ def link_to_add(*args, &block)
def link_to_remove(*args, &block)
options = args.extract_options!.symbolize_keys
options[:class] = [options[:class], "remove_nested_fields"].compact.join(" ")
-
+
# Extracting "milestones" from "...[milestones_attributes][...]"
md = object_name.to_s.match /(\w+)_attributes\]\[[\w\d]+\]$/
association = md && md[1]
options["data-association"] = association
-
+
args << (options.delete(:href) || "javascript:void(0)")
args << options
hidden_field(:_destroy) << @template.link_to(*args, &block)
@@ -3,4 +3,6 @@ class Project < ActiveRecord::Base
has_many :assignments, :class_name => 'ProjectTask'
accepts_nested_attributes_for :tasks
accepts_nested_attributes_for :assignments
+
+ has_many :not_nested_tasks, :class_name => 'Task'
end
@@ -24,11 +24,16 @@
subject.link_to_add(:tasks) { "Add" }.should == '<a href="javascript:void(0)" class="add_nested_fields" data-association="tasks" data-blueprint-id="tasks_fields_blueprint">Add</a>'
end
- context 'when missing association is provided' do
- it 'raises Argument error' do
- expect{ subject.link_to_add('Add', :bugs) }.to raise_error(ArgumentError,
- 'Failed to find Project association by name "bugs"')
- end
+ it 'raises ArgumentError when missing association is provided' do
+ expect {
+ subject.link_to_add('Add', :bugs)
+ }.to raise_error(ArgumentError)
+ end
+
+ it 'raises ArgumentError when accepts_nested_attributes_for is missing' do
+ expect {
+ subject.link_to_add('Add', :not_nested_tasks)
+ }.to raise_error(ArgumentError)
end
end

0 comments on commit 9c54ac3

Please sign in to comment.