rails_devs_for_data_integrity 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -56,9 +56,14 @@ module ActiveRecord::RailsDevsForDataIntegrity
56
56
  # class User < ActiveRecord::Base
57
57
  # handle_unique_key_violation :user_name, :message => 'is taken"
58
58
  # end
59
- def handle_unique_key_violation(name, options={})
59
+ def handle_unique_key_violation(*args)
60
60
  alias_data_integrity_methods
61
- self.unique_key_check_options = options.merge(:field_name => name)
61
+ options = args.extract_options! || {}
62
+ options.symbolize_keys!
63
+
64
+ args.each do |name|
65
+ self.unique_key_check_options[ name.to_sym ]= options.merge(:field_name => name)
66
+ end
62
67
  end
63
68
 
64
69
  # Handle a MySQL foreign key violation by placing an error message on violating
@@ -95,11 +100,16 @@ module ActiveRecord::RailsDevsForDataIntegrity
95
100
  alias_data_integrity_methods
96
101
  end
97
102
 
103
+ #enable uniqueness and foreign key checks for all ActiveRecord instances
104
+ def enable_all_database_violation_checks
105
+ ActiveRecord::Base.alias_data_integrity_methods
106
+ end
107
+
98
108
  protected
99
109
 
100
110
  #alias insert and update methods
101
111
  def alias_data_integrity_methods#:nodoc:
102
- return if method_defined?(:create_or_update_without_data_integrity_check)
112
+ return if method_defined?( :save_without_data_integrity_check! )
103
113
  alias_method_chain :create_or_update, :data_integrity_check
104
114
  alias_method_chain :save!, :data_integrity_check
105
115
  end
@@ -107,9 +117,15 @@ module ActiveRecord::RailsDevsForDataIntegrity
107
117
 
108
118
  # Add a duplicate error message to errors based on the exception
109
119
  def add_unique_key_error(exception)
110
- if unique_key_check_options[:field_name]
111
- self.errors.add(unique_key_check_options[:field_name], unique_key_check_options[:message]||"is a duplicate.")
120
+ unless unique_key_check_options.blank?
121
+ # we are not sure which violation occurred if we have multiple entries
122
+ # since mysql does not return a good error message
123
+ # add them all
124
+ unique_key_check_options.each do |name, options|
125
+ self.errors.add(options[:field_name], options[:message]||"has already been taken.")
126
+ end
112
127
  else
128
+ unique_key_check_options.keys
113
129
  self.errors.add_to_base(unique_key_check_options[:message]||"Duplicate field.")
114
130
  end
115
131
  end
@@ -135,16 +151,6 @@ module ActiveRecord::RailsDevsForDataIntegrity
135
151
  end
136
152
  end
137
153
 
138
- # The classes unique key check options
139
- def unique_key_check_options
140
- self.class.unique_key_check_options
141
- end
142
-
143
- # The classes foreign key check options
144
- def foreign_key_check_options
145
- self.class.foreign_key_check_options
146
- end
147
-
148
154
  # If +exception+ is a unique key violation or a foreign key error,
149
155
  # excute the block if it exists. If not and a record exists, add the
150
156
  # appropriate error messages. Reraise any exceptions that are not data integrity violation errors
@@ -206,11 +212,10 @@ module ActiveRecord::RailsDevsForDataIntegrity
206
212
  # RecordNotSaved will be thrown by save! before converting to the standard
207
213
  # validation error ActiveRecord::RecordInvalid
208
214
  def save_with_data_integrity_check!(*args)
209
- unless save(*args)
210
- raise ActiveRecord::RecordInvalid.new(self) if @duplicate_exception||@foreign_key_exception
211
- raise ActiveRecord::RecordNotSaved
212
- end
213
- true
215
+ save_without_data_integrity_check!(*args)
216
+ rescue ActiveRecord::RecordNotSaved => e
217
+ raise ActiveRecord::RecordInvalid.new(self) if @duplicate_exception||@foreign_key_exception
218
+ raise e
214
219
  end
215
220
  end
216
221
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails_devs_for_data_integrity}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Blythe Dunham"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_devs_for_data_integrity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blythe Dunham