acts_as_having_string_id 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -1
- data/lib/acts_as_having_string_id/railtie.rb +8 -1
- data/lib/acts_as_having_string_id/version.rb +1 -1
- data/lib/acts_as_having_string_id.rb +12 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 203f83e904e3610801246812ab5c46d198d050c7
|
4
|
+
data.tar.gz: def4ce48a2648ad8badf8fc8599f71a4495e99e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99a188bf7ee46b217d20293c0e0faf5208c24c05f50a8f77a28fe478da77cb2bafb1d583724eb1c8f172e38c34d5f134ad8fe9955f48c6027aeafa9ec32ceb12
|
7
|
+
data.tar.gz: f768f3a63fd24fd5fdee863ac4e2e3319d6f2eace227cb4c1d88b9f81cf84285b5c40018eedf54eab1b22067575dca4c41375831a51b81f78cddb01617f03153
|
data/README.md
CHANGED
@@ -78,9 +78,20 @@ Then, for exposing your string ID, use the `id_string` method. For example, if y
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
+
You can also get the string representation of an id without having the instance
|
82
|
+
|
83
|
+
> MyModel.id_string(1)
|
84
|
+
=> "7EajpSfdWIf"
|
85
|
+
|
86
|
+
And, conversely, getting the id from the string representation
|
87
|
+
|
88
|
+
> MyModel.id_int("7EajpSfdWIf")
|
89
|
+
=> 1
|
90
|
+
|
81
91
|
And that's just about it!
|
82
92
|
|
83
93
|
## TODO
|
94
|
+
* You should be able to do `MyOtherModel.create! my_model_id: "KuUnDvpJYS2"` and `my_other_model.my_model_id = "KuUnDvpJYS2"`
|
84
95
|
* Since the `MyModel.find("7EajpSfdWIf")` functionality depends on the argument now being a string, `MyModel.find("5")` will no longer mean `MyModel.find(5)`, but rather `MyModel.find(4387534)` or something. Is that a problem?
|
85
96
|
* It's a potential security problem that we don't force strings from controllers (integer id coming from JSON postdata will make it find by original id)
|
86
97
|
* Although TEA handles (and outputs) 64-bit ids, we currently limit the input to 32-bit
|
@@ -103,7 +114,15 @@ $ gem install acts_as_having_string_id
|
|
103
114
|
```
|
104
115
|
|
105
116
|
## Contributing
|
106
|
-
To contribute, fork the repo, edit the code
|
117
|
+
To contribute, fork and clone the repo, edit the code. Add tests, run them using
|
118
|
+
|
119
|
+
bin/test
|
120
|
+
|
121
|
+
Then create a pull request.
|
122
|
+
|
123
|
+
To build the gem (this is mostly for myself), run
|
124
|
+
|
125
|
+
gem build acts_as_having_string_id.gemspec
|
107
126
|
|
108
127
|
## Acknowledgements
|
109
128
|
The Tiny Encryption Algorithm was created by David Wheeler and Roger Needham of the Cambridge Computer Laboratory. This library's implementation is based on [this code](https://github.com/pmarreck/ruby-snippets/blob/master/TEA.rb) by Jeremy Hinegardner.
|
@@ -1,7 +1,14 @@
|
|
1
1
|
module ActsAsHavingStringId
|
2
2
|
class Railtie < Rails::Railtie
|
3
3
|
initializer "railtie.include_in_application_record" do
|
4
|
-
|
4
|
+
if defined?(Spring)
|
5
|
+
Spring.after_fork do
|
6
|
+
# This needs to happen every time Spring reloads
|
7
|
+
ApplicationRecord.include(ActsAsHavingStringId)
|
8
|
+
end
|
9
|
+
else
|
10
|
+
ApplicationRecord.include(ActsAsHavingStringId)
|
11
|
+
end
|
5
12
|
end
|
6
13
|
end
|
7
14
|
end
|
@@ -10,7 +10,18 @@ module ActsAsHavingStringId
|
|
10
10
|
def acts_as_having_string_id(options = {})
|
11
11
|
class_eval do
|
12
12
|
attribute :id, ActsAsHavingStringId::StringId.new(_tea)
|
13
|
+
|
14
|
+
def self.id_string(id)
|
15
|
+
# Return the string representation of id
|
16
|
+
_tea.encrypt(id).base62_encode
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.id_int(id_string)
|
20
|
+
# Return the id from a string representation
|
21
|
+
_tea.decrypt(id_string.base62_decode)
|
22
|
+
end
|
13
23
|
end
|
24
|
+
|
14
25
|
include ActsAsHavingStringId::LocalInstanceMethods
|
15
26
|
end
|
16
27
|
|
@@ -22,7 +33,7 @@ module ActsAsHavingStringId
|
|
22
33
|
|
23
34
|
module LocalInstanceMethods
|
24
35
|
def id_string
|
25
|
-
self.class.
|
36
|
+
self.class.id_string(id)
|
26
37
|
end
|
27
38
|
end
|
28
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_having_string_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Magnus Hult
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|