validates_nested_uniqueness 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,22 +7,52 @@ module ActiveModel
7
7
 
8
8
  def initialize(options)
9
9
  super(options)
10
- @klass = options[:class]
11
- @association = options[:association]
10
+ @klass = get_class_name(options[:scope])
11
+ @association = options[:association]
12
+ @message = options[:message]
12
13
  end
13
14
 
14
15
  def validate_each(record, attribute, value)
15
- parent = nil
16
- ObjectSpace.each_object(eval(@klass)) { |r| parent = r if r.try(@association).include?(record) }
17
- if parent
18
- count = parent.try(@association).to_a.count { |r| r[attribute] == record[attribute] }
19
- if count > 1
20
- record.errors.add(attribute, 'duplicate', :value => 'val')
16
+
17
+ # working on trying to use conventions to determine association
18
+ # also, if has only one belongs_to, can use that as a default.
19
+ #puts record.class.name.underscore.to_sym
20
+ #puts(eval(@klass).reflect_on_association(record.class.name.underscore.to_sym))
21
+
22
+ if parent = get_parent(record)
23
+ if get_child_count(parent, record, attribute) > 1
24
+ record.errors.add(attribute,
25
+ @message,
26
+ :value => record[attribute] )
21
27
  end
22
28
  end
23
29
  end
30
+
31
+ # Gets a class name from a symbol.
32
+ def get_class_name(sym)
33
+ sym.to_s.split('_').collect(&:capitalize).join
34
+ end
35
+
36
+ # Searches ObjectSpace for the parent object of record.
37
+ #
38
+ # It looks at all objects for the given class, then checks
39
+ def get_parent(record)
40
+ parent = nil
41
+ ObjectSpace.each_object(eval(@klass)) { |r|
42
+ parent = r if r.try(@association).include?(record)
43
+ }
44
+ parent
45
+ end
46
+
47
+ # Searches the parent for duplicate children.
48
+ # A duplicate is defined as another child with an equivilant
49
+ # attribute.
50
+ def get_child_count(parent, record, attribute)
51
+ count = parent.try(@association).to_a.count { |r|
52
+ r[attribute] == record[attribute]
53
+ }
54
+ end
24
55
 
25
56
  end
26
-
27
57
  end
28
58
  end
@@ -1,3 +1,3 @@
1
1
  module ValidatesNestedUniqueness
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_nested_uniqueness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-26 00:00:00.000000000 Z
12
+ date: 2012-04-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: activemodel
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -50,8 +66,8 @@ executables: []
50
66
  extensions: []
51
67
  extra_rdoc_files: []
52
68
  files:
53
- - lib/validates_nested_uniqueness/version.rb
54
69
  - lib/validates_nested_uniqueness.rb
70
+ - lib/validates_nested_uniqueness/version.rb
55
71
  homepage: http://github.com/kstevens715/validates_nested_uniqueness
56
72
  licenses: []
57
73
  post_install_message:
@@ -72,8 +88,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
88
  version: '0'
73
89
  requirements: []
74
90
  rubyforge_project:
75
- rubygems_version: 1.8.21
91
+ rubygems_version: 1.8.23
76
92
  signing_key:
77
93
  specification_version: 3
78
94
  summary: Validates uniqueness of has_many children
79
95
  test_files: []
96
+ has_rdoc: