is_unique 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,65 @@
1
+ Is Unique
2
+ =========
3
+
4
+ This is a simple ActiveRecord extension that makes sure that no duplicate records are added to the database. When you create a new model instance that already exists, it won't create a new record, but return the existing one instead. The extension generates a hash off the model attributes and use it to check for existing records. Of course, it ignores primary key, created_at, updated_at and other columns that you ask it to.
5
+
6
+ Here's an example. Say you have a Location model and you want all your locations to be unique. When you add a new location you'd like to check whether it already exists and if it does, you don't want to create a new record.
7
+
8
+ $ ./script/generate model Location name:string lat:float lng:float alias:string
9
+
10
+ IsUnique will take care of that. You need to install the gem:
11
+
12
+ $ sudo gem install is_unique
13
+
14
+ Then you need to add a column to your table to store a unique hash that would be used to check for existing records. The gem includes a generator for that:
15
+
16
+ $ ./script/generate is_unique Location
17
+
18
+ In the model you need to specify that it's supposed to be unique:
19
+
20
+ class Location < ActiveRecord::Base
21
+ is_unique
22
+ end
23
+
24
+ Then fire up console and try it:
25
+
26
+ >> l = Location.create(:name => 'London', :lat => '51.5', :lng => '-0.13', :alias => 'Londinium')
27
+ Location Load (0.4ms) SELECT * FROM "locations" WHERE ("locations"."unique_hash" = 189338025) LIMIT 1
28
+ Location Create (0.6ms) INSERT INTO "locations" ...
29
+ => #<Location id: ...>
30
+ >> l.clone.save
31
+ Location Load (0.7ms) SELECT * FROM "locations" WHERE ("locations"."unique_hash" = 189338025) LIMIT 1
32
+ Location Load (0.6ms) SELECT * FROM "locations" WHERE ("locations"."id" = 1)
33
+ => true
34
+
35
+ Now there's a couple of customization options. First, you can ignore certain attributes when calculating the hash. Say there may be different aliases for a location, but you still want just one record in the database. The is_unique method accepts an ignore parameter for that purpose:
36
+
37
+ class Location < ActiveRecord::Base
38
+ is_unique :ignore => :alias
39
+ end
40
+
41
+ >> l = Location.last
42
+ Location Load (0.7ms) SELECT * FROM "locations" ORDER BY locations.id DESC LIMIT 1
43
+ => #<Location ...>
44
+ >> n = l.clone
45
+ => #<Location ...>
46
+ >> n.alias = 'Something different'
47
+ => "Something different"
48
+ >> n.save
49
+ Location Load (0.7ms) SELECT * FROM "locations" WHERE ("locations"."unique_hash" = 189338025) LIMIT 1
50
+ Location Load (0.6ms) SELECT * FROM "locations" WHERE ("locations"."id" = 2)
51
+ => true
52
+ >> n == l
53
+ => true
54
+
55
+ If you'd like to name the unique hash column differently (it's "unique_hash" by default), you need to provide the name to the generator:
56
+
57
+ $ ./script/generate is_unique Location my_unique_hash_column_name
58
+
59
+ and specify it in the model:
60
+
61
+ class Location < ActiveRecord::Base
62
+ is_unique :hash_column => :my_unique_has_column_name
63
+ end
64
+
65
+ Copyright (c) 2010 Loco2, released under the MIT license
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -3,7 +3,7 @@ class <%= file_name.camelcase %> < ActiveRecord::Migration
3
3
  add_column :<%= table_name %>, :<%= unique_hash_column %>, :integer
4
4
  add_index :<%= table_name %>, :<%= unique_hash_column %>
5
5
  # Generate hashes for existing records
6
- <%= class_name %>.all.collect { |r| r.save(false) }
6
+ <%= class_name %>.find_each { |r| r.save(false) }
7
7
  end
8
8
 
9
9
  def self.down
@@ -5,15 +5,19 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{is_unique}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Eugene Bolshakov"]
12
- s.date = %q{2010-03-22}
12
+ s.date = %q{2010-03-24}
13
13
  s.description = %q{Makes ActiveRecord return existing records instead of creating duplicates}
14
14
  s.email = %q{eugene.bolshakov@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
15
18
  s.files = [
16
19
  ".gitignore",
20
+ "README",
17
21
  "Rakefile",
18
22
  "VERSION",
19
23
  "generators/is_unique/is_unique_generator.rb",
@@ -47,7 +47,7 @@ module IsUnique
47
47
  end
48
48
 
49
49
  def calculate_unique_hash
50
- self.is_unique_hash = unique_attributes.hash
50
+ self.is_unique_hash = unique_attributes.sort_by{ |pair| pair.hash }.hash
51
51
  end
52
52
 
53
53
  def unique_attributes
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Eugene Bolshakov
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-22 00:00:00 +03:00
17
+ date: 2010-03-24 00:00:00 +03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -37,10 +37,11 @@ executables: []
37
37
 
38
38
  extensions: []
39
39
 
40
- extra_rdoc_files: []
41
-
40
+ extra_rdoc_files:
41
+ - README
42
42
  files:
43
43
  - .gitignore
44
+ - README
44
45
  - Rakefile
45
46
  - VERSION
46
47
  - generators/is_unique/is_unique_generator.rb