Class: Osm::FlexiRecord
Defined Under Namespace
Instance Attribute Summary collapse
-
#id ⇒ Fixnum
The id for the flexi_record.
-
#name ⇒ String
The flexi record’s name name.
-
#section_id ⇒ Fixnum
The section the member belongs to.
Instance Method Summary collapse
-
#<=>(another) ⇒ Object
Compare FlexiRecord based on section_id then name.
-
#add_column(api, name) ⇒ Boolean
Add a column in OSM.
-
#get_columns(api, options = {}) ⇒ Array<Osm::FlexiRecordColumn>
Get structure for the flexi record.
-
#get_data(api, term = nil, options = {}) ⇒ Array<FlexiRecordData>
Get data for flexi record.
Methods inherited from Model
#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i
Instance Attribute Details
#id ⇒ Fixnum
Returns the id for the flexi_record.
11 |
# File 'lib/osm/flexi_record.rb', line 11 attribute :id, :type => Integer |
#name ⇒ String
Returns the flexi record’s name name.
11 |
# File 'lib/osm/flexi_record.rb', line 11 attribute :id, :type => Integer |
#section_id ⇒ Fixnum
Returns 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
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§ionid=#{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
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, ={}) require_ability_to(api, :read, :flexi, section_id, ) cache_key = ['flexi_record_columns', self.id] if ![:no_cache] && Osm::Model.cache_exist?(api, cache_key) return cache_read(api, cache_key) end data = api.perform_query("extras.php?action=getExtra§ionid=#{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
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, ={}) require_ability_to(api, :read, :flexi, section_id, ) 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 ![:no_cache] && Osm::Model.cache_exist?(api, cache_key) return cache_read(api, cache_key) end data = api.perform_query("extras.php?action=getExtraRecords§ionid=#{section.id}&extraid=#{id}&termid=#{term_id}§ion=#{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 |