-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from freeletics/fix-ambiguous-column-issue
Fix PG::AmbiguousColumn if we do a join on 2 tables that has the same array_enum col name
- Loading branch information
Showing
9 changed files
with
139 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ | |
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
.tool-versions | ||
.byebug_history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
source "https://rubygems.org" | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in array_enum.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,44 @@ | ||
|
||
lib = File.expand_path("../lib", __FILE__) | ||
lib = File.expand_path('lib', __dir__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require "array_enum/version" | ||
require 'array_enum/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "array_enum" | ||
spec.name = 'array_enum' | ||
spec.version = ArrayEnum::VERSION | ||
spec.authors = ["Wojciech Wnętrzak"] | ||
spec.email = ["w.wnetrzak@gmail.com", "eng@freeletics.com"] | ||
spec.authors = ['Wojciech Wnętrzak'] | ||
spec.email = ['w.wnetrzak@gmail.com', 'eng@freeletics.com'] | ||
|
||
spec.summary = %q{String to integer mapping for PostgreSQL array columns.} | ||
spec.description = %q{Extension for ActiveRecord that adds support for PostgreSQL array columns, mapping string values to integers.} | ||
spec.homepage = "https://github.com/freeletics/array_enum" | ||
spec.license = "MIT" | ||
spec.summary = 'String to integer mapping for PostgreSQL array columns.' | ||
spec.description = 'Extension for ActiveRecord that adds support for PostgreSQL array columns, mapping string values to integers.' | ||
spec.homepage = 'https://github.com/freeletics/array_enum' | ||
spec.license = 'MIT' | ||
|
||
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' | ||
# to allow pushing to a single host or delete this section to allow pushing to any host. | ||
if spec.respond_to?(:metadata) | ||
spec.metadata["homepage_uri"] = spec.homepage | ||
spec.metadata["source_code_uri"] = "https://github.com/freeletics/array_enum" | ||
spec.metadata['homepage_uri'] = spec.homepage | ||
spec.metadata['source_code_uri'] = 'https://github.com/freeletics/array_enum' | ||
else | ||
raise "RubyGems 2.0 or newer is required to protect against " \ | ||
"public gem pushes." | ||
raise 'RubyGems 2.0 or newer is required to protect against ' \ | ||
'public gem pushes.' | ||
end | ||
|
||
# Specify which files should be added to the gem when it is released. | ||
# The `git ls-files -z` loads the files in the RubyGem that have been added into git. | ||
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do | ||
spec.files = Dir.chdir(File.expand_path(__dir__)) do | ||
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } | ||
end | ||
spec.bindir = "exe" | ||
spec.bindir = 'exe' | ||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
spec.require_paths = ["lib"] | ||
spec.require_paths = ['lib'] | ||
|
||
spec.required_ruby_version = ">= 2.3" | ||
spec.required_ruby_version = '>= 3.0' | ||
|
||
spec.add_dependency "activemodel" | ||
spec.add_dependency 'activemodel' | ||
|
||
spec.add_development_dependency "activerecord" | ||
spec.add_development_dependency "pg" | ||
spec.add_development_dependency "bundler", ">= 1.17" | ||
spec.add_development_dependency "rake", ">= 10.0" | ||
spec.add_development_dependency "minitest", ">= 5.0" | ||
spec.add_development_dependency 'activerecord' | ||
spec.add_development_dependency 'bundler', '>= 1.17' | ||
spec.add_development_dependency 'minitest', '>= 5.0' | ||
spec.add_development_dependency 'pg' | ||
spec.add_development_dependency 'rake', '>= 10.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,126 @@ | ||
require "test_helper" | ||
require 'test_helper' | ||
|
||
class ArrayEnumTest < Minitest::Test | ||
def setup | ||
User.delete_all | ||
end | ||
|
||
def test_assigning_enum_values | ||
user = User.new(favourite_colors: ["red", "blue"]) | ||
assert_equal ["red", "blue"], user.favourite_colors | ||
user = User.new(favourite_colors: %w[red blue]) | ||
assert_equal %w[red blue], user.favourite_colors | ||
end | ||
|
||
def test_assigning_enum_values_as_symbols | ||
user = User.new(favourite_colors: [:red, :blue]) | ||
assert_equal ["red", "blue"], user.favourite_colors | ||
user = User.new(favourite_colors: %i[red blue]) | ||
assert_equal %w[red blue], user.favourite_colors | ||
end | ||
|
||
def test_raising_error_on_unknown_value | ||
error = assert_raises(ArgumentError) do | ||
User.new(favourite_colors: ["black"]) | ||
User.new(favourite_colors: ['black']) | ||
end | ||
assert_match(/black is not a valid value for favourite_colors/, error.message) | ||
end | ||
|
||
def test_storing_values_as_integers | ||
user = User.create!(favourite_colors: ["red"]) | ||
user = User.create!(favourite_colors: ['red']) | ||
user.reload | ||
assert_equal "{1}", user.read_attribute_before_type_cast("favourite_colors") | ||
assert_equal '{1}', user.read_attribute_before_type_cast('favourite_colors') | ||
end | ||
|
||
# with_attr scope | ||
def test_quering_db_with_single_matching_value | ||
user = User.create!(favourite_colors: ["red"]) | ||
assert_equal [user], User.with_favourite_colors("red") | ||
user = User.create!(favourite_colors: ['red']) | ||
assert_equal [user], User.with_favourite_colors('red') | ||
end | ||
|
||
def test_quering_db_with_single_matching_symbol_value | ||
user = User.create!(favourite_colors: ["red"]) | ||
user = User.create!(favourite_colors: ['red']) | ||
assert_equal [user], User.with_favourite_colors(:red) | ||
end | ||
|
||
def test_quering_db_by_one_of_matching_value | ||
user = User.create!(favourite_colors: ["red", "blue"]) | ||
assert_equal [user], User.with_favourite_colors("red") | ||
user = User.create!(favourite_colors: %w[red blue]) | ||
assert_equal [user], User.with_favourite_colors('red') | ||
end | ||
|
||
def test_quering_db_by_excluded_value_does_not_return_record | ||
User.create!(favourite_colors: ["red", "blue"]) | ||
assert_equal [], User.with_favourite_colors("green") | ||
User.create!(favourite_colors: %w[red blue]) | ||
assert_equal [], User.with_favourite_colors('green') | ||
end | ||
|
||
def test_quering_db_by_many_values_does_not_return_record_on_excluded_value | ||
User.create!(favourite_colors: ["red", "blue"]) | ||
assert_equal [], User.with_favourite_colors(["red", "green"]) | ||
User.create!(favourite_colors: %w[red blue]) | ||
assert_equal [], User.with_favourite_colors(%w[red green]) | ||
end | ||
|
||
def test_quering_db_only_with_single_matching_value | ||
user = User.create!(favourite_colors: ["red"]) | ||
assert_equal [user], User.only_with_favourite_colors(["red"]) | ||
user = User.create!(favourite_colors: ['red']) | ||
assert_equal [user], User.only_with_favourite_colors(['red']) | ||
end | ||
|
||
def test_quering_db_only_with_single_matching_value_from_many_values_does_not_return_record | ||
User.create!(favourite_colors: ["red", "blue"]) | ||
assert_equal [], User.only_with_favourite_colors(["red"]) | ||
User.create!(favourite_colors: %w[red blue]) | ||
assert_equal [], User.only_with_favourite_colors(['red']) | ||
end | ||
|
||
def test_quering_db_by_non_existing_value_raises_error | ||
User.create!(favourite_colors: ["red", "blue"]) | ||
User.create!(favourite_colors: %w[red blue]) | ||
error = assert_raises(ArgumentError) do | ||
User.with_favourite_colors("black") | ||
User.with_favourite_colors('black') | ||
end | ||
assert_match(/black is not a valid value for favourite_colors/, error.message) | ||
end | ||
|
||
# with_any_of_attr scope | ||
def test_with_any_of_attr_matching_value | ||
user = User.create!(favourite_colors: ["red"]) | ||
assert_equal [user], User.with_any_of_favourite_colors("red") | ||
user = User.create!(favourite_colors: ['red']) | ||
assert_equal [user], User.with_any_of_favourite_colors('red') | ||
end | ||
|
||
def test_with_any_of_attr_matching_symbol_value | ||
user = User.create!(favourite_colors: ["red"]) | ||
user = User.create!(favourite_colors: ['red']) | ||
assert_equal [user], User.with_any_of_favourite_colors(:red) | ||
end | ||
|
||
def test_with_any_of_attr_one_of_matching_value | ||
user = User.create!(favourite_colors: ["red", "blue"]) | ||
assert_equal [user], User.with_any_of_favourite_colors("red") | ||
user = User.create!(favourite_colors: %w[red blue]) | ||
assert_equal [user], User.with_any_of_favourite_colors('red') | ||
end | ||
|
||
def test_with_any_of_attr_excluded_value_does_not_return_record | ||
User.create!(favourite_colors: ["red", "blue"]) | ||
assert_equal [], User.with_any_of_favourite_colors("green") | ||
User.create!(favourite_colors: %w[red blue]) | ||
assert_equal [], User.with_any_of_favourite_colors('green') | ||
end | ||
|
||
def test_with_any_of_attr_many_value_when_single_match | ||
user = User.create!(favourite_colors: ["red", "blue"]) | ||
assert_equal [user], User.with_any_of_favourite_colors(["red", "green"]) | ||
user = User.create!(favourite_colors: %w[red blue]) | ||
assert_equal [user], User.with_any_of_favourite_colors(%w[red green]) | ||
end | ||
|
||
def test_with_any_of_attr_by_non_existing_value_raises_error | ||
User.create!(favourite_colors: ["red", "blue"]) | ||
User.create!(favourite_colors: %w[red blue]) | ||
error = assert_raises(ArgumentError) do | ||
User.with_any_of_favourite_colors("black") | ||
User.with_any_of_favourite_colors('black') | ||
end | ||
assert_match(/black is not a valid value for favourite_colors/, error.message) | ||
end | ||
|
||
|
||
def test_lists_values | ||
assert_equal User.favourite_colors, {"red"=>1, "blue"=>2, "green"=>3} | ||
assert_equal User.favourite_colors, { 'red' => 1, 'blue' => 2, 'green' => 3 } | ||
end | ||
|
||
def test_values_can_be_accessed_indifferently | ||
assert_equal User.favourite_colors[:red], 1 | ||
assert_equal User.favourite_colors[:blue], 2 | ||
assert_equal User.favourite_colors[:green], 3 | ||
assert_equal User.favourite_colors["red"], 1 | ||
assert_equal User.favourite_colors['red'], 1 | ||
end | ||
|
||
def test_user_with_join_table_which_has_same_array_enum_col | ||
user = User.create!(favourite_colors: %w[red blue], | ||
profiles: [Profile.new(slug: 'profile_slug')]) | ||
|
||
assert_equal [user], User.joins(:profiles).with_any_of_favourite_colors(%w[red]) | ||
end | ||
end |
Oops, something went wrong.