public_id 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,7 +24,7 @@ rails generate migration add_ident_to_users ident:string
24
24
  Tell your activerecord object that ident is your new public identifier.
25
25
  ```ruby
26
26
  class User < ActiveRecord::Base
27
- publically_identified_by :ident
27
+ has_public_id :ident
28
28
  # Automatically defines to_param as :ident
29
29
  end
30
30
  User.new.ident
@@ -56,7 +56,7 @@ There's a few other convenience methods that you may find useful.
56
56
 
57
57
  * Get a new random ID for your own nefarious purposes:
58
58
 
59
- ``` User.new_public_identifier ```
59
+ ``` User.new_public_id ```
60
60
 
61
61
  ### Configuration
62
62
 
@@ -66,11 +66,11 @@ The suffix and prefix are joined by a dash.
66
66
 
67
67
  You can skip the prefix alltogether:
68
68
  ```ruby
69
- publically_identified_by column_name, length: 10, prefix: false
69
+ has_public_id column_name, length: 10, prefix: false
70
70
  ```
71
71
  or set it directly:
72
72
  ```
73
- publically_identified_by other_column_name, length: 15, prefix: 'user_'
73
+ has_public_id other_column_name, length: 15, prefix: 'user_'
74
74
  ```
75
75
  The "length" option refers to the length argument passed to SecureRandom. The actual length
76
76
  of the random base64 string will be about 4/3's this length. The defaults to 10, for a
@@ -0,0 +1,55 @@
1
+ module PublicId
2
+ module ActiveRecord
3
+ module InstanceMethods
4
+ def to_param
5
+ self.send(public_id_attr)
6
+ end
7
+ def public_id_attr
8
+ self.class.public_id_attr
9
+ end
10
+ def initialize_public_id
11
+ self.send(public_id_attr) or
12
+ self.send("#{public_id_attr}=", self.class.new_public_id)
13
+ end
14
+ end
15
+ module PublicallyIdentifiedBy
16
+ extend ActiveSupport::Concern
17
+ included do
18
+ end
19
+ module ClassMethods
20
+ def has_public_id(attribute_name, *args)
21
+ return if respond_to?(:public_id_attribute)
22
+ options = args.extract_options!
23
+ class << self
24
+ attr_accessor :public_id_attr, :public_id_options
25
+ # def public_identifier
26
+ # @public_identifier
27
+ # end
28
+ # def public_identifier=(attribute_name)
29
+ # @public_identifier = attribute_name
30
+ # end
31
+ def initialize_public_ids!
32
+ self.where(self.public_id_attr => nil).find_each do |obj|
33
+ obj.update_attribute(self.public_id_attr, self.new_public_id)
34
+ end
35
+ end
36
+ def find_by_public_id(public_id)
37
+ where(self.public_id_attr => public_id).first
38
+ end
39
+ def new_public_id
40
+ while(true)
41
+ new_id = ::PublicId::Util.new_public_id(self, self.public_id_options)
42
+ break unless where(self.public_id_attr => new_id).exists?
43
+ end
44
+ return new_id
45
+ end
46
+ end
47
+ self.public_id_attr = attribute_name
48
+ self.public_id_options = options
49
+ include ::PublicId::ActiveRecord::InstanceMethods
50
+ after_initialize :initialize_public_id
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -6,7 +6,7 @@ module PublicId
6
6
  def self.generate_prefix(klass, joiner)
7
7
  klass.to_s.demodulize.underscore.first(3) + joiner
8
8
  end
9
- def self.new_public_identifier(klass, options = {})
9
+ def self.new_public_id(klass, options = {})
10
10
  length = options[:length] || 10
11
11
  prefix = options.fetch(:prefix, generate_prefix(klass, options.fetch(:joiner, '-') ))
12
12
  suffix = generate_random_suffix(length)
@@ -1,3 +1,3 @@
1
1
  module PublicId
2
- VERSION = "0.5.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  class User < ActiveRecord::Base
2
- publically_identified_by :ident
2
+ has_public_id :ident
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: public_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -52,7 +52,7 @@ executables: []
52
52
  extensions: []
53
53
  extra_rdoc_files: []
54
54
  files:
55
- - lib/public_id/activerecord/publically_identified_by.rb
55
+ - lib/public_id/activerecord/has_public_id.rb
56
56
  - lib/public_id/util.rb
57
57
  - lib/public_id/version.rb
58
58
  - lib/public_id.rb
@@ -1,55 +0,0 @@
1
- module PublicId
2
- module ActiveRecord
3
- module InstanceMethods
4
- def to_param
5
- self.send(self.class.public_identifier_attribute)
6
- end
7
- def public_identifier_attribute
8
- self.class.public_identifier_attribute
9
- end
10
- def initialize_public_id
11
- self.send(public_identifier_attribute) or
12
- self.send("#{public_identifier_attribute}=", self.class.new_public_identifier)
13
- end
14
- end
15
- module PublicallyIdentifiedBy
16
- extend ActiveSupport::Concern
17
- included do
18
- end
19
- module ClassMethods
20
- def publically_identified_by(attribute_name, *args)
21
- return if respond_to?(:public_identifier_attribute)
22
- options = args.extract_options!
23
- class << self
24
- attr_accessor :public_identifier_attribute, :public_identifier_options
25
- # def public_identifier
26
- # @public_identifier
27
- # end
28
- # def public_identifier=(attribute_name)
29
- # @public_identifier = attribute_name
30
- # end
31
- def initialize_public_ids!
32
- self.where(self.public_identifier_attribute => nil).find_each do |obj|
33
- obj.update_attribute(self.public_identifier_attribute, self.new_public_identifier)
34
- end
35
- end
36
- def find_by_public_id(public_id)
37
- where(self.public_identifier_attribute => public_id).first
38
- end
39
- def new_public_identifier
40
- while(true)
41
- new_id = ::PublicId::Util.new_public_identifier(self, self.public_identifier_options)
42
- break unless where(self.public_identifier_attribute => new_id).exists?
43
- end
44
- return new_id
45
- end
46
- end
47
- self.public_identifier_attribute = attribute_name
48
- self.public_identifier_options = options
49
- include ::PublicId::ActiveRecord::InstanceMethods
50
- after_initialize :initialize_public_id
51
- end
52
- end
53
- end
54
- end
55
- end