uuids 0.1.0 → 0.2.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 +17 -12
- data/Rakefile +2 -1
- data/lib/uuids/base.rb +36 -0
- data/lib/uuids/version.rb +1 -1
- data/lib/uuids.rb +1 -0
- data/spec/dummy/app/models/item.rb +4 -0
- data/spec/dummy/db/migrate/20141017081115_create_items.rb +8 -0
- data/spec/dummy/db/schema.rb +6 -1
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/test.log +3715 -0
- data/spec/lib/uuids/base_spec.rb +53 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e74d457a60d98ff3a145f6a6a1833f2075983af
|
4
|
+
data.tar.gz: 6ff0c0939c243ef4648edb145a5d6f5b3b9cab3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d4b2414ae87ce041c34248cb76c4e382072d2e20f5e1b54c86a884b5cb488bc703acf02220a8012fb8f3bb080dac55801a779147c67cd73567375ee182016fa
|
7
|
+
data.tar.gz: 168e82b3e79766a35e814ccb6660f35d4a82d277a8a9a23030936099faa4aa4c631a748beceadb5f72766dd1293b2c50662f2f9b4729106634faf645aabd8499
|
data/README.rdoc
CHANGED
@@ -16,6 +16,11 @@ identified by many uuids.
|
|
16
16
|
|
17
17
|
The gem is a {mountable Rails engine}[http://guides.rubyonrails.org/engines.html#generating-an-engine].
|
18
18
|
|
19
|
+
The gem provides:
|
20
|
+
|
21
|
+
<tt>Uuids::Uuid</tt>:: A model of UUIDs associated with various records.
|
22
|
+
<tt>Uuids::Base</tt>:: A module to be included to AR model to provide required +#uuids+ attribute.
|
23
|
+
|
19
24
|
=== Pattern
|
20
25
|
|
21
26
|
This model allows refering records by some uuid (not the id).
|
@@ -52,12 +57,6 @@ The Uuid is generated authomatically following the
|
|
52
57
|
This allows keeping all uuids in one model and referring to any record whatever
|
53
58
|
type it has.
|
54
59
|
|
55
|
-
=== Models
|
56
|
-
|
57
|
-
A model <tt>Uuids::Uuid</tt> has two attributes: +value+ and +record+.
|
58
|
-
Only the +record+ can (and must) be set and changed.
|
59
|
-
The readonly +value+ (the uuid itself) is assigned by default.
|
60
|
-
|
61
60
|
=== Translations
|
62
61
|
|
63
62
|
Error messages are translated to English and Russian (see `config/locales`).
|
@@ -100,17 +99,23 @@ Add the assotiation to your AR model:
|
|
100
99
|
class YourModel < ActiveRecord::Base
|
101
100
|
|
102
101
|
# declares the association
|
103
|
-
has_many :uuids, class_name: "Uuids::Uuid", as: :record,
|
102
|
+
has_many :uuids, class_name: "Uuids::Uuid", as: :record, validate: false
|
104
103
|
|
105
104
|
# auto-generates uuid by default
|
106
|
-
before_create
|
105
|
+
before_create -> { uuids.present? || uuids.new }
|
107
106
|
|
108
107
|
# prevents the record from being unreferable
|
109
|
-
validates :uuids, presence: true,
|
108
|
+
validates :uuids, presence: true, on: :update
|
109
|
+
|
110
|
+
# prevents a record destruction before its uuids reassigned to another one
|
111
|
+
before_destroy -> { uuids.blank? }
|
110
112
|
end
|
111
113
|
|
112
|
-
|
113
|
-
|
114
|
+
Instead of above you can simply include the Uuids::Base module:
|
115
|
+
|
116
|
+
class YourModel < ActiveRecord::Base
|
117
|
+
include Uuids::Base
|
118
|
+
end
|
114
119
|
|
115
120
|
Now you can refer to your model via uuid:
|
116
121
|
|
@@ -169,7 +174,7 @@ Redefine AR::Base class methods +has_one+ and +has_many+ to allow reference
|
|
169
174
|
by uuid:
|
170
175
|
|
171
176
|
class MyModel < ActiveRecord::Base
|
172
|
-
|
177
|
+
include Uuids::Associations
|
173
178
|
|
174
179
|
has_one :another_model, uuid: true
|
175
180
|
end
|
data/Rakefile
CHANGED
data/lib/uuids/base.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Uuids
|
2
|
+
|
3
|
+
# Creates required `#uuids` attribute of the ActiveRecord model.
|
4
|
+
module Base
|
5
|
+
|
6
|
+
def self.included(klass)
|
7
|
+
fail unless klass.ancestors.include? ActiveRecord::Base
|
8
|
+
|
9
|
+
klass.has_many :uuids, uuids_params
|
10
|
+
|
11
|
+
klass.before_create :add_default_uuid
|
12
|
+
klass.before_destroy :prevent_destruction
|
13
|
+
|
14
|
+
klass.validates :uuids, presence: true, on: :update
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
# Parameters for uuid associations
|
20
|
+
def self.uuids_params
|
21
|
+
{ class_name: "Uuids::Uuid", as: :record, validate: false }
|
22
|
+
end
|
23
|
+
|
24
|
+
# Creates the uuids by default preventing a record from being ureferrable
|
25
|
+
def add_default_uuid
|
26
|
+
uuids.present? || uuids.new
|
27
|
+
end
|
28
|
+
|
29
|
+
# Prevents destruction of a record before its uuids assigned to another one
|
30
|
+
def prevent_destruction
|
31
|
+
return true if uuids.blank?
|
32
|
+
errors.add :base, :uuids_present
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/uuids/version.rb
CHANGED
data/lib/uuids.rb
CHANGED
data/spec/dummy/db/schema.rb
CHANGED
@@ -11,7 +11,12 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended that you check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
14
|
+
ActiveRecord::Schema.define(version: 20141017081115) do
|
15
|
+
|
16
|
+
create_table "items", force: true do |t|
|
17
|
+
t.datetime "created_at"
|
18
|
+
t.datetime "updated_at"
|
19
|
+
end
|
15
20
|
|
16
21
|
create_table "records", force: true do |t|
|
17
22
|
t.datetime "created_at"
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|