public_uid 1.1.0 → 1.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: 76143c57285dda08be346c203901c384bc273bf0
4
- data.tar.gz: 1bf42db639233d908a3bd447c57d9be3582fa572
3
+ metadata.gz: 84a09238730e59d9814e21cb6981e8679583f939
4
+ data.tar.gz: dfec8f57246595a82792f17c1280fc50302a7412
5
5
  SHA512:
6
- metadata.gz: fd980096e936fa5816f1838aeb614d2d51dda724f66f046e45bceab9d072a0103e09a07325e92e800d45217352dc10cfe464c0897888b9711090c98d3a820428
7
- data.tar.gz: 31f622b8165abae325f8c81921820fc18b91bf8dd0fe6c51a152c989834ef6a31b3841ef9d0a1e9ce40fec2ca57941e99e8f3c73b0591e0c8cad7d7a0cb7b8c3
6
+ metadata.gz: 20cb1ef26cd4f60ee236fa83c086be46c91a59011a942560bd740da7d640455e49cec638317dfcc4402cadc5bb18c027622edb653a7ff20b34e4f756986cdab1
7
+ data.tar.gz: 1f354dc5c5523d2c1ec2dd3f7f401da9d7d8f2e3c4ff471a91de8ebe0712ea7d8c291252ae9cb6b2505e7eb831145d2c1fa48651844024178f6d0dad16b2d1f0
data/.travis.yml CHANGED
@@ -1,8 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.2
4
3
  - 1.9.3
5
4
  - 2.0.0
6
5
  - 2.1.0
7
6
  - 2.1.1
7
+ - 2.2.2
8
8
  - ruby-head
data/README.md CHANGED
@@ -156,6 +156,14 @@ u.save! #=> true
156
156
  u.public_uid #=> 2398
157
157
  ```
158
158
 
159
+ ### Rails rake task
160
+
161
+ By using this gem you will automatically gain rake task `rake public_uid:generate`
162
+ in your Rails application which will generate `public_uid` on the tables
163
+ using `public_uid` on records where `public_uid == nil`.
164
+
165
+ This is helpfull to generate `public_uid` on pre-existing recrods.
166
+
159
167
  ## Contributing
160
168
 
161
169
  1. Fork it
data/lib/public_uid.rb CHANGED
@@ -5,4 +5,6 @@ require "public_uid/model"
5
5
  require "public_uid/generators/number_random"
6
6
  require "public_uid/generators/range_string"
7
7
 
8
+ require 'public_uid/tasks' if defined?(Rails)
9
+
8
10
  require 'orm/active_record' if defined?(ActiveRecord::Base)
@@ -4,20 +4,46 @@ module PublicUid
4
4
  base.extend(ClassMethods)
5
5
  end
6
6
 
7
- def generate_uid
8
- pub_uid = SetPublicUid.new(record: self, column: self.class.public_uid_column)
9
- pub_uid.generate self.class.public_uid_generator
10
- pub_uid.set
7
+ module InstanceMethods
8
+ def generate_uid
9
+ generate_uid! unless send(self.class.public_uid_column)
10
+ end
11
+
12
+ def generate_uid!
13
+ pub_uid = SetPublicUid.new(record: self, column: self.class.public_uid_column)
14
+ pub_uid.generate self.class.public_uid_generator
15
+ pub_uid.set
16
+ end
11
17
  end
12
18
 
13
19
  module ClassMethods
14
- attr_reader :public_uid_column
15
- attr_reader :public_uid_generator
20
+ attr_writer :public_uid_column
21
+ attr_writer :public_uid_generator
16
22
 
17
23
  def generate_public_uid(options={})
18
- @public_uid_column = options[:column] || :public_uid
19
- @public_uid_generator = options[:generator] || Generators::RangeString.new
20
- before_create :generate_uid, unless: @public_uid_column
24
+ @public_uid_column = options[:column]
25
+ @public_uid_generator = options[:generator]
26
+
27
+ _include_public_uid_instance_methods
28
+ _set_callback_to_generate_public_uid
29
+ end
30
+
31
+
32
+ def public_uid_column
33
+ @public_uid_column || :public_uid
34
+ end
35
+
36
+ def public_uid_generator
37
+ @public_uid_generator || Generators::RangeString.new
38
+ end
39
+
40
+ private
41
+ def _include_public_uid_instance_methods
42
+ self.include PublicUid::Model::InstanceMethods
43
+ end
44
+
45
+ def _set_callback_to_generate_public_uid
46
+ self.before_create :generate_uid
21
47
  end
22
48
  end
23
49
  end
@@ -0,0 +1,7 @@
1
+ module PublicUid
2
+ class Tasks < Rails::Railtie
3
+ rake_tasks do
4
+ Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ namespace :public_uid do
2
+ desc "Generate public_uid on Models that have public_uid column on records that have nil public_uid"
3
+ task :generate => :environment do
4
+ Rails.application.eager_load!
5
+ ActiveRecord::Base.descendants.each do |model|
6
+ model.connection # establis conection
7
+
8
+ if model.instance_methods.collect(&:to_s).include?('generate_uid')
9
+ puts "Model #{model.name}: generating public_uids for missing records"
10
+
11
+ model
12
+ .where(public_uid: nil)
13
+ .tap { |scope| puts " * generating #{scope.count} public_uid(s)" }
14
+ .each do |record_without_public_uid|
15
+ record_without_public_uid.generate_uid
16
+ record_without_public_uid.save!
17
+ end
18
+ puts ''
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module PublicUid
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ TestConf.orm_modules.each do |orm_module|
4
+ describe orm_module.description do
5
+ context "model doesn't want to generate public_uid" do
6
+
7
+ let(:model) do
8
+ "#{orm_module}::ModelWithoutGenaratePublicUid".constantize
9
+ end
10
+
11
+ it 'model should not expose :generate_uid public instance method' do
12
+ model.instance_methods.wont_include :generate_uid
13
+ end
14
+ end
15
+
16
+ context "model wants to generate public_uid" do
17
+ let(:model) do
18
+ "#{orm_module}::ModelWithGeneratorDefaults".constantize
19
+ end
20
+
21
+ it 'model should expose :generate_uid public instance method' do
22
+ model.instance_methods.must_include :generate_uid
23
+ end
24
+ end
25
+ end
26
+ end
@@ -25,6 +25,10 @@ module ActRec
25
25
  generate_public_uid
26
26
  end
27
27
 
28
+ class ModelWithoutGenaratePublicUid < ActiveRecord::Base
29
+ self.table_name = "users"
30
+ end
31
+
28
32
  class User < ActiveRecord::Base
29
33
  self.table_name = 'users'
30
34
  end
metadata CHANGED
@@ -1,111 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: public_uid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Valent
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-05 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: orm_adapter
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '5'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '5'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rr
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: 1.1.2
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.1.2
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: activerecord
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ~>
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
103
  version: '3.2'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ~>
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '3.2'
111
111
  description: Automatic generates public unique identifier for model
@@ -115,8 +115,8 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
- - .gitignore
119
- - .travis.yml
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
120
  - Gemfile
121
121
  - LICENSE.txt
122
122
  - README.md
@@ -127,12 +127,15 @@ files:
127
127
  - lib/public_uid/generators/range_string.rb
128
128
  - lib/public_uid/model.rb
129
129
  - lib/public_uid/set_public_uid.rb
130
+ - lib/public_uid/tasks.rb
131
+ - lib/public_uid/tasks/generate.rake
130
132
  - lib/public_uid/version.rb
131
133
  - public_uid.gemspec
132
134
  - test/lib/generators/number_random_test.rb
133
135
  - test/lib/generators/range_string_test.rb
134
136
  - test/lib/set_public_uid_test.rb
135
137
  - test/models/custom_public_uid_column_test.rb
138
+ - test/models/instance_methods_initialization_test.rb
136
139
  - test/models/model_with_custom_generator_test.rb
137
140
  - test/models/model_with_generator_defaults_test.rb
138
141
  - test/support/orm/active_record.rb
@@ -148,17 +151,17 @@ require_paths:
148
151
  - lib
149
152
  required_ruby_version: !ruby/object:Gem::Requirement
150
153
  requirements:
151
- - - '>='
154
+ - - ">="
152
155
  - !ruby/object:Gem::Version
153
156
  version: '0'
154
157
  required_rubygems_version: !ruby/object:Gem::Requirement
155
158
  requirements:
156
- - - '>='
159
+ - - ">="
157
160
  - !ruby/object:Gem::Version
158
161
  version: '0'
159
162
  requirements: []
160
163
  rubyforge_project:
161
- rubygems_version: 2.0.14
164
+ rubygems_version: 2.4.6
162
165
  signing_key:
163
166
  specification_version: 4
164
167
  summary: Automatic generates public UID column
@@ -167,6 +170,7 @@ test_files:
167
170
  - test/lib/generators/range_string_test.rb
168
171
  - test/lib/set_public_uid_test.rb
169
172
  - test/models/custom_public_uid_column_test.rb
173
+ - test/models/instance_methods_initialization_test.rb
170
174
  - test/models/model_with_custom_generator_test.rb
171
175
  - test/models/model_with_generator_defaults_test.rb
172
176
  - test/support/orm/active_record.rb