-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for polymorphic associations
- Loading branch information
Showing
19 changed files
with
372 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.6.5 | ||
2.6.6 |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module TableSaw | ||
class Associations | ||
attr_reader :manifest | ||
|
||
def initialize(manifest) | ||
@manifest = manifest | ||
end | ||
|
||
def belongs_to | ||
@belongs_to ||= foreign_keys.each_with_object(Hash.new { |h, k| h[k] = Set.new }) do |fk, memo| | ||
memo[fk.from_table].add(fk) | ||
end | ||
end | ||
|
||
def has_many | ||
@has_many ||= foreign_keys.each_with_object(Hash.new { |h, k| h[k] = Set.new }) do |fk, memo| | ||
memo[fk.to_table].add(fk) | ||
end | ||
end | ||
|
||
private | ||
|
||
def foreign_keys | ||
@foreign_keys ||= manifest_foreign_keys + schema_foreign_keys | ||
end | ||
|
||
def manifest_foreign_keys | ||
manifest.foreign_keys.map do |fk| | ||
TableSaw::ForeignKey.new(from_table: fk['from_table'], from_column: fk['from_column'], | ||
to_table: fk['to_table'], to_column: fk['to_column']) | ||
end | ||
end | ||
|
||
def schema_foreign_keys | ||
TableSaw.information_schema.foreign_key_relationships.foreign_keys | ||
end | ||
end | ||
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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module TableSaw | ||
class ForeignKey | ||
class Column | ||
REGEX = /(\w+)(?::(\w+)\((\w+)\))?/.freeze | ||
|
||
attr_reader :value | ||
|
||
def initialize(value) | ||
@value = value | ||
end | ||
|
||
def primary_key | ||
value[REGEX, 1] | ||
end | ||
|
||
def type_condition | ||
polymorphic? ? "#{type_column} = '#{type_value}'" : '1 = 1' | ||
end | ||
|
||
private | ||
|
||
def type_column | ||
value[REGEX, 2] | ||
end | ||
|
||
def type_value | ||
value[REGEX, 3] | ||
end | ||
|
||
def polymorphic? | ||
!(type_column.nil? || type_value.nil?) | ||
end | ||
end | ||
|
||
attr_reader :name, :from_table, :from_column, :to_table, :to_column | ||
|
||
def initialize(name: nil, from_table:, from_column:, to_table:, to_column:) | ||
@name = name | ||
@from_table = from_table | ||
@from_column = from_column | ||
@to_table = to_table | ||
@to_column = to_column | ||
end | ||
|
||
def type_condition | ||
@type_condition ||= column.type_condition | ||
end | ||
|
||
def column | ||
@column ||= Column.new(from_column) | ||
end | ||
|
||
def eql?(other) | ||
hash == other.hash | ||
end | ||
|
||
def hash | ||
[from_table, from_column, to_table, to_column].hash | ||
end | ||
end | ||
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
Oops, something went wrong.