Skip to content

Commit b13d2d4

Browse files
author
Kyle d'Oliveira
committed
Remove a duplicate method and add the original monkey patched code a comment for ease of understanding the change
1 parent 38d0343 commit b13d2d4

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

lib/polymorphic_integer_type/extensions.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ def belongs_to(name, scope = nil, **options)
2424
mapping
2525
end
2626

27-
# Required way to dynamically define a class method on the model
28-
singleton_class.__send__(:define_method, "#{foreign_type}_mapping") do
29-
mapping
30-
end
31-
3227
define_method foreign_type do
3328
t = super()
3429
mapping[t]

lib/polymorphic_integer_type/predicate_builder_extension.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
module PolymorphicIntegerType
22
module PredicateBuilderExtension
3+
4+
# Original Code:
5+
# def self.expand(klass, table, column, value)
6+
# queries = []
7+
8+
# # Find the foreign key when using queries such as:
9+
# # Post.where(author: author)
10+
# #
11+
# # For polymorphic relationships, find the foreign key and type:
12+
# # PriceEstimate.where(estimate_of: treasure)
13+
# if klass && reflection = klass._reflect_on_association(column)
14+
# base_class = polymorphic_base_class_from_value(value)
15+
16+
# if reflection.polymorphic? && base_class
17+
# queries << build(table[reflection.foreign_type], base_class)
18+
# end
19+
20+
# column = reflection.foreign_key
21+
22+
# if base_class
23+
# primary_key = reflection.association_primary_key(base_class)
24+
# value = convert_value_to_association_ids(value, primary_key)
25+
# end
26+
# end
27+
28+
# queries << build(table[column], value)
29+
# queries
30+
# end
31+
332
def expand(klass, table, column, value)
433
queries = []
534

spec/spec_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
config.before(:suite) do
1414
database_config = YAML.load(File.open("#{File.dirname(__FILE__)}/support/database.yml"))
1515
ActiveRecord::Base.establish_connection(database_config)
16-
ActiveRecord::MigrationContext.new("#{File.dirname(__FILE__)}/support/migrations").migrate
16+
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("5")
17+
ActiveRecord::MigrationContext.new("#{File.dirname(__FILE__)}/support/migrations").migrate
18+
end
1719
end
1820

1921
config.around do |example|

0 commit comments

Comments
 (0)