iron_trail 0.2.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4541270b52cf87af024251d4b442fca74f17bf54c913b12baa2f61267d6aef7f
4
- data.tar.gz: c151fa8780fb9ef8aeb6e8ee0870b315cb2a97a776086ea1653776ef12b7f124
3
+ metadata.gz: 53e65b9972a06bb052bf64095ab551e9da2da8068d2e6dc4f99b212807d00efc
4
+ data.tar.gz: 26d20d4244935be46e0734ca97135a0708bc055cc766d97f34c777f5fa5c4630
5
5
  SHA512:
6
- metadata.gz: 2f81445c9ffac1c12426e5313d62587ec6cd91609fef8658ac23d156e6298453aedf6d43282a999d80eda8807139b35fc7e73491a7b62893a974a9df06950de0
7
- data.tar.gz: 32be78582d5146fb289b4b0a297d64d2d6c912528405d2a16f0111649cd686e39098bce51f9fea335d590d0c96d1cd1b703c552f6e898e504f5a4727976119ce
6
+ metadata.gz: '091d403022eb43516db646a17c96f9a65d1367dca2e845d0ebd7ca4d614edfa774a2fc88bbbbae5d89aaaca134d3ef255408594a0c176805834709257d02ee59'
7
+ data.tar.gz: 8bad5c9401a1bcb21d6504027944ccd8346fcbfdcad6730fc6155468ba0671050027395b293c3219285071b13e4ee71838266b4005bcb0dd06bdee8933f677f6
@@ -24,7 +24,9 @@ module IronTrail
24
24
  metadata = JSON.dump(current_metadata)
25
25
 
26
26
  if metadata.length > METADATA_MAX_LENGTH
27
- Rails.logger.warn("IronTrail metadata is longer than maximum length! #{metadata.length} > #{METADATA_MAX_LENGTH}")
27
+ msg = "IronTrail metadata is longer than maximum length! #{metadata.length} > #{METADATA_MAX_LENGTH}"
28
+ Rails.logger.warn(msg)
29
+ Sentry.capture_message(msg, level: :warning) if defined?(Sentry)
28
30
  next query
29
31
  end
30
32
 
@@ -25,27 +25,32 @@ module IronTrail
25
25
  record
26
26
  end
27
27
 
28
- def self.model_from_table_name(table_name, sti_type=nil)
29
- index = ActiveRecord::Base.descendants.reject(&:abstract_class).chunk(&:table_name).to_h do |key, val|
30
- v = \
31
- if val.length == 1
32
- val[0]
33
- else
34
- val.to_h { |k| [k.to_s, k] }
35
- end
28
+ def self.model_from_table_name(table_name, sti_type = nil)
29
+ candidates = ActiveRecord::Base.descendants
30
+ .reject(&:abstract_class)
31
+ .select { |klass| klass.table_name == table_name }
36
32
 
37
- [key, v]
38
- end
33
+ raise "Cannot infer model from table named '#{table_name}'" if candidates.empty?
34
+ return candidates.first if candidates.one?
39
35
 
40
- klass = index[table_name]
41
- raise "Cannot infer model from table named '#{table_name}'" unless klass
36
+ if sti_type.present?
37
+ klass = candidates.find { |c| c.name == sti_type }
38
+ return klass if klass
39
+
40
+ raise "Cannot infer STI model for table #{table_name} and type '#{sti_type}'"
41
+ end
42
42
 
43
- return klass unless klass.is_a?(Hash)
44
- klass = klass[sti_type]
43
+ # When sti_type is nil and multiple classes share the table,
44
+ # filter out STI subclasses to find the base class.
45
+ bases = candidates.reject { |c| candidates.any? { |other| other != c && c < other } }
46
+ return bases.first if bases.one?
45
47
 
46
- return klass if klass
48
+ # If multiple base classes remain, prefer the one that is an STI base
49
+ # (i.e., has subclasses among the original candidates).
50
+ sti_bases = bases.select { |c| candidates.any? { |other| other != c && other < c } }
51
+ return sti_bases.first if sti_bases.one?
47
52
 
48
- raise "Cannot infer STI model for table #{table_name} and type '#{sti_type}'"
53
+ raise "Cannot infer model from table named '#{table_name}'"
49
54
  end
50
55
  end
51
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_literal_string: true
2
2
 
3
3
  module IronTrail
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron_trail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - André Diego Piske
@@ -166,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
166
  - !ruby/object:Gem::Version
167
167
  version: '0'
168
168
  requirements: []
169
- rubygems_version: 4.0.3
169
+ rubygems_version: 4.0.6
170
170
  specification_version: 4
171
171
  summary: Creates a trail strong as iron
172
172
  test_files: []