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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 151323a9f9d9d1d1e2f0f852ca8dc3008d845022
4
- data.tar.gz: b6b1d36f0697890239eb7d68f31f807fdb314fbf
3
+ metadata.gz: 5e74d457a60d98ff3a145f6a6a1833f2075983af
4
+ data.tar.gz: 6ff0c0939c243ef4648edb145a5d6f5b3b9cab3b
5
5
  SHA512:
6
- metadata.gz: 864757381a06c18c1c0eeaa7883199fdf24a9873747aa9a0c736dd0a744a093f7a88a06b797f07f8a82d16405ec571ed00a7f1270a2c2962944e000be0a0b645
7
- data.tar.gz: aa74d786fb783b687e07d201491a4788d60ab8aeb8357738abef2c674687259274557e05452f9594931bf3949d5a4ed0210ecfdbd0ce5a3dff2a2ad204e8a96e
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, dependent: :destroy
102
+ has_many :uuids, class_name: "Uuids::Uuid", as: :record, validate: false
104
103
 
105
104
  # auto-generates uuid by default
106
- before_create -> { uuids.new }
105
+ before_create -> { uuids.present? || uuids.new }
107
106
 
108
107
  # prevents the record from being unreferable
109
- validates :uuids, presence: true, unless: :destroy
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
- **Note** the <tt>dependent: :destroy</tt> condition! It requires all uuids
113
- to be reassigned before destruction.
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
- extend Uuids
177
+ include Uuids::Associations
173
178
 
174
179
  has_one :another_model, uuid: true
175
180
  end
data/Rakefile CHANGED
@@ -34,7 +34,8 @@ task :default do
34
34
  sh "bundle exec rspec spec"
35
35
  end
36
36
 
37
- task full: [:default] do
37
+ task :full do
38
+ sh "coveralls report"
38
39
  sh "rubocop"
39
40
  sh "metric_fu"
40
41
  sh "rails_best_practices"
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
@@ -2,5 +2,5 @@
2
2
  module Uuids
3
3
 
4
4
  # Current release.
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
data/lib/uuids.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "rails"
2
2
  require "securerandom"
3
3
  require "uuids/engine"
4
+ require "uuids/base"
4
5
 
5
6
  # Namespace for a module
6
7
  module Uuids
@@ -0,0 +1,4 @@
1
+ require "uuids"
2
+
3
+ class Item < ActiveRecord::Base
4
+ end
@@ -0,0 +1,8 @@
1
+ class CreateItems < ActiveRecord::Migration
2
+ def change
3
+ create_table :items do |t|
4
+
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -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: 20141016113016) do
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"
Binary file