class ActiveGraphql::Model::BuildOrRelation

reformats action to default format which very opinionated and based in following assumptions:

github graphql structure was used as inspiration

Attributes

left_scope[R]
right_scope[R]

Public Class Methods

call(*args) click to toggle source
# File lib/active_graphql/model/build_or_relation.rb, line 12
def self.call(*args)
  new(*args).call
end
new(left_scope, right_scope) click to toggle source
# File lib/active_graphql/model/build_or_relation.rb, line 16
def initialize(left_scope, right_scope)
  @left_scope = left_scope
  @right_scope = right_scope
end

Public Instance Methods

call() click to toggle source
# File lib/active_graphql/model/build_or_relation.rb, line 21
def call
  shared_scope.where(or: or_attributes)
end

Private Instance Methods

clean_scope() click to toggle source
# File lib/active_graphql/model/build_or_relation.rb, line 33
def clean_scope
  left_scope.where_attributes.keys.reduce(left_scope) { |final, key| final.unscope(where: key) }
end
left_unique_attributes() click to toggle source
# File lib/active_graphql/model/build_or_relation.rb, line 49
def left_unique_attributes
  left_or_attributes = left_scope.where_attributes[:or] || {}

  unique_attributes = left_scope.where_attributes.except(:or).select do |key, value|
    !shared_attributes.key?(key) || shared_attributes[key] != value
  end

  left_or_attributes.merge(unique_attributes)
end
or_attributes() click to toggle source
# File lib/active_graphql/model/build_or_relation.rb, line 45
def or_attributes
  left_unique_attributes.merge(right_unique_attributes) { |_key, old_value, new_value| [*old_value, new_value] }
end
right_unique_attributes() click to toggle source
# File lib/active_graphql/model/build_or_relation.rb, line 59
def right_unique_attributes
  right_scope.where_attributes.select do |key, value|
    !shared_attributes.key?(key) || shared_attributes[key] != value
  end
end
shared_attributes() click to toggle source
# File lib/active_graphql/model/build_or_relation.rb, line 37
def shared_attributes
  @shared_attributes ||= begin
    left_attributes = left_scope.where_attributes
    right_attributes = right_scope.where_attributes
    left_attributes.select { |key, value| right_attributes.key?(key) && right_attributes[key] == value }
  end
end
shared_scope() click to toggle source
# File lib/active_graphql/model/build_or_relation.rb, line 29
def shared_scope
  @shared_scope ||= clean_scope.where(shared_attributes)
end