class Osm::FlexiRecord

Public Instance Methods

<=>(another) click to toggle source

Compare FlexiRecord based on section_id then name

# File lib/osm/flexi_record.rb, line 122
def <=>(another)
  result = self.section_id.to_i <=> another.try(:section_id).to_i
  result = self.name.to_s <=> another.try(:name).to_s if result == 0
  return result
end
add_column(api, name) click to toggle source

Add a column in OSM @param [Osm::Api] api The api to use to make the request @param [String] name The name for the created column @return [Boolean] whether the column was created in OSM

# File lib/osm/flexi_record.rb, line 58
def add_column(api, name)
  require_ability_to(api, :write, :flexi, section_id)
  raise ArgumentError, 'name is invalid' if name.blank?

  data = api.perform_query("extras.php?action=addColumn&sectionid=#{section_id}&extraid=#{id}", {
    'columnName' => name,
  })

  if (data.is_a?(Hash) && data.has_key?('config'))
    ActiveSupport::JSON.decode(data['config']).each do |field|
      if field['name'] == name
        # The cached fields for the flexi record will be out of date - remove them
        cache_delete(api, ['flexi_record_columns', id])
        return true
      end
    end
  end
  return false
end
get_columns(api, options={}) click to toggle source

Get structure for the flexi record @param [Osm::Api] api The api to use to make the request @!macro options_get @return [Array<Osm::FlexiRecordColumn>] representing the columns of the flexi record

# File lib/osm/flexi_record.rb, line 28
def get_columns(api, options={})
  require_ability_to(api, :read, :flexi, section_id, options)
  cache_key = ['flexi_record_columns', self.id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  data = api.perform_query("extras.php?action=getExtra&sectionid=#{self.section_id}&extraid=#{self.id}")

  structure = []
  data['structure'].each do |item|
    item['rows'].each do |row|
      structure.push Osm::FlexiRecord::Column.new(
        :id => row['field'],
        :name => row['name'],
        :editable => row['editable'] || false,
        :flexi_record => self,
      )
    end
  end
  cache_write(api, cache_key, structure)

  return structure
end
get_data(api, term=nil, options={}) click to toggle source

Get data for flexi record @param [Osm::Api] api The api to use to make the request @param [Osm::Term, Fixnum, to_i, nil] term The term (or its ID) to get the register for, passing nil causes the current term to be used @!macro options_get @return [Array<FlexiRecordData>]

# File lib/osm/flexi_record.rb, line 83
def get_data(api, term=nil, options={})
  require_ability_to(api, :read, :flexi, section_id, options)
  section = Osm::Section.get(api, self.section_id)
  term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
  cache_key = ['flexi_record_data', id, term_id]

  if !options[:no_cache] && Osm::Model.cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  data = api.perform_query("extras.php?action=getExtraRecords&sectionid=#{section.id}&extraid=#{id}&termid=#{term_id}&section=#{section.type}")

  to_return = []
  data['items'].each do |item|
    unless item['scoutid'].to_i < 0  # It's a total row
      fields = item.select { |key, value|
        ['firstname', 'lastname', 'dob', 'total', 'completed', 'age'].include?(key) || key.to_s.match(/\Af_\d+\Z/)
      }
      fields.merge!(
        'dob' => item['dob'].empty? ? nil : item['dob'],
        'total' => item['total'].to_s.empty? ? nil : item['total'],
        'completed' => item['completed'].to_s.empty? ? nil : item['completed'],
        'age' => item['age'].empty? ? nil : item['age'],
      )
  
      to_return.push Osm::FlexiRecord::Data.new(
        :member_id => Osm::to_i_or_nil(item['scoutid']),
        :grouping_id => Osm::to_i_or_nil(item['patrolid'].eql?('') ? nil : item['patrolid']),
        :fields => fields,
        :flexi_record => self,
      )
    end
  end

  cache_write(api, cache_key, to_return)
  return to_return
end