ulid-rails 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
  SHA256:
3
- metadata.gz: 83a5f3c3e4d66efd9d2df7948564b0e9603c1740dd2f3d8114f53d9db1da2d6b
4
- data.tar.gz: ce3fa484e01d9886b24e9ad6c2e3ea18c7ddd7f2608888af48ef758abafd756e
3
+ metadata.gz: 4d66abad8321b174f9037f061da19837f539c4d42d01d328f267710023a7fc1d
4
+ data.tar.gz: df85e60420b30edf90ad5790cf1c70c338950e8b062363c10c29309cff03ecfb
5
5
  SHA512:
6
- metadata.gz: 6782bc00605c44caec5faeaa1ec5e94f5db637226300a1db95cb852633405bf95ca73099dcd275eb4b59c72fe3a140b40d722aebcccc86a56c0bd5b07f233731
7
- data.tar.gz: aaa614bb9fca638ecad81d0c5f76027ef464f7ba1e9b1e4d1493455d56f23090d03dcaaf2e1cbae27da7b40a7a2bda7f06e8d6e371d7a586e8a78385a10dde8a
6
+ metadata.gz: ab2ffb0c44b887852eb56a6fe0b4b93f9881e06f6a8ce6fe6322f28debb90a1b922d7aaff15ecc4296171addd7e73f34c094d7eefde0d451d5554011e821e7e6
7
+ data.tar.gz: c32c054dd8f14849f8b96d8ed0b6f83eb6be4006bf2c4c87cf81da381ae902f2a9e36d638a1bb317630c29f853b2aa5ad355e77fdb78de3793a25ed80f12b6d7
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # ulid-rails CHANGELOG
2
+ ## 0.2
3
+
4
+ ### Breaking Changes
5
+
6
+ - `primary_key` option's default has been changed to `false`. Now you need to specify `primary_key: true` for primary keys: `ulid :id, primary_key: true`
7
+
8
+ ### New Features
9
+
10
+ - Added `auto_generate` option
11
+
12
+ ### Bug Fixes
13
+
14
+ - Auto-generate was on for foreign keys
15
+ - Encoded ULID was not 0-padded #1
data/Gemfile CHANGED
@@ -4,3 +4,6 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in ulid-rails.gemspec
6
6
  gemspec
7
+
8
+ version = ENV['AR_VERSION'] || '6.0'
9
+ eval_gemfile File.expand_path("../gemfiles/#{version}.gemfile", __FILE__)
data/README.md CHANGED
@@ -51,7 +51,23 @@ Just add the below lines to your models.
51
51
  ```ruby
52
52
  class MyModel < ApplicationRecord
53
53
  include ULID::Rails
54
- ulid :id # The argument is the primary key column name
54
+ ulid :id, primary_key: true # The first argument is the ULID column name
55
+ end
56
+ ```
57
+
58
+ ### Extract timestamp
59
+
60
+ Since ULID includes milli seconds precision timestamp, you don't need to store `created_at`.
61
+ `ulid-rails` provides a helper method that defines timestamp method which extract timestamp from ULID column.
62
+
63
+ ```ruby
64
+ class MyModel < ApplicationRecord
65
+ include ULID::Rails
66
+ ulid :id, primary_key: true # The first argument is the ULID column name
67
+
68
+ # defines `created_at` method which extract timestamp value from id column.
69
+ # This way you don't need physical `created_at` column.
70
+ ulid_extract_timestamp :id, :created_at
55
71
  end
56
72
  ```
57
73
 
@@ -59,20 +75,34 @@ end
59
75
 
60
76
  **MySQL Only (for now)**
61
77
 
62
- Since ULID includes milli seconds precision timestamp, you don't need to store `created_at`. You can instead create a virtual column that extracts the timestamp from the ULID column.
63
-
78
+ You can define a "virtual column" in MySQL DB that acts same as a physical column.
64
79
  Defining the virtual `created_at` is kind of comlicated so this gem provides a helper method for it.
65
80
 
81
+ A virtual column is useful if you want to add index on the timestamp column or want to execute raw SQL with created_at.
82
+
66
83
  ```ruby
67
- create_table :users, id: false do |t|
68
- t.binary :id, limit: 16, primary_key: true
69
- t.datetime :updated_at
70
- t.virtual_ulid_timestamp :created_at, :id
71
- end
84
+ create_table :users, id: false do |t|
85
+ t.binary :id, limit: 16, primary_key: true
86
+ t.datetime :updated_at
87
+ t.virtual_ulid_timestamp :created_at, :id
88
+ end
72
89
  ```
73
90
 
74
91
  `virtual_ulid_timestamp` takes two arguments, the first one is the name of the column name (typically, `created_at`) and the second one is the ULID column that creation timestamp is extracted from.
75
92
 
93
+ ### Auto-generate ULID
94
+
95
+ If `primary_key` is `true`, ULID is auto-generated before create by default.
96
+ You can enable or disable auto-generation with `auto_generate` option.
97
+
98
+ ```
99
+ class Model < ApplicationRecord
100
+ ulid :id, primary_key: true # primary key. auto-generate enabled
101
+ ulid :foreign_key # auto-generate disabled
102
+ ulid :ulid, auto_generate: true # non primary, auto-generate enabled
103
+ end
104
+ ```
105
+
76
106
  ## FAQ
77
107
 
78
108
  ### Foreign Keys
