Module: ChangesAreLogged::InstanceMethods

Defined in:
lib/changes_are_logged.rb

Instance Method Summary collapse

Instance Method Details

#attribute_change(attr) ⇒ Object

this modifies dirty.rb behavior. previously #changes returned the change in the accessor method now, #changes will return raw changes made to actual database attributes



54
55
56
# File 'lib/changes_are_logged.rb', line 54

def attribute_change(attr)
  [changed_attributes[attr], __send__(:read_attribute, attr)] if attribute_changed?(attr)
end

#log_itObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/changes_are_logged.rb', line 6

def log_it
  return unless @log_changes

  # FIXME too complected with lumos_rails
  unless @modifying_user_id
    @modifying_user_id = if defined?(StaffUser) && staff_user = StaffUser.current
      @modifying_user_is_staff = true
      staff_user.id
    end
    @modifying_user_id ||= if defined?(current_user) && current_user && current_user.respond_to?(:id)
      current_user.id
    end
  end

  if new_record?
    @change_comments = "new record" if @change_comments.blank?
    @changes_logged = {}
    save_change_log
  elsif changed? || !@change_comments.blank?
    @changes_logged = HashWithIndifferentAccess.new

    if log_changes_callback
      changes.each do |attribute, (old_value, new_value)|
        @changes_logged[attribute] = log_changes_callback.call(
          attribute, old_value, new_value
        )
      end
    else
      @changes_logged.merge!(changes)
    end

    @changes_logged.delete("updated_at")
    save_change_log
  end
end

#save_change_logObject



42
43
44
45
46
47
48
49
50
# File 'lib/changes_are_logged.rb', line 42

def save_change_log
  self.change_logs << ChangeLog.new(
    :changes_logged => @changes_logged,
    :user_id        => @modifying_user_id,
    :comments       => @change_comments,
    :user_is_staff  => @modifying_user_is_staff
  )
  @change_comments = nil
end