Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pragma::Decorator::Association::Expander from pragma
- Loading branch information
1 parent
ca94bd8
commit 45dff1b
Showing
5 changed files
with
107 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pragma | ||
module Decorator | ||
module Association | ||
module Expander | ||
include Adaptor::Loader | ||
register ActiveRecord, Poro | ||
end | ||
end | ||
end | ||
end |
45 changes: 45 additions & 0 deletions
45
lib/pragma/decorator/association/expander/active_record.rb
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pragma | ||
module Decorator | ||
module Association | ||
module Expander | ||
class ActiveRecord < Base | ||
class << self | ||
def supports?(relation) | ||
defined?(::ActiveRecord::Relation) && relation.is_a?(::ActiveRecord::Relation) | ||
end | ||
end | ||
|
||
def include_associations(expands) | ||
relation.includes(validate_associations( | ||
relation.model, | ||
destruct_associations(expands) | ||
)) | ||
end | ||
|
||
private | ||
|
||
def destruct_associations(expands) | ||
associations = {} | ||
|
||
expands.each do |expand| | ||
expand.split('.').inject(associations) do |accumulator, association| | ||
accumulator[association] ||= {} | ||
end | ||
end | ||
|
||
associations | ||
end | ||
|
||
def validate_associations(model, associations) | ||
Hash[associations.map do |(key, value)| | ||
reflection = model.reflect_on_association(key.to_sym) | ||
reflection ? [key, validate_associations(reflection.klass, value)] : [false, false] | ||
end.select { |_, v| v }] | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pragma | ||
module Decorator | ||
module Association | ||
module Expander | ||
class Base | ||
attr_reader :relation | ||
|
||
class << self | ||
def supports?(_relation) | ||
fail NotImplementedError | ||
end | ||
end | ||
|
||
def initialize(relation) | ||
@relation = relation | ||
end | ||
|
||
def include_associations(_expands) | ||
fail NotImplementedError | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pragma | ||
module Decorator | ||
module Association | ||
module Expander | ||
class Poro < Base | ||
class << self | ||
def supports?(_relation) | ||
true | ||
end | ||
end | ||
|
||
def include_associations(_expands) | ||
relation | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |