Class: Employee

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ExtensibleObjectHelper, HasCredentialModelHelper, IsActiveModelHelper
Defined in:
app/models/employee.rb

Defined Under Namespace

Classes: Slimtimer

Instance Method Summary collapse

Methods included from HasCredentialModelHelper

append_features, #credential_after_save, #validate_credential_fields

Methods included from IsActiveModelHelper

append_features, #initialize

Methods included from ExtensibleObjectHelper

append_features

Instance Method Details

#authorized_for?(options) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'app/models/employee.rb', line 35

def authorized_for?(options)
  return true unless options.try(:[],:action)
  
  case options[:action].to_sym
    when :delete
      ( Activity::Labor.count( :all, :conditions => ['employee_id = ?', id] ) > 0 ) ? false : true
    else
      true
  end 
end

#ensure_not_referenced_on_destroyObject



31
32
33
# File 'app/models/employee.rb', line 31

def ensure_not_referenced_on_destroy
  ( errors.add_to_base "Can't destroy a referenced employee" and return false ) unless authorized_for? :action => :delete
end

#nameObject



17
18
19
20
21
# File 'app/models/employee.rb', line 17

def name
  [
    first_name, last_name
  ].find_all{|x| x.try(:length).try(:>,0)}.join(' ')
end

#short_nameObject



23
24
25
26
27
28
29
# File 'app/models/employee.rb', line 23

def short_name
  if first_name.length > 0 and last_name.length > 0
    (first_name[0...1] + last_name).downcase
  else
    (first_name.length > 0) ? first_name.downcase : last_name.downcase
  end
end

#with_unsaved_associatedObject

There were some issues in rails 2.3.2 that caused associations (slimtimer/credential/etc) to not save without this hack we may have to do it for all active record objects in the project …



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/employee.rb', line 48

def with_unsaved_associated
  associations_for_update.all? do |association|
    association_proxy = instance_variable_get("@#{association.name}")

    if association_proxy
      records = association_proxy

      records = [records] unless records.is_a? Array # convert singular associations into collections for ease of use

      records.select {|r| r.changed? and not r.readonly?}.all?{|r| yield r} # must use select instead of find_all, which Rails overrides on association proxies for db access
    else
      true
    end

    association_proxy
  end
end