acts_as_having_string_id 0.2.5 → 0.2.6

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: 54db01ec2827efa829f39dfa28e1ef30a568c331
4
- data.tar.gz: 43f0e48cbdbe91c37a905ebc757347d4bc816b2d
3
+ metadata.gz: 10da8a58d8b3da26d869dba464bae672c3361f9c
4
+ data.tar.gz: 3b0cfe3941f23bbc3f4994dfec192332258c920b
5
5
  SHA512:
6
- metadata.gz: 0ab382de65d1ed57a107f22c32bbe387bf2baea22795c3cdeda5e76db9f8e89e9115e8935e5c0565f849b6706c0590d0ae7647826051333b4d5a67ac89664c27
7
- data.tar.gz: 72994163eddae1219fc58bb50be5687955cff408aadc0fdd82fae28b6bf3c698a65b5da8cc5180ccd88e0858becd9a0661bd4f8e7a6f6a51129773f4e59f4d5f
6
+ metadata.gz: 5e133a4799d234f105368aca15a92da63447fb7c3c0eeb12bac04330e923d3741ae89ac487377b9ad5696a81420ff34b60690b069a1e3f913cb664439fa6934b
7
+ data.tar.gz: 94875370108b2cc63ab3a0d78c24f08ebbecf0e5d0312a7a1b5bc51c4e9d1655239c587619c9f997400e2cea312e3377220ce60684a540925932925266994674
data/README.md CHANGED
@@ -60,10 +60,12 @@ First, set up your `secrets.yml`:
60
60
 
61
61
  Then, call the method in your model class, after any relations to other models:
62
62
 
63
- class MyModel < ApplicationRecord
64
- has_many :my_other_model
65
- acts_as_having_string_id
66
- end
63
+ ```ruby
64
+ class MyModel < ApplicationRecord
65
+ has_many :my_other_model
66
+ acts_as_having_string_id
67
+ end
68
+ ```
67
69
 
68
70
  The id of your model will now not be an int, but rather an instance of `ActsAsHavingStringId::StringId`. As an example:
69
71
 
@@ -89,13 +91,15 @@ In all associated models, foreign keys to your model will also be this new type
89
91
 
90
92
  Then, for exposing your string ID, make sure to always use `id.to_s`. For example, if you're using [ActiveModelSerializers](https://github.com/rails-api/active_model_serializers):
91
93
 
92
- class UserSerializer < ActiveModel::Serializer
93
- attributes :id, :name
94
+ ```ruby
95
+ class UserSerializer < ActiveModel::Serializer
96
+ attributes :id, :name
94
97
 
95
- def id
96
- object.id.to_s
97
- end
98
- end
98
+ def id
99
+ object.id.to_s
100
+ end
101
+ end
102
+ ```
99
103
 
100
104
  You can get the string representation of an ID from a class without having the instance
101
105
 
@@ -9,15 +9,25 @@ module ActsAsHavingStringId
9
9
  module ClassMethods
10
10
  def acts_as_having_string_id(options = {})
11
11
  class_eval do
12
+ def self.acts_as_having_string_id?
13
+ # This class does act as having string id
14
+ true
15
+ end
16
+
12
17
  attrib_type = ActsAsHavingStringId::StringId::Type.new(self)
13
18
  attribute :id, attrib_type
14
19
 
15
20
  self.reflections.each_value do |r|
16
- # Attribute all foreign keys pointing here as well
17
- unless r.is_a? ActiveRecord::Reflection::ThroughReflection
21
+ if r.is_a?(ActiveRecord::Reflection::HasManyReflection)
18
22
  r.klass.class_eval do
19
23
  attribute r.foreign_key.to_sym, attrib_type
20
24
  end
25
+ elsif r.is_a?(ActiveRecord::Reflection::BelongsToReflection)
26
+ if r.klass.respond_to?(:acts_as_having_string_id?) && \
27
+ r.klass.acts_as_having_string_id?
28
+ foreign_attrib_type = ActsAsHavingStringId::StringId::Type.new(r.klass)
29
+ attribute r.foreign_key.to_sym, foreign_attrib_type
30
+ end
21
31
  end
22
32
  end
23
33
 
@@ -1,3 +1,3 @@
1
1
  module ActsAsHavingStringId
2
- VERSION = '0.2.5'
2
+ VERSION = '0.2.6'
3
3
  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.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus Hult
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-28 00:00:00.000000000 Z
11
+ date: 2017-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubyforge_project:
111
- rubygems_version: 2.6.11
111
+ rubygems_version: 2.5.1
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Makes a model accept and expose a seemingly random string id