Class: Osm::Grouping

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

Constant Summary collapse

SORT_BY =
[:section_id, :name]

Instance Attribute Summary collapse

Class Method 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 Term



# File 'lib/osm/grouping.rb', line 68


Instance Attribute Details

#activeBoolean



17
# File 'lib/osm/grouping.rb', line 17

attribute :id, :type => Integer

#idFixnum



17
# File 'lib/osm/grouping.rb', line 17

attribute :id, :type => Integer

#nameString



17
# File 'lib/osm/grouping.rb', line 17

attribute :id, :type => Integer

#pointsFixnum



17
# File 'lib/osm/grouping.rb', line 17

attribute :id, :type => Integer

#section_idFixnum



17
# File 'lib/osm/grouping.rb', line 17

attribute :id, :type => Integer

Class Method Details

.get_for_section(api, section, options = {}) ⇒ Array<Osm::Grouping>?

Get the groupings that a section has

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

Instance Method Details

#update(api) ⇒ Boolan

Update the grouping in OSM

Raises:



77
78
79
80
81
82
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
# 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