Class: Taskrabbit::Task

Inherits:
Smash
  • Object
show all
Defined in:
lib/taskrabbit/task.rb

Constant Summary

Constants included from Transformer

Taskrabbit::Transformer::TIME_TRANSFORMER

Class Attribute Summary collapse

Attributes inherited from Smash

#api, #loaded

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Smash

#[], filtered_options, find, #initialize, #redirect?, #redirect_url, #request, #valid?

Methods included from Association

included

Constructor Details

This class inherits a constructor from Taskrabbit::Smash

Class Attribute Details

.attributes_to_clearObject (readonly)

Returns the value of attribute attributes_to_clear.



34
35
36
# File 'lib/taskrabbit/task.rb', line 34

def attributes_to_clear
  @attributes_to_clear
end

Class Method Details

.all(scope, options = {}) ⇒ Object



36
37
38
# File 'lib/taskrabbit/task.rb', line 36

def all(scope, options = {})
  scope.request('get', scope.association_path(self), Api::collection_transformers[self], options_with_class_includes(options))
end

.create(api, params) ⇒ Object



40
41
42
43
44
# File 'lib/taskrabbit/task.rb', line 40

def create(api, params)
  task = api.tasks.new(params)
  task.save
  task
end

.options_with_class_includes(options = {}) ⇒ Object



46
47
48
# File 'lib/taskrabbit/task.rb', line 46

def options_with_class_includes(options = {})
  options.merge(:extra_query => {:include => {:task => properties.to_a}})
end

Instance Method Details

#clear_update_attributesObject



85
86
87
# File 'lib/taskrabbit/task.rb', line 85

def clear_update_attributes
  delete_if { |k, v| self.class.attributes_to_clear.include?(k.to_sym) }
end

#delete!Object



74
75
76
# File 'lib/taskrabbit/task.rb', line 74

def delete!
  reload('delete', "tasks/#{id.to_s}")
end

#fetchObject



51
52
53
# File 'lib/taskrabbit/task.rb', line 51

def fetch
  reload('get', "tasks/#{id.to_s}") unless id.nil?
end

#new_record?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/taskrabbit/task.rb', line 70

def new_record?
  !id
end

#reload(method, path, options = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/taskrabbit/task.rb', line 78

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

#saveObject



55
56
57
58
59
60
61
# File 'lib/taskrabbit/task.rb', line 55

def save
  if id.nil?
    reload('post', "tasks", :task => self.to_hash)
  else
    reload('put', "tasks/#{id.to_s}", self.to_hash)
  end
end

#update_attributes(attributes) ⇒ Object



63
64
65
66
67
68
# File 'lib/taskrabbit/task.rb', line 63

def update_attributes(attributes)
  attributes.each_pair do |key, value|
    self.send("#{key}=", value)
  end
  save
end