Class: Trophonius::Record

Inherits:
Hash
  • Object
show all
Includes:
Translator
Defined in:
lib/record.rb

Overview

This class will hold a singular record

A Record is contained in a RecordSet and has methods to retrieve data from the fields inside the Record-hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Translator

#methodize_field, #methodize_portal_field, #portal_relation_name

Constructor Details

#initialize(fm_record = {}, model = '') ⇒ Record

Initializes a new Record



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/record.rb', line 21

def initialize(fm_record = {}, model = '')
  @modifiable_fields = {}
  @modified_fields = {}
  @model_name = model
  @model = model_name.instance_of?(String) ? constantize_model(model_name) : model_name
  @layout_name = @model.layout_name
  @portals = []
  define_field_methods(fm_record) if fm_record.present?
  define_portal_methods(fm_record) if fm_record.present?
  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/record.rb', line 43

def method_missing(method, *args, &block)
  if ActiveSupport::Inflector.pluralize(method).to_s == method.to_s
    result = find_has_many_relation(method)
    return result if result
  elsif constantize_model(method).respond_to?('first')
    result = find_belongs_to_relation(method)
    return result if result
  end

  super
rescue NameError => e
  if e.message.include?('constant')
    Error.throw_error('102', e.message.split(' ')[-1], layout_name)
  else
    raise e
  end
end

Instance Attribute Details

#layout_nameObject

Returns the value of attribute layout_name.



17
18
19
# File 'lib/record.rb', line 17

def layout_name
  @layout_name
end

#model_nameObject

Returns the value of attribute model_name.



17
18
19
# File 'lib/record.rb', line 17

def model_name
  @model_name
end

#modifiable_fieldsObject

Returns the value of attribute modifiable_fields.



17
18
19
# File 'lib/record.rb', line 17

def modifiable_fields
  @modifiable_fields
end

#modified_fieldsObject

Returns the value of attribute modified_fields.



17
18
19
# File 'lib/record.rb', line 17

def modified_fields
  @modified_fields
end

#record_idObject

Returns the value of attribute record_id.



17
18
19
# File 'lib/record.rb', line 17

def record_id
  @record_id
end

Instance Method Details

#[]=(field, new_val) ⇒ Object



37
38
39
40
41
# File 'lib/record.rb', line 37

def []=(field, new_val)
  modifiable_fields[field] = new_val
  modified_fields[field] = new_val
  super
end

#deleteTrue

Deletes the corresponding record from FileMaker Throws a FileMaker error if save failed

Returns:

  • (True)

    if successful



117
118
119
120
121
122
123
124
# File 'lib/record.rb', line 117

def delete
  url = "layouts/#{layout_name}/records/#{record_id}"

  @model.run_before_destroy
  response = DatabaseRequest.make_request(url, 'delete', '{}')
  @model.run_after_destroy
  response['messages'][0]['code'] == '0' ? true : Error.throw_error(response['messages'][0]['code'])
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/record.rb', line 61

def respond_to_missing?(method, include_private = false)
  if ActiveSupport::Inflector.pluralize(method).to_s == method.to_s
    target_model = constantize_model(method)
    return true if target_model.belongs_to_relations[parameterize_name(model_name)]
  else
    target_model = constantize_model(method)
    relation_key = parameterize_name(ActiveSupport::Inflector.pluralize(model_name))
    return true if target_model.has_many_relations[relation_key]
  end
  super
rescue NameError
  super
end

#run_script(script: '', scriptparameter: '') ⇒ String

Runs a FileMaker script from the context of the Model.

Parameters:

  • script: (String) (defaults to: '')

    the FileMaker script to run

  • scriptparameter: (String) (defaults to: '')

    the parameter required by the FileMaker script

Returns:

  • (String)

    : string representing the script result returned by FileMaker



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/record.rb', line 83

def run_script(script: '', scriptparameter: '')
  url = "layouts/#{layout_name}/records/#{record_id}?script=#{script}&script.param=#{scriptparameter}"
  result = DatabaseRequest.make_request(url, 'get', '{}')

  if result['messages'][0]['code'] != '0'
    Error.throw_error(result['messages'][0]['code'])
  elsif result['response']['scriptResult'] == '403'
    Error.throw_error(403)
  else
    ret_val = result['response']['scriptResult']
    ret_val || true
  end
end

#saveTrue

Saves the last changes made to the Record to FileMaker. Throws a FileMaker error if save failed

Returns:

  • (True)

    if successful



102
103
104
105
106
107
108
109
110
# File 'lib/record.rb', line 102

def save
  url = "layouts/#{layout_name}/records/#{record_id}"

  @model.run_before_update
  body = "{\"fieldData\": #{modified_fields.to_json}}"
  response = DatabaseRequest.make_request(url, 'patch', body)
  @model.run_after_update
  response['messages'][0]['code'] == '0' ? true : Error.throw_error(response['messages'][0]['code'])
end

#to_paramObject



33
34
35
# File 'lib/record.rb', line 33

def to_param
  record_id.to_s
end

#update(field_data, portal_data: {}) ⇒ True

Changes and saves the corresponding record in FileMaker Throws a FileMaker error if save failed

Parameters:

  • field_data: (Hash)

    Fields to be changed and data to fill the fields with

Returns:

  • (True)

    if successful



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
# File 'lib/record.rb', line 133

def update(field_data, portal_data: {})
  url = "layouts/#{layout_name}/records/#{record_id}"
  differences = calculate_differences_before_update(field_data, portal_data)
  puts differences
  puts differences.all? { |diff| diff.length.zero? }
  return if differences.all? { |diff| diff.length.zero? }

  field_data.each_key { |field| modifiable_fields[field] = field_data[field] }
  field_data.transform_keys! { |k| (@model.translations[k.to_s] || k).to_s }
  @model.run_before_update

  portal_data.each do |portal_name, values|
    values.map do |record|
      record.transform_keys! do |k|
        if k.to_s.downcase.include?('id') && k.to_s.downcase.include?('record')
          'recordId'
        else
          "#{portal_name}::#{k}"
        end
      end
    end
  end

  body = { fieldData: field_data }
  body.merge!({ portalData: portal_data }) if portal_data.present?

  response = DatabaseRequest.make_request(url, 'patch', body.to_json)
  code = response['messages'][0]['code']

  return throw_field_missing(field_data) if code == '102'
  return Error.throw_error(code) if code != '0'

  @model.run_after_update
  true
end

#upload(container_name:, file:, container_repetition: 1) ⇒ True

Uploads a file to a container field of the record Throws a FileMaker error if upload failed

Parameters:

  • container_name: (String)

    Case sensitive name of the container field on the layout

  • container_repetition: (Integer) (defaults to: 1)

    Number of the repetition of the container field to set

  • file: (Tempfile or File)

    File to upload

Returns:

  • (True)

    if successful



178
179
180
181
182
183
# File 'lib/record.rb', line 178

def upload(container_name:, file:, container_repetition: 1)
  url = "layouts/#{layout_name}/records/#{record_id}/containers/#{container_name}/#{container_repetition}"

  response = DatabaseRequest.upload_file_request(url, file)
  response['messages'][0]['code'] == '0' ? true : Error.throw_error(response['messages'][0]['code'])
end