Class: ActiveRecord::DeleteRestrictionError
- Inherits:
-
ActiveRecordError
- Object
- ActiveRecordError
- ActiveRecord::DeleteRestrictionError
- Defined in:
- lib/dependent_protect/delete_restriction_error.rb
Overview
This error is raised when trying to destroy a parent instance in N:1 or 1:1 associations (has_many, has_one) when there is at least 1 child associated instance. ex: if @project.tasks.size > 0, DeleteRestrictionError will be raised when trying to destroy @project
Instance Method Summary collapse
- #basic_message ⇒ Object
- #detailed_message ⇒ Object
-
#initialize(reflection, record) ⇒ DeleteRestrictionError
constructor
:nodoc:.
Constructor Details
#initialize(reflection, record) ⇒ DeleteRestrictionError
:nodoc:
6 7 8 9 10 |
# File 'lib/dependent_protect/delete_restriction_error.rb', line 6 def initialize(reflection, record) @reflection = reflection @record = record super() end |
Instance Method Details
#basic_message ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/dependent_protect/delete_restriction_error.rb', line 12 def single = @reflection.class_name.to_s.underscore.gsub('_', ' ') if [:has_many, :has_and_belongs_to_many].include?(@reflection.macro) count = @record.send(@reflection.name).count type = count == 1 ? single : single.pluralize exist = (count == 1 ? 'exists' : 'exist') "Cannot delete record because #{count} dependent #{type} #{exist}" else "Cannot delete record because dependent #{single} exists" end end |
#detailed_message ⇒ Object
24 25 26 27 28 29 |
# File 'lib/dependent_protect/delete_restriction_error.rb', line 24 def count = @record.send(@reflection.name).count examples = @record.send(@reflection.name).all(:limit => 5).map{|o| "#{o.id}: #{o.to_s}"} examples[4] = "...and #{count - 4} more" if count > 5 + "\n\n\nThese include:\n#{examples.join("\n")}" end |