rails_extensions 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/m_active_record_validations.rb +38 -0
- data/lib/tasks/rubyforge_config.yml +1 -1
- metadata +7 -2
@@ -0,0 +1,38 @@
|
|
1
|
+
module ActiveRecord::Validations::ClassMethods
|
2
|
+
|
3
|
+
alias_method :validates_uniqueness_case_insensitive, :validates_uniqueness_of
|
4
|
+
|
5
|
+
def validates_uniqueness_of(*attr_names)
|
6
|
+
configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken], :case_sensitive => false }
|
7
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
8
|
+
validates_each(attr_names,configuration) do |record, attr_name, value|
|
9
|
+
if value.nil? || !columns_hash[attr_name.to_s].text? || configuration[:case_sensitive] == true
|
10
|
+
condition_sql = "#{attr_name} #{attribute_condition(value)}"
|
11
|
+
condition_params = [value]
|
12
|
+
else
|
13
|
+
condition_sql = "LOWER(#{attr_name}) #{attribute_condition(value)}"
|
14
|
+
condition_params = [value.downcase]
|
15
|
+
end
|
16
|
+
if scope = configuration[:scope]
|
17
|
+
Array(scope).map do |scope_item|
|
18
|
+
scope_value = record.send(scope_item)
|
19
|
+
condition_sql << " AND #{record.class.table_name}.#{scope_item} #{attribute_condition(scope_value)}"
|
20
|
+
condition_params << scope_value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
unless record.new_record?
|
24
|
+
condition_sql << " AND #{record.class.table_name}.#{record.class.primary_key} <> ?"
|
25
|
+
condition_params << record.send(:id)
|
26
|
+
end
|
27
|
+
if record.class.respond_to? :find_with_deleted # if true it uses acts_as_paranoid deleted_at is null or status_id is deleted in search
|
28
|
+
rec = record.class.count_with_deleted(:conditions => [condition_sql, *condition_params])
|
29
|
+
else
|
30
|
+
rec = record.class.count(:conditions => [condition_sql, *condition_params])
|
31
|
+
end
|
32
|
+
if rec != 0 # when a record is found,
|
33
|
+
record.errors.add(attr_name, configuration[:message])
|
34
|
+
end
|
35
|
+
end # validates_each
|
36
|
+
end # validates_uniqueness_of
|
37
|
+
|
38
|
+
end # module ActiveRecord::Validations::ClassMethods
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: rails_extensions
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2007-10-
|
6
|
+
version: 1.0.2
|
7
|
+
date: 2007-10-23 00:00:00 -04:00
|
8
8
|
summary: rails_extensions
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -20,13 +20,17 @@ autorequire:
|
|
20
20
|
- m_migration_model
|
21
21
|
- m_form_tag_helper
|
22
22
|
- m_cookies
|
23
|
+
- m_active_record_validations
|
23
24
|
- m_active_record_base
|
24
25
|
- m_active_record_associatioins
|
25
26
|
- m_abstract_request
|
26
27
|
- rails_extensions
|
27
28
|
- m_migration_model
|
28
29
|
- m_form_tag_helper
|
30
|
+
- m_cookies
|
29
31
|
- m_active_record_base
|
32
|
+
- m_active_record_associatioins
|
33
|
+
- m_abstract_request
|
30
34
|
default_executable:
|
31
35
|
bindir: bin
|
32
36
|
has_rdoc: false
|
@@ -47,6 +51,7 @@ files:
|
|
47
51
|
- lib/m_abstract_request.rb
|
48
52
|
- lib/m_active_record_associatioins.rb
|
49
53
|
- lib/m_active_record_base.rb
|
54
|
+
- lib/m_active_record_validations.rb
|
50
55
|
- lib/m_cookies.rb
|
51
56
|
- lib/m_form_tag_helper.rb
|
52
57
|
- lib/m_migration_model.rb
|