uuids 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +41 -16
- data/Rakefile +2 -1
- data/app/models/uuids/uuid.rb +1 -3
- data/lib/tasks/uuids_tasks.rake +14 -7
- data/lib/uuids/version.rb +4 -2
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +1752 -0
- data/spec/models/uuids/uuid_spec.rb +13 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 151323a9f9d9d1d1e2f0f852ca8dc3008d845022
|
4
|
+
data.tar.gz: b6b1d36f0697890239eb7d68f31f807fdb314fbf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 864757381a06c18c1c0eeaa7883199fdf24a9873747aa9a0c736dd0a744a093f7a88a06b797f07f8a82d16405ec571ed00a7f1270a2c2962944e000be0a0b645
|
7
|
+
data.tar.gz: aa74d786fb783b687e07d201491a4788d60ab8aeb8357738abef2c674687259274557e05452f9594931bf3949d5a4ed0210ecfdbd0ce5a3dff2a2ad204e8a96e
|
data/README.rdoc
CHANGED
@@ -77,32 +77,42 @@ Or install it yourself as:
|
|
77
77
|
|
78
78
|
$ gem install uuids
|
79
79
|
|
80
|
-
|
80
|
+
=== Rails application
|
81
|
+
|
82
|
+
Run from a command line in application root:
|
81
83
|
|
82
84
|
$ rake uuids:install
|
83
85
|
|
86
|
+
This will copy gem migrations to a `db/migrate` folder and run it.
|
87
|
+
|
88
|
+
=== Rails mountable engine
|
89
|
+
|
90
|
+
Run from a command line in application root:
|
91
|
+
|
92
|
+
$ rake app:uuids:install
|
93
|
+
|
94
|
+
This will copy gem migrations to a `spec/dummy/db/migrate` folder and run it.
|
95
|
+
|
84
96
|
== Usage
|
85
97
|
|
86
98
|
Add the assotiation to your AR model:
|
87
99
|
|
88
100
|
class YourModel < ActiveRecord::Base
|
89
|
-
has_many :uuids, class_name: "Uuids::Uuid", as: :record, dependent: :destroy
|
90
|
-
...
|
91
|
-
end
|
92
|
-
|
93
|
-
Note the <tt>dependent: :destroy</tt> condition! It prevents a record from
|
94
|
-
destruction before a corresponding uuids reassigned to another record.
|
95
101
|
|
96
|
-
|
102
|
+
# declares the association
|
103
|
+
has_many :uuids, class_name: "Uuids::Uuid", as: :record, dependent: :destroy
|
97
104
|
|
98
|
-
|
99
|
-
...
|
100
|
-
# create the uuid by default
|
105
|
+
# auto-generates uuid by default
|
101
106
|
before_create -> { uuids.new }
|
102
|
-
|
107
|
+
|
108
|
+
# prevents the record from being unreferable
|
109
|
+
validates :uuids, presence: true, unless: :destroy
|
103
110
|
end
|
104
111
|
|
105
|
-
|
112
|
+
**Note** the <tt>dependent: :destroy</tt> condition! It requires all uuids
|
113
|
+
to be reassigned before destruction.
|
114
|
+
|
115
|
+
Now you can refer to your model via uuid:
|
106
116
|
|
107
117
|
class CreateAnotherModelTable < ActiveRecord::Migration
|
108
118
|
def change
|
@@ -116,20 +126,35 @@ Now you can add reference to your model via uuid:
|
|
116
126
|
class AnotherModel < ActiveRecord::Base
|
117
127
|
|
118
128
|
def your_model
|
119
|
-
@your_model ||= YourModel.join(:uuids)
|
129
|
+
@your_model ||= YourModel.join(:uuids)
|
130
|
+
.where(uuids_uuids: { value: your_model_uuid }).first
|
120
131
|
end
|
121
132
|
|
122
|
-
def your_model=(
|
123
|
-
@your_model_uuid ||= Uuids::Uuid.find_by_record(value)
|
133
|
+
def your_model=(record)
|
134
|
+
@your_model_uuid ||= Uuids::Uuid.find_by_record(record).try(:value)
|
124
135
|
end
|
125
136
|
end
|
126
137
|
|
127
138
|
== Uninstallation
|
128
139
|
|
140
|
+
=== Rails app
|
141
|
+
|
129
142
|
Run from a command line in application root:
|
130
143
|
|
131
144
|
$ rake uuids:uninstall
|
132
145
|
|
146
|
+
This will rollback and remove migration from `db/migrate` folder and then
|
147
|
+
remove the gem dependencies from application's +Gemfile+ and gemspec.
|
148
|
+
|
149
|
+
=== Rails mountable engine
|
150
|
+
|
151
|
+
Run from a command line in engine root:
|
152
|
+
|
153
|
+
$ rake app:uuids:uninstall
|
154
|
+
|
155
|
+
This will rollback and remove migration from `spec/dummy/db/migrate` folder and
|
156
|
+
then remove the gem dependencies from engine's +Gemfile+ and gemspec.
|
157
|
+
|
133
158
|
== Contributing
|
134
159
|
|
135
160
|
1. Fork it ( https://github.com/nepalez/uuids/fork )
|
data/Rakefile
CHANGED
data/app/models/uuids/uuid.rb
CHANGED
@@ -23,6 +23,7 @@ module Uuids
|
|
23
23
|
#
|
24
24
|
class Uuid < ActiveRecord::Base
|
25
25
|
|
26
|
+
attr_readonly :value
|
26
27
|
belongs_to :record, polymorphic: true
|
27
28
|
validate :record_present?
|
28
29
|
before_destroy :forbid_destruction
|
@@ -35,9 +36,6 @@ module Uuids
|
|
35
36
|
|
36
37
|
private
|
37
38
|
|
38
|
-
# Disables value assignment.
|
39
|
-
attr_writer :value
|
40
|
-
|
41
39
|
# Validates record presence with a custom error message.
|
42
40
|
def record_present?
|
43
41
|
return if record
|
data/lib/tasks/uuids_tasks.rake
CHANGED
@@ -8,13 +8,17 @@ namespace :uuids do
|
|
8
8
|
desc "Uninstalls and removes the uuids gem from a Rails application"
|
9
9
|
task :uninstall do
|
10
10
|
sh "rake db:rollback SCOPE=uuids"
|
11
|
-
|
11
|
+
remove_gem
|
12
|
+
sh "bundle"
|
13
|
+
end
|
14
|
+
|
15
|
+
def remove_gem
|
16
|
+
say "Removing the 'uuids' gem" do
|
12
17
|
remove_from_file "Gemfile", /gem\s+["|']uuids["|']/
|
13
18
|
Dir["*.gemspec"].each do |gemspec|
|
14
19
|
remove_from_file gemspec, /_dependency\s+["|']uuids["|']/
|
15
20
|
end
|
16
21
|
end
|
17
|
-
sh "bundle"
|
18
22
|
end
|
19
23
|
|
20
24
|
def remove_from_file(name, regex)
|
@@ -25,17 +29,20 @@ namespace :uuids do
|
|
25
29
|
end
|
26
30
|
end
|
27
31
|
|
28
|
-
def say
|
29
|
-
print "==
|
30
|
-
" ===================================================\n"
|
32
|
+
def say(name)
|
33
|
+
print "== #{ name } #{ "=" * (75 - name.count) }\n"
|
31
34
|
yield
|
32
35
|
print "\n"
|
33
36
|
end
|
34
37
|
|
35
38
|
def say_with_time(name)
|
36
|
-
start =
|
39
|
+
start = seconds
|
37
40
|
print "-- remove from #{ name }\n"
|
38
41
|
yield
|
39
|
-
print " -> #{
|
42
|
+
print " -> #{ seconds - start }s\n"
|
43
|
+
end
|
44
|
+
|
45
|
+
def seconds
|
46
|
+
Time.now.to_f.round(4)
|
40
47
|
end
|
41
48
|
end
|
data/lib/uuids/version.rb
CHANGED
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|