Module: CheckOut

Extended by:
ActiveSupport::Concern
Defined in:
lib/check_out.rb

Defined Under Namespace

Modules: ClassMethods Classes: AlreadyCheckedOutError, NotYoursError

Instance Method Summary collapse

Instance Method Details

#checked_out?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/check_out.rb', line 81

def checked_out?
  checked_out_at?
end

#checked_out_to?(user) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/check_out.rb', line 75

def checked_out_to?(user)
  checked_out? &&
  checked_out_by_user_type == user.class.name &&
  checked_out_by_user_id == user.id
end

#checkout(check_out_to_user) ⇒ Object Also known as: check_out, checkout_to



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/check_out.rb', line 85

def checkout(check_out_to_user)
  changes = {}

  if checked_out? && checked_out_by_user_id != check_out_to_user.id
    raise AlreadyCheckedOutError, "#{self.class} #{self.id} is already checked out by #{checked_out_by_user_type} #{checked_out_by_user_id}"
  elsif checked_out? && checked_out_by_user_id == check_out_to_user.id
    changes[:checked_out_at] = 
      write_attribute(:checked_out_at, current_time_from_proper_timezone)
  else
    changes[:checked_out_by_user_id] =
      write_attribute(:checked_out_by_user_id, check_out_to_user.id)
    changes[:checked_out_by_user_type] =
      write_attribute(:checked_out_by_user_type, check_out_to_user.class.name)
    changes[:checked_out_at] =
      write_attribute(:checked_out_at, current_time_from_proper_timezone)
  end

  self.class.unscoped do
    self.class.update_all changes, { id: self.id }
  end
end

#release(releasing_user = nil) ⇒ Object Also known as: release_from



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/check_out.rb', line 109

def release(releasing_user=nil)
  if releasing_user && releasing_user.id == checked_out_by_user_id
    release
  elsif releasing_user
    raise NotYoursError, "#{self.class} #{self.id} is not checked out by #{releasing_user.class.name} #{releasing_user.id} and cannot be released"
  end

  changes = {}
  changes[:checked_out_by_user_id] =
    write_attribute(:checked_out_by_user_id, nil)
  changes[:checked_out_by_user_type] =
    write_attribute(:checked_out_by_user_type, nil)
  changes[:checked_out_at] = write_attribute(:checked_out_at, nil)

  self.checked_out_by_user = nil

  self.class.unscoped do
    self.class.update_all changes, { id: self.id }
  end
end

#update_attributes_and_release(params = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/check_out.rb', line 131

def update_attributes_and_release(params={})
  params = params.merge(
    checked_out_by_user_id: nil,
    checked_out_at: nil,
    checked_out_by_user_type: nil
  )
  
  update_attributes params
end