uidable 1.1.0 → 2.0.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: 049db3fd13da41c90f3f26522a6a3fbe7ed7fc0a
4
- data.tar.gz: e229b43eedd837dfa33a2fb7add8bf6e76cfbe15
3
+ metadata.gz: 932433d8f3888343b55428409e091316f90f5e41
4
+ data.tar.gz: 2004454ac043c3bc3e8dcbccecd141e1f1c4dfb0
5
5
  SHA512:
6
- metadata.gz: 666c26af8fcd4071393c5ad29faa6b3a5a51d9dca8ecefe899ad2683cad3fc199ff300eb51e826e1c82e6aad7ee26519214ef5cec554ad7610cac287bdb616f2
7
- data.tar.gz: 6dc71358a57cf9dff53d8e5cb67d846e677587382d55244e2b1b4a592e4ebbe960d985d431ca787c8a5c750be498a1b24d414e3357a5aadb20248686ff8a6ba2
6
+ metadata.gz: 5e6e856d7c9c914ac5b02755ad779011374286a198061d5c62524346adf1effc28e73ac3600b00d91f390837dc6ce666f441aec8512827bb206883797447e70a
7
+ data.tar.gz: 8663d0c4cb2790a919d73e20d8b72d9de7d664761e56ed64be11fbdcd6ec61bd4c0af235de6f47090de844714f29b221a3920f14a944f377a6a7b2de1b7d70a4
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  [travis]: https://travis-ci.org/sibevin/uidable
9
9
  [coveralls]: https://coveralls.io/github/sibevin/uidable?branch=cover-check
10
10
 
11
- Create the uid(unqiue identifier) attribute in your model or class.
11
+ Create the uid(unique identifier) attribute in your model or class.
12
12
 
13
13
  ## Installation
14
14
 
@@ -90,13 +90,17 @@ The uid is read only by default. You can disabled it with `read_only: false`.
90
90
 
91
91
  ## Options for ActiveRecord
92
92
 
93
- ### Uniqueness and Presence Validation
93
+ ### Presence
94
94
 
95
- The uniqueness and presence validation is enabled by default. You can disable them with `uniqueness: false` and `presence: false`. Note that you should change your migration as well if needed.
95
+ The presence validation is enabled by default. You can disable them with `presence: false`.
96
+
97
+ ### Uniqueness
98
+
99
+ There are three options for the uniqueness validation - `:create`, `:always`, `:none`. `:create` is the default option, it means only check the uniqueness when creating a record. `:always` means doing uniqueness validation each time when the model is saved. `:none` means the uniqueness validation is disabled. Note that you should change your migration to support uniqueness as well if needed.
96
100
 
97
101
  ### Set to_param
98
102
 
99
- If the option `set_to_param: true` is given, the `to_param` is overrided with uid and it means you can use uid in your routes path.
103
+ If the option `set_to_param: true` is given, the `to_param` is overridden with uid and it means you can use uid in your routes path.
100
104
 
101
105
  ### Scope
102
106
 
data/lib/uidable.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "uidable/version"
1
+ require 'uidable/version'
2
2
 
3
3
  module Uidable
4
4
  DEFAULT_UID_SIZE = 32
@@ -27,29 +27,34 @@ module Uidable
27
27
 
28
28
  module ClassMethods
29
29
  def uidable(
30
- uid_name: "uid",
30
+ uid_name: 'uid',
31
31
  uid_size: DEFAULT_UID_SIZE,
32
32
  read_only: true,
33
33
  presence: true,
34
- uniqueness: true,
34
+ uniqueness: :create,
35
35
  set_to_param: false,
36
36
  scope: false)
37
37
  unless uidable_cols.include?(uid_name.to_sym)
38
+ uniqueness_check = case (uniqueness.to_sym)
39
+ when :create then "base.validates :'#{uid_name}', uniqueness: true, on: :create"
40
+ when :always then "base.validates :'#{uid_name}', uniqueness: true"
41
+ else ''
42
+ end
38
43
  uidable_cols << uid_name.to_sym
39
44
  mod = Module.new
40
45
  mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
41
46
  def self.included(base)
42
47
  if defined?(::ActiveRecord::Base) && base < ::ActiveRecord::Base
43
48
  base.before_validation :uidable_assign_#{uid_name}, on: :create
44
- #{ scope ? "base.scope :'with_#{uid_name}', -> (uid) { base.where(:'#{uid_name}' => uid) }" : "" }
45
- #{ set_to_param ? "base.include SetToParam#{uid_name}" : "" }
46
- #{ read_only ? "base.attr_readonly :'#{uid_name}'" : "" }
49
+ #{scope ? "base.scope :'with_#{uid_name}', -> (uid) { base.where(:'#{uid_name}' => uid) }" : ''}
50
+ #{set_to_param ? "base.include SetToParam#{uid_name}" : ''}
51
+ #{read_only ? "base.attr_readonly :'#{uid_name}'" : ''}
47
52
  else
48
- #{ read_only ? "attr_reader :'#{uid_name}'" : "attr_accessor :'#{uid_name}'" }
53
+ #{read_only ? "attr_reader :'#{uid_name}'" : "attr_accessor :'#{uid_name}'"}
49
54
  end
50
55
  if base.respond_to?(:validates)
51
- #{ presence ? "base.validates :'#{uid_name}', presence: true" : "" }
52
- #{ uniqueness ? "base.validates :'#{uid_name}', uniqueness: true" : "" }
56
+ #{presence ? "base.validates :'#{uid_name}', presence: true" : ''}
57
+ #{uniqueness_check}
53
58
  end
54
59
  end
55
60
 
@@ -77,5 +82,4 @@ module Uidable
77
82
  @uidable_cols ||= []
78
83
  end
79
84
  end
80
-
81
85
  end
@@ -1,3 +1,3 @@
1
1
  module Uidable
2
- VERSION = "1.1.0"
2
+ VERSION = "2.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uidable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sibevin Wang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-03 00:00:00.000000000 Z
11
+ date: 2016-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  version: '0'
79
79
  requirements: []
80
80
  rubyforge_project:
81
- rubygems_version: 2.4.8
81
+ rubygems_version: 2.4.5.1
82
82
  signing_key:
83
83
  specification_version: 4
84
84
  summary: Create the uid attribute in your model or class.