Class: ModelTimeline::TimelineEntry

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/model_timeline/timeline_entry.rb

Overview

Represents a timeline entry that records changes to a model. TimelineEntry stores the tracked object, user who made the change, IP address, and the changes that were made.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#timelineableObject

Returns The model instance that this timeline entry belongs to.

Returns:

  • (Object)

    The model instance that this timeline entry belongs to



13
# File 'lib/model_timeline/timeline_entry.rb', line 13

belongs_to :timelineable, polymorphic: true, optional: true

#userObject

Returns The user who made the change.

Returns:

  • (Object)

    The user who made the change



17
# File 'lib/model_timeline/timeline_entry.rb', line 17

belongs_to :user, polymorphic: true, optional: true

Class Method Details

.for_ip_address(ip) ⇒ ActiveRecord::Relation

Retrieves timeline entries from a specific IP address

Parameters:

  • ip (String)

    The IP address to search for

Returns:

  • (ActiveRecord::Relation)

    Timeline entries from the specified IP address



39
40
41
# File 'lib/model_timeline/timeline_entry.rb', line 39

def self.for_ip_address(ip)
  where(ip_address: ip)
end

.for_timelineable(timelineable) ⇒ ActiveRecord::Relation

Retrieves timeline entries for a specific timelineable object

Parameters:

  • timelineable (Object)

    The object to find timeline entries for

Returns:

  • (ActiveRecord::Relation)

    Timeline entries for the specified object



23
24
25
# File 'lib/model_timeline/timeline_entry.rb', line 23

def self.for_timelineable(timelineable)
  where(timelineable_type: timelineable.class.name, timelineable_id: timelineable.id)
end

.for_user(user) ⇒ ActiveRecord::Relation

Retrieves timeline entries created by a specific user

Parameters:

  • user (Object)

    The user who created the timeline entries

Returns:

  • (ActiveRecord::Relation)

    Timeline entries created by the specified user



31
32
33
# File 'lib/model_timeline/timeline_entry.rb', line 31

def self.for_user(user)
  where(user_type: user.class.name, user_id: user.id)
end

.with_changed_attribute(attribute) ⇒ ActiveRecord::Relation

Retrieves timeline entries where a specific attribute was changed

Parameters:

  • attribute (Symbol, String)

    The attribute name to check for changes

Returns:

  • (ActiveRecord::Relation)

    Timeline entries where the specified attribute changed



47
48
49
# File 'lib/model_timeline/timeline_entry.rb', line 47

def self.with_changed_attribute(attribute)
  where('object_changes ? :key', key: attribute.to_s)
end

.with_changed_value(attribute, value) ⇒ ActiveRecord::Relation

Retrieves timeline entries where an attribute was changed to a specific value

Parameters:

  • attribute (Symbol, String)

    The attribute name to check

  • value (Object)

    The value the attribute was changed to

Returns:

  • (ActiveRecord::Relation)

    Timeline entries matching the attribute and value change



56
57
58
# File 'lib/model_timeline/timeline_entry.rb', line 56

def self.with_changed_value(attribute, value)
  where('object_changes -> :key ->> 1 = :value', key: attribute.to_s, value: value.to_s)
end