Skip to content

Commit 20c19c5

Browse files
author
Kyle d'Oliveira
authored
Merge pull request #21 from doliveirakn/find-by-bug
Find by bug
2 parents e126048 + 0606c1f commit 20c19c5

14 files changed

Lines changed: 52 additions & 17 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ spec/support/active_record.rb
1717
test/tmp
1818
test/version_tmp
1919
tmp
20+
.byebug_history
21+
polymorphic_integer_type_test

lib/polymorphic_integer_type.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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"
45

56
module PolymorphicIntegerType; end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module PolymorphicIntegerType
2+
module PolymorphicArrayValueExtension
3+
def type_to_ids_mapping
4+
super.tap do |result|
5+
association = @associated_table.send(:association)
6+
klass = association.active_record
7+
name = association.name
8+
9+
if klass.respond_to?("#{name}_type_mapping")
10+
result.transform_keys! do |key|
11+
klass.send("#{name}_type_mapping").key(key)
12+
end
13+
end
14+
result
15+
end
16+
end
17+
end
18+
end
19+
20+
ActiveRecord::PredicateBuilder::PolymorphicArrayValue.prepend(PolymorphicIntegerType::PolymorphicArrayValueExtension)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module PolymorphicIntegerType
2-
VERSION = "2.1.0"
2+
VERSION = "2.2.0"
33
end

polymorphic_integer_type.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
1818
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
1919
spec.require_paths = ["lib"]
2020

21-
spec.add_development_dependency "bundler", "~> 1.2"
21+
spec.add_dependency "activerecord"
22+
spec.add_development_dependency "bundler"
2223
spec.add_development_dependency "rake"
2324
spec.add_development_dependency "rspec"
24-
spec.add_development_dependency "activerecord", "4.2.0"
25-
spec.add_development_dependency "mysql2", "0.3.16"
25+
spec.add_development_dependency "sqlite3"
2626
spec.add_development_dependency "byebug"
2727
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
end
2525
end
2626

27+
context "when querying the associations" do
28+
let(:source) { cat }
29+
let(:target) { nil }
30+
it "properly finds the object with a where" do
31+
expect(Link.where(source: source, id: link.id).first).to eql link
32+
end
33+
it "properly finds the object with a find_by" do
34+
expect(Link.find_by(source: source, id: link.id)).to eql link
35+
end
36+
end
37+
2738
shared_examples "proper source" do
2839
it "should have the proper id, type and object for the source" do
2940
expect(link.source_id).to eql source.id
@@ -47,12 +58,12 @@
4758

4859
context "and the link is accessed through the associations" do
4960
before { link }
50-
61+
5162
it "should have the proper source" do
5263
expect(source.source_links[0].source).to eql source
5364
end
5465
end
55-
66+
5667
end
5768
context "When a link is given polymorphic record" do
5869
let(:link) { Link.create(source: source) }
@@ -137,7 +148,7 @@
137148

138149

139150
end
140-
151+
141152
context "when the association is an STI table" do
142153
let(:link) { Link.create(source: source, target: whiskey) }
143154
let(:source) { Dog.create(name: "Bela", kind: "Dog", owner: owner) }
@@ -147,7 +158,7 @@
147158
expect(link.source).to eql source
148159
end
149160
end
150-
161+
151162
context "when mapping is given inline in the belongs_to model" do
152163
class InlineLink < ActiveRecord::Base
153164
include PolymorphicIntegerType::Extensions
@@ -187,7 +198,7 @@ class InlineDrink < ActiveRecord::Base
187198
include_examples "proper target"
188199

189200
it "creates foreign_type mapping method" do
190-
expect(Link.source_type_mapping).to eq({0 => "Person", 1 => "Animal"})
201+
expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal"})
191202
expect(InlineLink.source_type_mapping).to eq({10 => "Person", 11 => "InlineAnimal"})
192203
end
193204

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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
1617
end
1718

1819
config.around do |example|

spec/support/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
PolymorphicIntegerType::Mapping.configuration do |config|
2-
config.add :source, {0 => "Person", 1 => "Animal"}
3-
config.add :target, {0 => "Food", 1 => "Drink"}
2+
config.add :source, {1 => "Person", 2 => "Animal"}
3+
config.add :target, {1 => "Food", 2 => "Drink"}
44
end

spec/support/database.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
:adapter: mysql2
2+
:adapter: sqlite3
33
:host: localhost
44
:database: polymorphic_integer_type_test
55
:username: root

spec/support/migrations/1_create_link_table.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class CreateLinkTable < ActiveRecord::Migration
1+
class CreateLinkTable < ActiveRecord::Migration[5.2]
22

33
def up
44
create_table :links do |t|

0 commit comments

Comments
 (0)