Class: GraphqlRails::Decorator::RelationDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_rails/decorator/relation_decorator.rb

Overview

wraps active record relation and returns decorated object instead

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(decorator:, relation:, decorator_args: [], decorator_kwargs: {}) ⇒ RelationDecorator

Returns a new instance of RelationDecorator.



15
16
17
18
19
20
# File 'lib/graphql_rails/decorator/relation_decorator.rb', line 15

def initialize(decorator:, relation:, decorator_args: [], decorator_kwargs: {})
  @relation = relation
  @decorator = decorator
  @decorator_args = decorator_args
  @decorator_kwargs = decorator_kwargs
end

Class Method Details

.decorates?(object) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/graphql_rails/decorator/relation_decorator.rb', line 10

def self.decorates?(object)
  (defined?(ActiveRecord) && object.is_a?(ActiveRecord::Relation)) ||
    defined?(Mongoid) && object.is_a?(Mongoid::Criteria)
end

Instance Method Details

#to_aObject



40
41
42
# File 'lib/graphql_rails/decorator/relation_decorator.rb', line 40

def to_a
  @to_a ||= relation.to_a.map { |it| decorator.new(it, *decorator_args, **decorator_kwargs) }
end