Class: Osm::FlexiRecord::Column

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

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

Constructor Details

#initializeObject

Initialize a new FlexiRecord::Column

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)


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

Instance Attribute Details

#editableBoolean

Returns Wether the field can be edited.

Returns:

  • (Boolean)

    Wether the field can be edited


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

attribute :flexi_record, :type => Object

#flexi_recordBoolean

Returns The FlexiRecord this column belongs to.

Returns:

  • (Boolean)

    The FlexiRecord this column belongs to


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

attribute :flexi_record, :type => Object

#idString

Returns OSM identifier for the field. Special ones are 'dob', 'total', 'completed', 'age', 'firstname' and 'lastname', user ones are of the format 'f_NUMBER'.

Returns:

  • (String)

    OSM identifier for the field. Special ones are 'dob', 'total', 'completed', 'age', 'firstname' and 'lastname', user ones are of the format 'f_NUMBER'


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

attribute :flexi_record, :type => Object

#nameString

Returns Human readable name for the field.

Returns:

  • (String)

    Human readable name for the field


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

attribute :flexi_record, :type => Object

Instance Method Details

#<=>(another) ⇒ Object

Compare Column based on flexi_record then id


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/osm/flexi_record.rb', line 214

def <=>(another)
  result = self.flexi_record <=> another.try(:flexi_record)
  if result == 0
    if id.match(/\Af_\d+\Z/)
      # This is a user column
      unless another.try(:id).to_s.match(/\Af_\d+\Z/)
        return 1
      end
    else
      # This is a system column
      if another.try(:id).to_s.match(/\Af_\d+\Z/)
        return -1
      end
    end
    result = self.id <=> another.try(:id)
  end
  return result
end

#delete(api) ⇒ Boolean

Delete a column in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the column was deleted from OSM

Raises:


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/osm/flexi_record.rb', line 191

def delete(api)
  require_ability_to(api, :write, :flexi, flexi_record.section_id)
  raise Osm::Forbidden, 'this column is not editable' unless self.editable

  data = api.perform_query("extras.php?action=deleteColumn&sectionid=#{flexi_record.section_id}&extraid=#{flexi_record.id}", {
    'columnId' => self.id,
  })

  if (data.is_a?(Hash) && data.has_key?('config'))
    ActiveSupport::JSON.decode(data['config']).each do |f|
      if f['id'] == self.id
        # It wasn't deleted
        return false
      end
    end
  end

  # The cached columns for the flexi record will be out of date - remove them
  cache_delete(api, ['flexi_record_columns', flexi_record.id])
  return true
end

#inspectObject


233
234
235
# File 'lib/osm/flexi_record.rb', line 233

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

#update(api) ⇒ Boolean

Update a column in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the column was updated in OSM

Raises:


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/osm/flexi_record.rb', line 164

def update(api)
  raise Osm::ObjectIsInvalid, 'column is invalid' unless valid?
  require_ability_to(api, :write, :flexi, flexi_record.section_id)
  raise Osm::Forbidden, 'this column is not editable' unless self.editable

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

  if (data.is_a?(Hash) && data.has_key?('config'))
    ActiveSupport::JSON.decode(data['config']).each do |f|
      if (f['id'] == self.id) && (f['name'] == self.name)
        reset_changed_attributes
        # The cached columns for the flexi record will be out of date - remove them
        cache_delete(api, ['flexi_record_columns', flexi_record.id])
        return true
      end
    end
  end
  return false
end