Class: Filemaker::Model::Relations::HasMany

Inherits:
Proxy
  • Object
show all
Defined in:
lib/filemaker/model/relations/has_many.rb

Instance Attribute Summary

Attributes inherited from Proxy

#options, #owner, #target

Instance Method Summary collapse

Methods inherited from Proxy

init, #method_missing, #respond_to_missing?, #target_class

Constructor Details

#initialize(owner, name, options) ⇒ HasMany

Returns a new instance of HasMany.



7
8
9
10
# File 'lib/filemaker/model/relations/has_many.rb', line 7

def initialize(owner, name, options)
  super(owner, name, options)
  build_target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Filemaker::Model::Relations::Proxy

Instance Method Details

#<<(*args) ⇒ Object Also known as: push

Append a model or array of models to the relation. Will set the owner ID to the children.

Examples:

Append a model

job.applicants << applicant

Array of models

job.applicants << [applicant_a, applicant_b, applicant_c]

Parameters:



42
43
44
45
46
47
48
49
50
# File 'lib/filemaker/model/relations/has_many.rb', line 42

def <<(*args)
  docs = args.flatten
  return concat(docs) if docs.size > 1

  if (doc = docs.first)
    create(doc)
  end
  self
end

#build(attrs = {}) ⇒ Filemaker::Model

Build a single model. The owner will be linked, but the record will not be saved.

Examples:

Append a model

job.applicants.build(name: 'Bob')
job.save

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes for the fields

Returns:



67
68
69
70
71
72
73
# File 'lib/filemaker/model/relations/has_many.rb', line 67

def build(attrs = {})
  # attrs.merge!(owner.identity.name => owner.identity_id) if \
  #   owner.identity_id
  #
  attrs[owner.identity.name] = owner.identity_id if owner.identity_id
  target_class.new(attrs)
end

#create(attrs = {}) ⇒ Filemaker::Model

Same as ‘build`, except that it will be saved automatically.

Returns:



78
79
80
# File 'lib/filemaker/model/relations/has_many.rb', line 78

def create(attrs = {})
  build(attrs).save
end

#final_reference_keyObject



26
27
28
29
30
# File 'lib/filemaker/model/relations/has_many.rb', line 26

def final_reference_key
  target_class.find_field_by_name(source_key).try(:name) ||
    target_class.find_field_by_name(reference_key).try(:name) ||
    target_class.identity.try(:name)
end

#reference_keyObject

If no reference_key, we will use owner’s identity field. If there is no identity, we will…??



14
15
16
# File 'lib/filemaker/model/relations/has_many.rb', line 14

def reference_key
  options.fetch(:reference_key) { owner.identity.name }
end

#reference_valueObject



18
19
20
# File 'lib/filemaker/model/relations/has_many.rb', line 18

def reference_value
  owner.public_send(reference_key.to_sym)
end

#source_keyObject



22
23
24
# File 'lib/filemaker/model/relations/has_many.rb', line 22

def source_key
  options.fetch(:source_key) { nil }
end