Module: ActiveRecord::RailsDevsForDataIntegrity::ClassMethods

Defined in:
lib/rails_devs_for_data_integrity.rb

Instance Method Summary collapse

Instance Method Details

#enable_all_database_violation_checksObject

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, options={})
  alias_data_integrity_methods
  self.foreign_key_check_options = {name.to_sym => options}
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(options={})
  self.foreign_key_check_options = options
  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
  options = args.extract_options! || {}
  options.symbolize_keys!
  args.each do |name|
    self.unique_key_check_options[ name.to_sym ]= options.merge(
      :field_name => name.to_s,
      :columns => [name.to_s].
        concat( (options[:scope]||[]).collect(&:to_s) ).uniq.sort
    )
  end
end