class Osm::Grouping

Constants

SORT_BY

Public Class Methods

get_for_section(api, section, options={}) click to toggle source

Get the groupings that a section has @param [Osm::Api] api The api to use to make the request @param [Fixnum] section The section (or its ID) of the section to get groupings for @!macro options_get @return [Array<Osm::Grouping>, nil] An array of groupings or nil if the user can not access that section

# File lib/osm/grouping.rb, line 39
def self.get_for_section(api, section, options={})
  section_id = section.to_i
  require_ability_to(api, :read, :member, section_id)
  cache_key = ['groupings', section_id]

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

  data = api.perform_query("users.php?action=getPatrols&sectionid=#{section_id}")

  result = Array.new
  if data.is_a?(Hash) && data['patrols'].is_a?(Array)
    data['patrols'].each do |item|
      result.push Osm::Grouping.new({
      :id => Osm::to_i_or_nil(item['patrolid']),
      :section_id => section_id,
      :name => item['name'],
      :active => (item['active'] == 1),
      :points => Osm::to_i_or_nil(item['points']),
    })
    end
    cache_write(api, cache_key, result)
  end

  return result
end

Public Instance Methods

update(api) click to toggle source

Update the grouping in OSM @param [Osm::Api] api The api to use to make the request @return [Boolan] whether the member was successfully updated or not @raise [Osm::ObjectIsInvalid] If the Grouping is invalid

# File lib/osm/grouping.rb, line 77
def update(api)
  raise Osm::ObjectIsInvalid, 'grouping is invalid' unless valid?
  require_ability_to(api, :read, :member, section_id)

  to_update = changed_attributes
  result = true

  if to_update.include?('name') || to_update.include?('active')
    data = api.perform_query("users.php?action=editPatrol&sectionid=#{section_id}", {
      'patrolid' => self.id,
      'name' => name,
      'active' => active,
    })
    result &= data.nil?
  end

  if to_update.include?('points')
    data = api.perform_query("users.php?action=updatePatrolPoints&sectionid=#{section_id}", {
      'patrolid' => self.id,
      'points' => points,
    })
    result &= (data == {})
  end

  if result
    reset_changed_attributes
    # The cached groupings for the section will be out of date - remove them
    Osm::Model.cache_delete(api, ['groupings', section_id])
  end

  return result
end