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)


39
40
41
42
43
44
45
46
# File 'app/models/employee.rb', line 39

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

#ensure_not_referenced_on_destroyObject



35
36
37
# File 'app/models/employee.rb', line 35

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

#nameObject



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

def name
  ret = String.new
  
  ret += first_name if !first_name.nil? and first_name.length > 0
  
  ret += (ret.length > 0) ? " #{last_name}" : last_name if !last_name.nil? and last_name.length > 0
  
  ret
end

#short_nameObject



27
28
29
30
31
32
33
# File 'app/models/employee.rb', line 27

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 ...



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

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