Class: INatGet::Data::Helper::Field::Has Private

Inherits:
Scalar show all
Defined in:
lib/inat-get/data/helpers/defs/has.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Direct Known Subclasses

Licenses

Instance Attribute Summary

Attributes inherited from INatGet::Data::Helper::Field

#helper, #key

Instance Method Summary collapse

Methods inherited from Scalar

#valid?

Methods inherited from INatGet::Data::Helper::Field

#prepare, #to_api, #valid?

Constructor Details

#initialize(helper, key, association, extra = nil) ⇒ Has

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Has.



7
8
9
10
11
# File 'lib/inat-get/data/helpers/defs/has.rb', line 7

def initialize helper, key, association, extra = nil
  super helper, key, Boolean
  @association = association
  @extra = extra
end

Instance Method Details

#to_sequel(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/inat-get/data/helpers/defs/has.rb', line 13

def to_sequel(value)
  reflection = @helper.manager.model.association_reflection(@association)
  main_table = @helper.manager.model.table_name
  if reflection[:type] == :many_to_many
    join_table = reflection[:join_table]
    left_key = reflection[:left_key]
    right_key = reflection[:right_key]
    right_table = reflection.associated_class.table_name
    query = @helper.manager.model.db[join_table]
                                 .join(right_table, :id => right_key)
                                 .where(Sequel[join_table][left_key] => Sequel[main_table][:id])
    query = query.where(@extra) if @extra
    condition = query.exists
  else
    associated_class = reflection.associated_class
    foreign_key = reflection[:key]
    query = associated_class.where(foreign_key => Sequel[main_table][:id])
    query = query.where(@extra) if @extra
    condition = query.exists
  end
  value ? condition : Sequel.~(condition)
end