@@ -0,0 +1,4 @@
1
+ gem "activesupport", "~> 5.2"
2
+ gem "activemodel", "~> 5.2"
3
+ gem "activerecord", "~> 5.2"
4
+ gem 'sqlite3', '~> 1.3.6'
@@ -0,0 +1,4 @@
1
+ gem "activesupport", "~> 6.0"
2
+ gem "activemodel", "~> 6.0"
3
+ gem "activerecord", "~> 6.0"
4
+ gem 'sqlite3', '~> 1.4.1'
data/lib/ulid/rails.rb CHANGED
@@ -12,25 +12,30 @@ module ULID
12
12
  extend ActiveSupport::Concern
13
13
 
14
14
  class_methods do
15
- def ulid(column_name = :id, primary_key: true)
15
+ def ulid(column_name, primary_key: false, auto_generate: nil)
16
16
  attribute column_name, ULID::Rails::Type.new
17
17
 
18
- before_create do
19
- send("#{column_name}=", ULID.generate) if send(column_name).nil?
18
+ auto_generate = auto_generate.nil? ? primary_key : false
19
+ if auto_generate
20
+ before_create do
21
+ send("#{column_name}=", ULID.generate) if send(column_name).nil?
22
+ end
20
23
  end
21
24
 
22
- if primary_key
23
- define_method :created_at do
25
+ def ulid_extract_timestamp(ulid_column, timestamp_column = :created_at)
26
+ define_method timestamp_column do
24
27
  at = super() rescue nil
25
- if !at && (id_val = send(column_name))
28
+ if !at && (id_val = send(ulid_column))
26
29
  Time.zone.at((Base32::Crockford.decode(id_val) >> 80) / 1000.0)
27
30
  else
28
31
  at
29
32
  end
30
33
  end
31
34
 
32
- define_singleton_method(:timestamp_attributes_for_create) do
33
- []
35
+ if timestamp_column.to_s == "created_at"
36
+ define_singleton_method(:timestamp_attributes_for_create) do
37
+ []
38
+ end
34
39
  end
35
40
  end
36
41
  end
@@ -4,20 +4,12 @@ module ULID
4
4
  module Rails
5
5
  module Formatter
6
6
  def self.format(v)
7
- if v.length == 32
8
- Base32::Crockford.encode(v.hex)
9
- else
10
- v
11
- end
7
+ v.length == 32 ? Base32::Crockford.encode(v.hex).rjust(26, "0") : v
12
8
  end
13
9
 
14
10
  def self.unformat(v)
15
11
  Base32::Crockford.decode(v).to_s(16).rjust(32, "0")
16
12
  end
17
-
18
- def self.regexp
19
- /\A[[:alnum:]]{25}\z/
20
- end
21
13
  end
22
14
  end
23
15
  end
@@ -18,6 +18,8 @@ module ULID
18
18
  @formatter.format(value.to_s)
19
19
  elsif value&.encoding == Encoding::ASCII_8BIT
20
20
  @formatter.format(value.unpack("H*")[0])
21
+ elsif value&.length == 32
22
+ @formatter.format(value)
21
23
  else
22
24
  super
23
25
  end
@@ -1,5 +1,5 @@
1
1
  module ULID
2
2
  module Rails
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ulid-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazunori Kajihiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-22 00:00:00.000000000 Z
11
+ date: 2019-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ulid
@@ -131,13 +131,15 @@ extra_rdoc_files: []
131
131
  files:
132
132
  - ".gitignore"
133
133
  - ".travis.yml"
134
+ - CHANGELOG.md
134
135
  - Gemfile
135
- - Gemfile.lock
136
136
  - LICENSE.txt
137
137
  - README.md
138
138
  - Rakefile
139
139
  - bin/console
140
140
  - bin/setup
141
+ - gemfiles/5.2.gemfile
142
+ - gemfiles/6.0.gemfile
141
143
  - lib/ulid/rails.rb
142
144
  - lib/ulid/rails/formatter.rb
143
145
  - lib/ulid/rails/patch.rb
data/Gemfile.lock DELETED
@@ -1,49 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- ulid-rails (0.1.0)
5
- activemodel (>= 5.0)
6
- activerecord (>= 5.0)
7
- activesupport (>= 5.0)
8
- base32-crockford (~> 0.1)
9
- ulid (~> 1.0)
10
-
11
- GEM
12
- remote: https://rubygems.org/
13
- specs:
14
- activemodel (5.2.1)
15
- activesupport (= 5.2.1)
16
- activerecord (5.2.1)
17
- activemodel (= 5.2.1)
18
- activesupport (= 5.2.1)
19
- arel (>= 9.0)
20
- activesupport (5.2.1)
21
- concurrent-ruby (~> 1.0, >= 1.0.2)
22
- i18n (>= 0.7, < 2)
23
- minitest (~> 5.1)
24
- tzinfo (~> 1.1)
25
- arel (9.0.0)
26
- base32-crockford (0.1.0)
27
- concurrent-ruby (1.0.5)
28
- i18n (1.1.0)
29
- concurrent-ruby (~> 1.0)
30
- minitest (5.11.3)
31
- rake (10.5.0)
32
- sysrandom (1.0.5)
33
- thread_safe (0.3.6)
34
- tzinfo (1.2.5)
35
- thread_safe (~> 0.1)
36
- ulid (1.0.0)
37
- sysrandom (>= 1.0.0, < 2.0)
38
-
39
- PLATFORMS
40
- ruby
41
-
42
- DEPENDENCIES
43
- bundler (~> 1.16)
44
- minitest (~> 5.0)
45
- rake (~> 10.0)
46
- ulid-rails!
47
-
48
- BUNDLED WITH
49
- 1.16.4