Module: ActiveRecord::RailsDevsForDataIntegrity::ClassMethods
- Defined in:
- lib/rails_devs_for_data_integrity.rb
Instance Method Summary collapse
-
#enable_all_database_violation_checks ⇒ Object
enable uniqueness and foreign key checks for all ActiveRecord instances.
-
#handle_foreign_key_violation(name, options = {}) ⇒ Object
Handle a MySQL foreign key violation by placing an error message on violating foreign key field.
-
#handle_foreign_key_violations(options = {}) ⇒ Object
Handle a MySQL foreign key violations by placing an error message on violating foreign key field.
-
#handle_unique_key_violation(*args) ⇒ Object
Handle a MySQL unique key violation by placing an error message on
nameCurrently only one duplicate key per table is supported because no parsing support to determine the violating key.
Instance Method Details
#enable_all_database_violation_checks ⇒ Object
enable uniqueness and foreign key checks for all ActiveRecord instances
114 115 116 |
# File 'lib/rails_devs_for_data_integrity.rb', line 114 def enable_all_database_violation_checks ActiveRecord::Base.alias_data_integrity_methods end |
#handle_foreign_key_violation(name, options = {}) ⇒ Object
Handle a MySQL foreign key violation by placing an error message on violating foreign key field
name - the field name of the duplicate key.
Options
:message - custom message. Defaults ‘association does not exist’.
Example
class User < ActiveRecord::Base
handle_foreign_key_violation :primary_email_id, :message => 'is does not exist"
end
91 92 93 94 |
# File 'lib/rails_devs_for_data_integrity.rb', line 91 def handle_foreign_key_violation(name, ={}) alias_data_integrity_methods self. = {name.to_sym => } end |
#handle_foreign_key_violations(options = {}) ⇒ Object
Handle a MySQL foreign key violations by placing an error message on violating foreign key field
:message - custom message. Defaults ‘association does not exist’.
Options
Options are a hash of foreign_key field name to options (messages). Without options all violations use the default.
Example
class User < ActiveRecord::Base
handle_foreign_key_violations :primary_email_id => {:message => 'is does not exist"}
end
108 109 110 111 |
# File 'lib/rails_devs_for_data_integrity.rb', line 108 def handle_foreign_key_violations(={}) self. = alias_data_integrity_methods end |
#handle_unique_key_violation(*args) ⇒ Object
Handle a MySQL unique key violation by placing an error message on name Currently only one duplicate key per table is supported because no parsing support to determine the violating key
name - the field name of the duplicate key.
Options
:message - custom message. Defaults ‘is not unique’.
Example
class User < ActiveRecord::Base
handle_unique_key_violation :user_name, :message => 'is taken"
end
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rails_devs_for_data_integrity.rb', line 66 def handle_unique_key_violation(*args) alias_data_integrity_methods = args. || {} .symbolize_keys! args.each do |name| self.[ name.to_sym ]= .merge( :field_name => name.to_s, :columns => [name.to_s]. concat( ([:scope]||[]).collect(&:to_s) ).uniq.sort ) end end |