module PaperTrail::VersionConcern::ClassMethods

Public Instance Methods

between(start_time, end_time) click to toggle source
# File lib/mongo_trails/version_concern.rb, line 41
def between(start_time, end_time)
  where(:created_at.gt => start_time).and(:created_at.lt => end_time).order(timestamp_sort_order)
end
creates() click to toggle source
# File lib/mongo_trails/version_concern.rb, line 25
def creates
  where event: "create"
end
destroys() click to toggle source
# File lib/mongo_trails/version_concern.rb, line 33
def destroys
  where event: "destroy"
end
item_subtype_column_present?() click to toggle source
# File lib/mongo_trails/version_concern.rb, line 17
def item_subtype_column_present?
  column_names.include?("item_subtype")
end
not_creates() click to toggle source
# File lib/mongo_trails/version_concern.rb, line 37
def not_creates
  where "event <> ?", "create"
end
object_changes_col_is_json?() click to toggle source

Returns whether the `object_changes` column is using the `json` type supported by PostgreSQL.

# File lib/mongo_trails/version_concern.rb, line 122
def object_changes_col_is_json?
  # %i[json jsonb].include?(columns_hash["object_changes"].try(:type))
  true
end
object_col_is_json?() click to toggle source

Returns whether the `object` column is using the `json` type supported by PostgreSQL.

# File lib/mongo_trails/version_concern.rb, line 115
def object_col_is_json?
  # %i[json jsonb].include?(columns_hash["object"].type)
  true
end
preceding(obj, timestamp_arg = false) click to toggle source

Returns versions before `obj`.

@param obj - a `Version` or a timestamp @param timestamp_arg - boolean - When true, `obj` is a timestamp.

Default: false.

@return `ActiveRecord::Relation` @api public

# File lib/mongo_trails/version_concern.rb, line 134
def preceding(obj, timestamp_arg = false)
  if timestamp_arg != true && primary_key_is_int?
    preceding_by_id(obj)
  else
    preceding_by_timestamp(obj)
  end
end
primary_key_is_int?() click to toggle source
# File lib/mongo_trails/version_concern.rb, line 107
def primary_key_is_int?
  @primary_key_is_int ||= columns_hash[primary_key].type == :integer
rescue StandardError # TODO: Rescue something more specific
  true
end
subsequent(obj, timestamp_arg = false) click to toggle source

Returns versions after `obj`.

@param obj - a `Version` or a timestamp @param timestamp_arg - boolean - When true, `obj` is a timestamp.

Default: false.

@return `ActiveRecord::Relation` @api public

# File lib/mongo_trails/version_concern.rb, line 149
def subsequent(obj, timestamp_arg = false)
  if timestamp_arg != true && primary_key_is_int?
    subsequent_by_id(obj)
  else
    subsequent_by_timestamp(obj)
  end
end
timestamp_sort_order(direction = "asc") click to toggle source

Defaults to using the primary key as the secondary sort order if possible.

# File lib/mongo_trails/version_concern.rb, line 47
def timestamp_sort_order(direction = "asc")
  { created_at: direction.downcase }
end
updates() click to toggle source
# File lib/mongo_trails/version_concern.rb, line 29
def updates
  where event: "update"
end
where_object(args = {}) click to toggle source

Given a hash of attributes like `name: 'Joan'`, query the `versions.objects` column.

“` SELECT “versions”.* FROM “versions” WHERE (“versions”.“object” LIKE '% name: Joan %') “`

This is useful for finding versions where a given attribute had a given value. Imagine, in the example above, that Joan had changed her name and we wanted to find the versions before that change.

Based on the data type of the `object` column, the appropriate SQL operator is used. For example, a text column will use `like`, and a jsonb column will use `@>`.

@api public

# File lib/mongo_trails/version_concern.rb, line 71
def where_object(args = {})
  raise ArgumentError, "expected to receive a Hash" unless args.is_a?(Hash)
  Queries::Versions::WhereObject.new(self, args).execute
end
where_object_changes(args = {}) click to toggle source

Given a hash of attributes like `name: 'Joan'`, query the `versions.objects_changes` column.

“` SELECT “versions”.* FROM “versions” WHERE .. (“versions”.“object_changes” LIKE '% name:

  • Joan

%' OR “versions”.“object_changes” LIKE '% name: -%

  • Joan

%') “`

This is useful for finding versions immediately before and after a given attribute had a given value. Imagine, in the example above, that someone changed their name to Joan and we wanted to find the versions immediately before and after that change.

Based on the data type of the `object` column, the appropriate SQL operator is used. For example, a text column will use `like`, and a jsonb column will use `@>`.

@api public

# File lib/mongo_trails/version_concern.rb, line 102
def where_object_changes(args = {})
  raise ArgumentError, "expected to receive a Hash" unless args.is_a?(Hash)
  Queries::Versions::WhereObjectChanges.new(self, args).execute
end
with_item_keys(item_type, item_id) click to toggle source
# File lib/mongo_trails/version_concern.rb, line 21
def with_item_keys(item_type, item_id)
  where(item_type: item_type).and(item_id: item_id)
end

Private Instance Methods

preceding_by_id(obj) click to toggle source

@api private

# File lib/mongo_trails/version_concern.rb, line 160
def preceding_by_id(obj)
  where(:integer_id.lt => obj.integer_id).order(integer_id: :desc)
end
preceding_by_timestamp(obj) click to toggle source

@api private

# File lib/mongo_trails/version_concern.rb, line 165
def preceding_by_timestamp(obj)
  obj = obj.send(:created_at) if obj.is_a?(self)
  where(:created_at.lt => obj).order(timestamp_sort_order("desc"))
end
subsequent_by_id(version) click to toggle source

@api private

# File lib/mongo_trails/version_concern.rb, line 171
def subsequent_by_id(version)
  where(:integer_id.gt => version.integer_id).order(integer_id: :asc)
end
subsequent_by_timestamp(obj) click to toggle source

@api private

# File lib/mongo_trails/version_concern.rb, line 176
def subsequent_by_timestamp(obj)
  obj = obj.send(:created_at) if obj.is_a?(self)
  where(:created_at.gt => obj).order(timestamp_sort_order)
end