Class: Filemaker::Model::Relations::HasMany
- Defined in:
- lib/filemaker/model/relations/has_many.rb
Instance Attribute Summary
Attributes inherited from Proxy
Instance Method Summary collapse
-
#<<(*args) ⇒ Object
(also: #push)
Append a model or array of models to the relation.
-
#build(attrs = {}) ⇒ Filemaker::Model
Build a single model.
-
#create(attrs = {}) ⇒ Filemaker::Model
Same as ‘build`, except that it will be saved automatically.
- #final_reference_key ⇒ Object
-
#initialize(owner, name, options) ⇒ HasMany
constructor
A new instance of HasMany.
-
#reference_key ⇒ Object
If no reference_key, we will use owner’s identity field.
- #reference_value ⇒ Object
- #source_key ⇒ Object
Methods inherited from Proxy
#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, ) super(owner, name, ) 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.
42 43 44 45 46 47 48 49 |
# 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.
66 67 68 69 70 71 72 |
# File 'lib/filemaker/model/relations/has_many.rb', line 66 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.
77 78 79 |
# File 'lib/filemaker/model/relations/has_many.rb', line 77 def create(attrs = {}) build(attrs).save end |
#final_reference_key ⇒ Object
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_key ⇒ Object
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 .fetch(:reference_key) { owner.identity.name } end |
#reference_value ⇒ Object
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_key ⇒ Object
22 23 24 |
# File 'lib/filemaker/model/relations/has_many.rb', line 22 def source_key .fetch(:source_key) { nil } end |