/
task.rb
89 lines (77 loc) · 2.61 KB
/
task.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module Taskrabbit
class Task < Smash
property :id
property :name
property :user, :transformer => User
property :runner, :transformer => User
property :runners, :transformer => Api::collection_transformers[User]
property :named_price
property :charge_price
property :cost_in_cents
property :number_runners_to_fill
property :links
property :state_label
property :city_id
property :city, :transformer => City
property :description, :default => ''
property :private_description, :default => ''
property :private_runner, :default => false
property :virtual, :default => false
property :state
property :assignment_type
property :complete_by_time, :transformer => TIME_TRANSFORMER
property :state_changed_at, :transformer => TIME_TRANSFORMER
property :assign_by_time, :transformer => TIME_TRANSFORMER
property :location_visits, :transformer => Api::collection_transformers[Location]
property :offers, :transformer => Api::collection_transformers[Offer]
property :other_locations_attributes
property :uploaded_photos_attributes
property :uploaded_sounds_attributes
@attributes_to_clear = [:other_locations_attributes, :uploaded_photos_attributes, :uploaded_sounds_attributes]
class << self
attr_reader :attributes_to_clear
def all(scope, options = {})
scope.request('get', scope.association_path(self), Api::collection_transformers[self], options_with_class_includes(options))
end
def create(api, params)
task = api.tasks.new(params)
task.save
task
end
def options_with_class_includes(options = {})
options.merge(:extra_query => {:include => {:task => properties.to_a}})
end
end
def fetch
reload('get', "tasks/#{id.to_s}") unless id.nil?
end
def save
if id.nil?
reload('post', "tasks", :task => self.to_hash)
else
reload('put', "tasks/#{id.to_s}", self.to_hash)
end
end
def update_attributes(attributes)
attributes.each_pair do |key, value|
self.send("#{key}=", value)
end
save
end
def new_record?
!id
end
def delete!
reload('delete', "tasks/#{id.to_s}")
end
def reload(method, path, options = {})
options = self.class.options_with_class_includes(options) if method.to_s == 'get'
reloaded_task = super(method, path, options)
clear_update_attributes
reloaded_task
end
def clear_update_attributes
delete_if { |k, v| self.class.attributes_to_clear.include?(k.to_sym) }
end
end
end