Skip to content
Browse files

Added check boxes to the nested models which will make it raise an ex…

…ception. This is so people can easily play with transactions.
  • Loading branch information...
1 parent d142946 commit ce66710f0e4dc3e071b12e1d0248a41668ab2e6a @alloy committed Jan 22, 2009
Showing with 37 additions and 0 deletions.
  1. +9 −0 app/models/author.rb
  2. +9 −0 app/models/tag.rb
  3. +9 −0 app/models/task.rb
  4. +10 −0 app/views/projects/_form.html.erb
View
9 app/models/author.rb
@@ -1,3 +1,12 @@
class Author < ActiveRecord::Base
has_many :projects
+
+ attr_accessor :raise_exception
+
+ after_save :raise_exception_if_needed
+ def raise_exception_if_needed
+ if @raise_exception.to_i == 1
+ raise 'Oh noes!'
+ end
+ end
end
View
9 app/models/tag.rb
@@ -1,3 +1,12 @@
class Tag < ActiveRecord::Base
validates_presence_of :name, :message => "can't just be blank"
+
+ attr_accessor :raise_exception
+
+ after_save :raise_exception_if_needed
+ def raise_exception_if_needed
+ if @raise_exception.to_i == 1
+ raise 'Oh noes!'
+ end
+ end
end
View
9 app/models/task.rb
@@ -1,4 +1,13 @@
class Task < ActiveRecord::Base
belongs_to :project
validates_presence_of :name
+
+ attr_accessor :raise_exception
+
+ after_save :raise_exception_if_needed
+ def raise_exception_if_needed
+ if @raise_exception.to_i == 1
+ raise 'Oh noes!'
+ end
+ end
end
View
10 app/views/projects/_form.html.erb
@@ -13,6 +13,9 @@
<% project_form.fields_for :author do |author_form| %>
<%= author_form.label :name, "Author name:" %>
<%= author_form.text_field :name %>
+
+ <%= author_form.label :raise_exception, "Raise exception:" %>
+ <%= author_form.check_box :raise_exception %>
<% end %>
</p>
@@ -22,6 +25,10 @@
<p>
<%= task_form.label :name, "Task:" %>
<%= task_form.text_field :name %>
+
+ <%= task_form.label :raise_exception, "Raise exception:" %>
+ <%= task_form.check_box :raise_exception %>
+
<!-- Uses JS to hide this `task' and set a hidden_field with the name
`_delete' to "1" which causes the record to be deleted. -->
<%= remove_link_unless_new_record(task_form) %>
@@ -40,6 +47,9 @@
<%= tag_form.label :name, "Tag:" %>
<%= tag_form.text_field :name %>
+ <%= tag_form.label :raise_exception, "Raise exception:" %>
+ <%= tag_form.check_box :raise_exception %>
+
<% unless tag_form.object.new_record? %>
<!-- For existing records we add a checkbox to be able to delete records. -->
<%= tag_form.label :_delete, "Remove:" %>

0 comments on commit ce66710

Please sign in to comment.
Something went wrong with that request. Please try again.