Class: Activity

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ExtensibleObjectHelper, MoneyModelHelper
Defined in:
app/models/activity.rb

Defined Under Namespace

Classes: Adjustment, Labor, Material, Proposal

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MoneyModelHelper

append_features

Methods included from ExtensibleObjectHelper

append_features

Constructor Details

#initialize(*args) ⇒ Activity

Returns a new instance of Activity.



20
21
22
# File 'app/models/activity.rb', line 20

def initialize(*args)
  super(*args)
end

Instance Attribute Details

#dont_validate_type_associationsObject

Returns the value of attribute dont_validate_type_associations.



28
29
30
# File 'app/models/activity.rb', line 28

def dont_validate_type_associations
  @dont_validate_type_associations
end

Instance Method Details

#authorized_for?(options) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
# File 'app/models/activity.rb', line 83

def authorized_for?(options)
  return true unless options.try(:[],:action)
  
  case options[:action].to_s
    when /^(edit|delete)$/
      (is_published?) ? false : true
    else
      true
  end
end

#ensure_not_publishedObject



63
64
65
66
67
68
# File 'app/models/activity.rb', line 63

def ensure_not_published
  if is_published?
    errors.add_to_base "Can't destroy an activity once its invoice is published"
    return false
  end
end

#is_paid?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/activity.rb', line 55

def is_paid?
  (invoice.nil?) ? false : invoice.is_paid?
end

#is_published?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/models/activity.rb', line 59

def is_published?
  (invoice.nil?) ? false : invoice.is_published
end

#labelObject



24
25
26
# File 'app/models/activity.rb', line 24

def label
  (self.activity_type.nil? or self.activity_type.length == 0) ? "Activity" : self.activity_type.capitalize
end

#move_to_invoice!(dest) ⇒ Object

Raises:

  • (StandardError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/activity.rb', line 94

def move_to_invoice!(dest)        
  dest_invoice = (dest.class == Integer) ? Invoice.find(dest) : dest

  raise StandardError, "Can't move an already-published activity." if is_published?
  raise StandardError, "Can't move an activity to an already published invoice." if dest_invoice.try(:is_published?)

  if dest_invoice.nil?
    self.invoice_id =  nil

    # OMG - this took me forever to figure out. It *seems* there's some bug in rails 2.3.8 that's 
    # causing this not to update from the above statement, when we're setting the association to nil
    # Weirder still - this only happens when transactions are enabled. (Such as when we're in test:units!)
    @changed_attributes["invoice_id"] = nil
  else
    self.invoice_id = (dest_invoice.nil?) ? nil : dest_invoice.id
    self.client_id = dest_invoice.client_id
  end

  @dont_validate_type_associations = true
  save!
  @dont_validate_type_associations = false
end

#sub_activityObject



79
80
81
# File 'app/models/activity.rb', line 79

def sub_activity
  send activity_type unless activity_type.nil?
end

#validateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/activity.rb', line 29

def validate
  activity_type_sym = (activity_type.nil? or activity_type.empty?) ? nil : activity_type.to_sym
  
  unless dont_validate_type_associations or !self.class.reflections.has_key?(activity_type_sym)
    type_association = self.send activity_type_sym

    if type_association.nil?
      errors.add activity_type_sym, 'missing'
    else
      type_association.valid?
      type_association.errors.each { |attr,msg| errors.add attr, msg }
    end

  end
end

#validate_on_updateObject



70
71
72
73
74
75
76
77
# File 'app/models/activity.rb', line 70

def validate_on_update
  errors.add_to_base "Activity can't be adjusted once its invoice is published" if ( 
    # If we're published, and someone's trying to change us ....
    is_published? and changed_attributes.length > 0 and 
    # *But* this change isn't the case of an invoice association from nil to (not nil) [this case is cool]:
    !(changed_attributes.length == 1 and changed_attributes.keys.include? "invoice_id" and invoice_id_change[0].nil?)
  )
end