rails_devs_for_data_integrity 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/rails_devs_for_data_integrity.rb +25 -20
- data/rails_devs_for_data_integrity.gemspec +1 -1
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
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(
|
59
|
+
def handle_unique_key_violation(*args)
|
60
60
|
alias_data_integrity_methods
|
61
|
-
|
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?(:
|
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
|
-
|
111
|
-
|
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
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
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
|
|