Class: Osm::FlexiRecord

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

Defined Under Namespace

Classes: Column, Data

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

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

Instance Attribute Details

#idFixnum

Returns the id for the flexi_record.

Returns:

  • (Fixnum)

    the id for the flexi_record


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

attribute :id, :type => Integer

#nameString

Returns the flexi record's name name.

Returns:

  • (String)

    the flexi record's name name


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

attribute :id, :type => Integer

#section_idFixnum

Returns the section the member belongs to.

Returns:

  • (Fixnum)

    the section the member belongs to


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

attribute :id, :type => Integer

Instance Method Details

#<=>(another) ⇒ Object

Compare FlexiRecord based on section_id then name


122
123
124
125
126
# 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) ⇒ Boolean

Add a column in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • name (String)

    The name for the created column

Returns:

  • (Boolean)

    whether the column was created in OSM

Raises:

  • (ArgumentError)

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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 = {}) ⇒ Array<Osm::FlexiRecordColumn>

Get structure for the flexi record

Parameters:

  • api (Osm::Api)

    The api to use to make the request

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

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:

  • (Array<Osm::FlexiRecordColumn>)

    representing the columns of the flexi record


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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 = {}) ⇒ Array<FlexiRecordData>

Get data for flexi record

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • term (Osm::Term, Fixnum, #to_i, nil) (defaults to: nil)

    The term (or its ID) to get the register for, passing nil causes the current term to be used

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

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:

  • (Array<FlexiRecordData>)

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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