safe_polymorphic 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cdcf7aaf3518f1d4a718593cc9e384e2d867e8bc92a519d0ef192535ed754cbf
4
- data.tar.gz: 0a9b9ab48c6efdda379cfc5956bf6579cf230bcb092c5f98df6225e76815314e
3
+ metadata.gz: 0aaf728e17cf09be9b04b2a3465b43e089b3daeb0c61c69530f7a40cde085037
4
+ data.tar.gz: dcadfc2a11171a2aad8d269b672297606a05175de1200562b003219d2f1bb39e
5
5
  SHA512:
6
- metadata.gz: 4acc62a25aaf5cf4a3739f4578870555b1e85fac29d3058f358f51f0805c1857c8b4b31bf18b3b384c7591b3822e51599164c6e4420500ad7896faec5b6cc099
7
- data.tar.gz: 036dba54db4d711e0e7b8319f383259c38717ac73b196fde6545082cc7168587af2aa429441409eb393eb6232f3af00c75f19412548a9d2636babad9c1c3023e
6
+ metadata.gz: 21ead7903d65fd2cccf30325cd47069702ce44f385d949e45fc0f0d598f313db35d68026127e36433fb270b4d934703289294ba0118767e519151a28620bc473
7
+ data.tar.gz: 4b9cc16deef36d7b430c936a74f79562c0983a32ae500f4c65d6075f289b9d454305e2217fa49fe8b8ba2712cb393e4e128f342dc471cf0b18aa03000908166e
data/CHANGELOG.md CHANGED
@@ -4,3 +4,7 @@
4
4
 
5
5
  - Initial release
6
6
  - Moved from [Belongs to Polymorphic](https://rubygems.org/gems/belongs_to_polymorphic)
7
+
8
+ ## [0.1.1] - 2022-12-28
9
+
10
+ - Fix finder method bug when searching using an instance of a class
data/Gemfile CHANGED
@@ -5,9 +5,9 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in safe_polymorphic.gemspec
6
6
  gemspec
7
7
 
8
- gem 'database_cleaner-active_record', '~> 1.8.0', require: false
8
+ gem 'database_cleaner-active_record', '~> 1.8.0', require: false, group: :test
9
9
  gem 'rake', '~> 13.0'
10
10
  gem 'rspec', '~> 3.0'
11
11
  gem 'rubocop', '~> 1.21'
12
- gem 'simplecov', '~> 0.17.1'
13
- gem 'sqlite3', '~> 1.4.2', require: false
12
+ gem 'simplecov', '~> 0.17.1', require: false, group: :test
13
+ gem 'sqlite3', '~> 1.4.2', require: false, group: :test
data/Gemfile.lock CHANGED
@@ -1,22 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- safe_polymorphic (0.1.0)
4
+ safe_polymorphic (0.1.1)
5
5
  activerecord (>= 5.2, < 7.1)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activemodel (7.0.4)
11
- activesupport (= 7.0.4)
12
- activerecord (7.0.4)
13
- activemodel (= 7.0.4)
14
- activesupport (= 7.0.4)
15
- activesupport (7.0.4)
10
+ activemodel (6.1.4.1)
11
+ activesupport (= 6.1.4.1)
12
+ activerecord (6.1.4.1)
13
+ activemodel (= 6.1.4.1)
14
+ activesupport (= 6.1.4.1)
15
+ activesupport (6.1.4.1)
16
16
  concurrent-ruby (~> 1.0, >= 1.0.2)
17
17
  i18n (>= 1.6, < 2)
18
18
  minitest (>= 5.1)
19
19
  tzinfo (~> 2.0)
20
+ zeitwerk (~> 2.3)
20
21
  ast (2.4.2)
21
22
  concurrent-ruby (1.1.10)
22
23
  database_cleaner (1.8.5)
@@ -71,6 +72,7 @@ GEM
71
72
  tzinfo (2.0.5)
72
73
  concurrent-ruby (~> 1.0)
73
74
  unicode-display_width (2.3.0)
75
+ zeitwerk (2.6.0)
74
76
 
75
77
  PLATFORMS
76
78
  arm64-darwin-21
@@ -86,4 +88,4 @@ DEPENDENCIES
86
88
  sqlite3 (~> 1.4.2)
87
89
 
88
90
  BUNDLED WITH
89
- 2.3.7
91
+ 2.4.1
data/README.md CHANGED
@@ -1,4 +1,8 @@
1
1
  # SafePolymorphic
2
+ [![Gem Version](https://badge.fury.io/rb/safe_polymorphic.svg)](https://badge.fury.io/rb/safe_polymorphic)
3
+ [![Ruby](https://github.com/gogrow-dev/safe_polymorphic/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/gogrow-dev/safe_polymorphic/actions/workflows/main.yml)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/1b0a4b92bbee2be50dc1/maintainability)](https://codeclimate.com/github/gogrow-dev/safe_polymorphic/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/1b0a4b92bbee2be50dc1/test_coverage)](https://codeclimate.com/github/gogrow-dev/safe_polymorphic/test_coverage)
2
6
 
3
7
  An ActiveRecord extension which allows us to safely use polymorphic associations, by validating which classes are allowed to be related to, while also adding some helper methods.
4
8
 
@@ -32,6 +36,14 @@ If we try to set an `owner` from a class rather than the aforementioend ones, it
32
36
  #ActiveRecord::RecordInvalid: Validation failed: Owner type OtherThing class is not an allowed class.
33
37
  ```
34
38
 
39
+ We can also use strings and symbols instead of the classes themselves:
40
+ ```ruby
41
+ class Book < ActiveRecord::Base
42
+ belongs_to :owner, polymorphic: [:user, 'Publisher']
43
+ end
44
+ ```
45
+ Provided that the strings and symbols translate to existing classes when used with `.classify.constantize`.
46
+
35
47
  ### Usage with namespaced models
36
48
 
37
49
  ```ruby
@@ -82,3 +94,10 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
82
94
  ## Code of Conduct
83
95
 
84
96
  Everyone interacting in the SafePolymorphic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/safe_polymorphic/blob/main/CODE_OF_CONDUCT.md).
97
+
98
+ ## Credits
99
+
100
+ belongs_to_polymorphic is maintained by [GoGrow](https://gogrow.dev) with the help of our
101
+ [contributors](https://github.com/gogrow-dev/belongs_to_polymorphic/contributors).
102
+
103
+ [<img src="https://user-images.githubusercontent.com/9309458/180014465-00477428-fd76-48f6-b984-5b401b8ce241.svg" height="50"/>](https://gogrow.dev)
@@ -49,7 +49,7 @@ module SafePolymorphic
49
49
  const_class = case klass
50
50
  when String then klass
51
51
  when Symbol then klass.to_s
52
- else type.class.name
52
+ else klass.class.name
53
53
  end
54
54
  const_class.classify.constantize
55
55
  end
@@ -63,12 +63,8 @@ module SafePolymorphic
63
63
  # Generates a generic finder method
64
64
  def define_generic_finder_method(name)
65
65
  define_singleton_method("with_#{name}") do |type|
66
- type = case type
67
- when Class then type.name
68
- when String then type
69
- else type.class.name
70
- end
71
- where("#{name}_type" => type)
66
+ type = constantize(type) unless type.is_a?(Class)
67
+ where("#{name}_type" => type.name)
72
68
  end
73
69
  end
74
70
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SafePolymorphic
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safe_polymorphic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Erlichman