Module: Graphoid::ActiveRecordDriver

Defined in:
lib/graphoid/drivers/active_record.rb

Class Method Summary collapse

Class Method Details

.belongs_to?(type) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/graphoid/drivers/active_record.rb', line 16

def belongs_to?(type)
  type == ActiveRecord::Reflection::BelongsToReflection
end

.class_of(relation) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/graphoid/drivers/active_record.rb', line 54

def class_of(relation)
  {
    ActiveRecord::Reflection::HasAndBelongsToManyReflection => ManyToMany,
    ActiveRecord::Reflection::BelongsToReflection => BelongsTo,
    ActiveRecord::Reflection::ThroughReflection => ManyToMany,
    ActiveRecord::Reflection::HasManyReflection => HasMany,
    ActiveRecord::Reflection::HasOneReflection => HasOne
  }[relation.class] || Relation
end

.eager_load(_selection, model) ⇒ Object



84
85
86
# File 'lib/graphoid/drivers/active_record.rb', line 84

def eager_load(_selection, model)
  model
end

.embedded_in?(type) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/graphoid/drivers/active_record.rb', line 32

def embedded_in?(type)
  false
end

.embeds_many?(type) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/graphoid/drivers/active_record.rb', line 28

def embeds_many?(type)
  false
end

.embeds_one?(type) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/graphoid/drivers/active_record.rb', line 24

def embeds_one?(type)
  false
end

.execute_and(scope, parsed) ⇒ Object



88
89
90
# File 'lib/graphoid/drivers/active_record.rb', line 88

def execute_and(scope, parsed)
  scope.where(parsed)
end

.execute_or(scope, list) ⇒ Object



92
93
94
95
96
97
# File 'lib/graphoid/drivers/active_record.rb', line 92

def execute_or(scope, list)
  list.map! do |object|
    Graphoid::Queries::Processor.execute(scope, object)
  end
  list.reduce(:or)
end

.fields_of(model) ⇒ Object



68
69
70
# File 'lib/graphoid/drivers/active_record.rb', line 68

def fields_of(model)
  model.columns
end

.has_and_belongs_to_many?(type) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/graphoid/drivers/active_record.rb', line 8

def has_and_belongs_to_many?(type)
  type == ActiveRecord::Reflection::HasAndBelongsToManyReflection
end

.has_many?(type) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/graphoid/drivers/active_record.rb', line 12

def has_many?(type)
  type == ActiveRecord::Reflection::HasManyReflection
end

.has_one?(type) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/graphoid/drivers/active_record.rb', line 20

def has_one?(type)
  type == ActiveRecord::Reflection::HasOneReflection
end

.inverse_name_of(relation) ⇒ Object



64
65
66
# File 'lib/graphoid/drivers/active_record.rb', line 64

def inverse_name_of(relation)
  relation.inverse_of&.class_name&.underscore
end

.parse(attribute, value, operator) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/graphoid/drivers/active_record.rb', line 99

def parse(attribute, value, operator)
  field = attribute.name
  case operator
  when "not"
    parsed = *["#{field} != ?", value]
    parsed = *["#{field} not like ?", "#{value}"] if attribute.type == :string
    parsed = *["#{field} is not null"] if value.nil?
  when "contains", "regex"
    parsed = *["#{field} like ?", "%#{value}%"]
  when "gt", "gte", "lt", "lte", "not", "in", "nin"
    operator = { gt: ">", gte: ">=", lt: "<", lte: "<=", in: "in", nin: "not in" }[operator.to_sym]
    parsed = *["#{field} #{operator} (?)", value]
  else
    parsed = *["#{field} = ?", value]
  end
  parsed
end

.relate_many(scope, relation, value, operator) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/graphoid/drivers/active_record.rb', line 128

def relate_many(scope, relation, value, operator)
  parsed = {}
  field_name = relation.inverse_name || scope.name.underscore
  target = Graphoid::Queries::Processor.execute(relation.klass, value).to_a

  if relation.many_to_many?
    field_name = field_name.to_s.singularize + "_ids"
    ids = target.map(&(field_name.to_sym))
    ids.flatten!.uniq!
  else
    field_name = :"#{field_name}_id"
    ids = target.map(&(field_name))
  end

  if operator == "none"
    parsed = *["id not in (?)", ids] if ids.present?
  elsif operator == "some"
    parsed = *["id in (?)", ids]
  elsif operator == "every"

    # the following process is a SQL division
    # the amount of queries it executes is on per row
    # it is the same than doing an iteration process
    # that iteration process would work in mongoid too

    # TODO: check and fix this query for many to many relations

    plural_name = relation.name.pluralize
    conditions = value.map do |_key, _value|
      operation = Operation.new(relation.klass, _key, _value)
      parsed = parse(operation.operand, operation.value, operation.operator)
      val = parsed.last.is_a?(String) ? "'#{parsed.last}'" : parsed.last
      parsed = parsed.first.sub("?", val)
      " AND #{parsed}"
    end.join

    query = "
              SELECT count(id) as total, #{field_name}
              FROM #{plural_name} A
              GROUP BY #{field_name}
              HAVING total = (
                SELECT count(id)
                FROM #{plural_name} B
                WHERE B.#{field_name} = A.#{field_name}
                #{conditions}
              )
            "
    result = ActiveRecord::Base.connection.execute(query)
    ids = result.map{ |row| row["#{field_name}"] }

    parsed = *["id in (?)", ids]
  end

  parsed
end

.relate_through(scope, relation, value) ⇒ Object

TODO: fix this as it is unused



118
119
120
121
122
123
124
125
126
# File 'lib/graphoid/drivers/active_record.rb', line 118

def relate_through(scope, relation, value)
  # if relation.has_one_through?
  #   ids = Graphoid::Queries::Processor.execute(relation.klass, value).to_a.map(&:id)
  #   through = relation.source.options[:through].to_s.camelize.constantize
  #   ids = through.where(id: ids)
  #   ids = Graphoid::Queries::Processor.execute(relation.klass, value).to_a.map(&:id)
  #   parsed = *["#{field.underscore}_id in (?)", ids]
  # end
end

.relation_type(relation) ⇒ Object



80
81
82
# File 'lib/graphoid/drivers/active_record.rb', line 80

def relation_type(relation)
  relation.class
end

.relations_of(model) ⇒ Object



72
73
74
# File 'lib/graphoid/drivers/active_record.rb', line 72

def relations_of(model)
  model.reflections
end

.skip(result, skip) ⇒ Object



76
77
78
# File 'lib/graphoid/drivers/active_record.rb', line 76

def skip(result, skip)
  result.offset(skip)
end

.through?(type) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/graphoid/drivers/active_record.rb', line 4

def through?(type)
  type == ActiveRecord::Reflection::ThroughReflection
end

.types_map ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/graphoid/drivers/active_record.rb', line 36

def types_map
  {
    binary:  GraphQL::Types::Boolean,
    boolean: GraphQL::Types::Boolean,
    float:   GraphQL::Types::Float,
    integer: GraphQL::Types::Int,
    string:  GraphQL::Types::String,

    datetime:   Graphoid::Scalars::DateTime,
    date:       Graphoid::Scalars::DateTime,
    time:       Graphoid::Scalars::DateTime,
    timestamp:  Graphoid::Scalars::DateTime,
    text:       Graphoid::Scalars::Text,
    bigint:     Graphoid::Scalars::BigInt,
    decimal:    Graphoid::Scalars::Decimal
  }
end