Class: Osm::FlexiRecord::Data

Inherits:
Model
  • Object
show all
Defined in:
lib/osm/flexi_record.rb

Constant Summary collapse

SORT_BY =
[:flexi_record, :grouping_id, :member_id]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Instance Attribute Details

#fieldsDirtyHashy

Returns Keys are the field’s id, values are the field values.

Returns:

  • (DirtyHashy)

    Keys are the field’s id, values are the field values



252
# File 'lib/osm/flexi_record.rb', line 252

attribute :flexi_record, :type => Object

#flexi_recordBoolean

Returns The FlexiRecord this column belongs to.

Returns:

  • (Boolean)

    The FlexiRecord this column belongs to



252
# File 'lib/osm/flexi_record.rb', line 252

attribute :flexi_record, :type => Object

#grouping__idFixnum

Returns OSM id for the grouping the member is in.

Returns:

  • (Fixnum)

    OSM id for the grouping the member is in



252
# File 'lib/osm/flexi_record.rb', line 252

attribute :flexi_record, :type => Object

#member_idFixnum

Returns OSM id for the member.

Returns:

  • (Fixnum)

    OSM id for the member



252
# File 'lib/osm/flexi_record.rb', line 252

attribute :flexi_record, :type => Object

Instance Method Details

#<=>(another) ⇒ Object

Compare Data based on flexi_record, grouping_id then member_id



324
325
326
327
328
329
# File 'lib/osm/flexi_record.rb', line 324

def <=>(another)
  result = self.flexi_record <=> another.try(:flexi_record)
  result = self.grouping_id <=> another.try(:grouping_id) if result == 0
  result = self.member_id <=> another.try(:member_id) if result == 0
  return result
end

#inspectObject



331
332
333
# File 'lib/osm/flexi_record.rb', line 331

def inspect
  Osm.inspect_instance(self, options={:replace_with => {'flexi_record' => :id}})
end

#update(api) ⇒ Boolean

Update data in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the data was updated in OSM

Raises:



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/osm/flexi_record.rb', line 284

def update(api)
  raise Osm::ObjectIsInvalid, 'data is invalid' unless valid?
  require_ability_to(api, :write, :flexi, flexi_record.section_id)

  term_id = Osm::Term.get_current_term_for_section(api, flexi_record.section_id).id

  updated = true
  editable_fields = flexi_record.get_columns(api).select{ |c| c.editable }.map{ |i| i.id }
  fields.changes.each do |field, (was,now)|
    if editable_fields.include?(field)
      data = api.perform_query("extras.php?action=updateScout", {
        'termid' => term_id,
        'scoutid' => self.member_id,
        'column' => field,
        'value' => now,
        'sectionid' => flexi_record.section_id,
        'extraid' => flexi_record.id,
      })
      if (data.is_a?(Hash) && data['items'].is_a?(Array))
        data['items'].each do |item|
          if item['scoutid'] == member_id.to_s  # Find this member from the list of all members
            updated = false unless item[field] == now
          end
        end
      else
        updated = false
      end
    end
  end

  if updated
    fields.clean_up!
    # The cached datas for the flexi record will be out of date - remove them
    cache_delete(api, ['flexi_record_data', flexi_record.id])
  end

  return updated
end