Skip to content

Commit dc3fb4d

Browse files
author
Kyle d'Oliveira
authored
Merge pull request #22 from doliveirakn/rails4-support
Rails4 support
2 parents 20c19c5 + b13d2d4 commit dc3fb4d

4 files changed

Lines changed: 74 additions & 3 deletions

File tree

lib/polymorphic_integer_type.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
require "polymorphic_integer_type/version"
22
require "polymorphic_integer_type/extensions"
33
require "polymorphic_integer_type/mapping"
4-
require "polymorphic_integer_type/polymorphic_array_value_extension"
4+
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("5")
5+
require "polymorphic_integer_type/predicate_builder_extension"
6+
else
7+
require "polymorphic_integer_type/polymorphic_array_value_extension"
8+
end
59

610
module PolymorphicIntegerType; end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module PolymorphicIntegerType
2+
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+
32+
def expand(klass, table, column, value)
33+
queries = []
34+
35+
# Find the foreign key when using queries such as:
36+
# Post.where(author: author)
37+
#
38+
# For polymorphic relationships, find the foreign key and type:
39+
# PriceEstimate.where(estimate_of: treasure)
40+
if klass && reflection = klass._reflect_on_association(column)
41+
base_class = polymorphic_base_class_from_value(value)
42+
43+
if reflection.polymorphic? && base_class
44+
if klass.respond_to?("#{column}_type_mapping")
45+
queries << build(table[reflection.foreign_type], klass.send("#{column}_type_mapping").key(base_class.to_s))
46+
else
47+
queries << build(table[reflection.foreign_type], base_class)
48+
end
49+
end
50+
51+
column = reflection.foreign_key
52+
53+
if base_class
54+
primary_key = reflection.association_primary_key(base_class)
55+
value = convert_value_to_association_ids(value, primary_key)
56+
end
57+
end
58+
59+
queries << build(table[column], value)
60+
queries
61+
end
62+
end
63+
end
64+
65+
ActiveRecord::PredicateBuilder.singleton_class.prepend(PolymorphicIntegerType::PredicateBuilderExtension)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module PolymorphicIntegerType
2-
VERSION = "2.2.0"
2+
VERSION = "2.2.1"
33
end

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)