Module: PaperTrail::VersionConcern::ClassMethods

Defined in:
lib/paper_trail/version_concern.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#between(start_time, end_time) ⇒ Object



98
99
100
101
102
103
# File 'lib/paper_trail/version_concern.rb', line 98

def between(start_time, end_time)
  where(
    arel_table[PaperTrail.timestamp_field].gt(start_time).
    and(arel_table[PaperTrail.timestamp_field].lt(end_time))
  ).order(timestamp_sort_order)
end

#createsObject



49
50
51
# File 'lib/paper_trail/version_concern.rb', line 49

def creates
  where event: "create"
end

#destroysObject



57
58
59
# File 'lib/paper_trail/version_concern.rb', line 57

def destroys
  where event: "destroy"
end

#not_createsObject



61
62
63
# File 'lib/paper_trail/version_concern.rb', line 61

def not_creates
  where "event <> ?", "create"
end

#object_changes_col_is_json?Boolean

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

Returns:

  • (Boolean)


185
186
187
# File 'lib/paper_trail/version_concern.rb', line 185

def object_changes_col_is_json?
  [:json, :jsonb].include?(columns_hash["object_changes"].try(:type))
end

#object_col_is_json?Boolean

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

Returns:

  • (Boolean)


179
180
181
# File 'lib/paper_trail/version_concern.rb', line 179

def object_col_is_json?
  [:json, :jsonb].include?(columns_hash["object"].type)
end

#preceding(obj, timestamp_arg = false) ⇒ Object

Returns versions before ‘obj`.

Parameters:

  • obj
    • a ‘Version` or a timestamp

  • timestamp_arg (defaults to: false)
    • boolean - When true, ‘obj` is a timestamp.

    Default: false.

Returns:

  • ‘ActiveRecord::Relation`



88
89
90
91
92
93
94
95
96
# File 'lib/paper_trail/version_concern.rb', line 88

def preceding(obj, timestamp_arg = false)
  if timestamp_arg != true && primary_key_is_int?
    return where(arel_table[primary_key].lt(obj.id)).order(arel_table[primary_key].desc)
  end

  obj = obj.send(PaperTrail.timestamp_field) if obj.is_a?(self)
  where(arel_table[PaperTrail.timestamp_field].lt(obj)).
    order(timestamp_sort_order("desc"))
end

#primary_key_is_int?Boolean

Returns:

  • (Boolean)


171
172
173
174
175
# File 'lib/paper_trail/version_concern.rb', line 171

def primary_key_is_int?
  @primary_key_is_int ||= columns_hash[primary_key].type == :integer
rescue
  true
end

#subsequent(obj, timestamp_arg = false) ⇒ Object

Returns versions after ‘obj`.

Parameters:

  • obj
    • a ‘Version` or a timestamp

  • timestamp_arg (defaults to: false)
    • boolean - When true, ‘obj` is a timestamp.

    Default: false.

Returns:

  • ‘ActiveRecord::Relation`



72
73
74
75
76
77
78
79
# File 'lib/paper_trail/version_concern.rb', line 72

def subsequent(obj, timestamp_arg = false)
  if timestamp_arg != true && primary_key_is_int?
    return where(arel_table[primary_key].gt(obj.id)).order(arel_table[primary_key].asc)
  end

  obj = obj.send(PaperTrail.timestamp_field) if obj.is_a?(self)
  where(arel_table[PaperTrail.timestamp_field].gt(obj)).order(timestamp_sort_order)
end

#timestamp_sort_order(direction = "asc") ⇒ Object

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



107
108
109
110
111
# File 'lib/paper_trail/version_concern.rb', line 107

def timestamp_sort_order(direction = "asc")
  [arel_table[PaperTrail.timestamp_field].send(direction.downcase)].tap do |array|
    array << arel_table[primary_key].send(direction.downcase) if primary_key_is_int?
  end
end

#updatesObject



53
54
55
# File 'lib/paper_trail/version_concern.rb', line 53

def updates
  where event: "update"
end

#where_object(args = {}) ⇒ Object

Query the ‘versions.objects` column using the SQL LIKE operator. Performs an attribute search on the serialized object by invoking the identically-named method in the serializer being used.

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/paper_trail/version_concern.rb', line 117

def where_object(args = {})
  raise ArgumentError, "expected to receive a Hash" unless args.is_a?(Hash)

  if columns_hash["object"].type == :jsonb
    where("object @> ?", args.to_json)
  elsif columns_hash["object"].type == :json
    predicates = []
    values = []
    args.each do |field, value|
      predicates.push "object->>? = ?"
      values.concat([field, value.to_s])
    end
    sql = predicates.join(" and ")
    where(sql, *values)
  else
    arel_field = arel_table[:object]
    where_conditions = args.map { |field, value|
      PaperTrail.serializer.where_object_condition(arel_field, field, value)
    }
    where_conditions = where_conditions.reduce { |a, e| a.and(e) }
    where(where_conditions)
  end
end

#where_object_changes(args = {}) ⇒ Object

Query the ‘versions.object_changes` column by attributes, using the SQL LIKE operator.

Raises:

  • (ArgumentError)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/paper_trail/version_concern.rb', line 144

def where_object_changes(args = {})
  raise ArgumentError, "expected to receive a Hash" unless args.is_a?(Hash)

  if columns_hash["object_changes"].type == :jsonb
    args.each { |field, value| args[field] = [value] }
    where("object_changes @> ?", args.to_json)
  elsif columns_hash["object"].type == :json
    predicates = []
    values = []
    args.each do |field, value|
      predicates.push(
        "((object_changes->>? ILIKE ?) OR (object_changes->>? ILIKE ?))"
      )
      values.concat([field, "[#{value.to_json},%", field, "[%,#{value.to_json}]%"])
    end
    sql = predicates.join(" and ")
    where(sql, *values)
  else
    arel_field = arel_table[:object_changes]
    where_conditions = args.map { |field, value|
      PaperTrail.serializer.where_object_changes_condition(arel_field, field, value)
    }
    where_conditions = where_conditions.reduce { |a, e| a.and(e) }
    where(where_conditions)
  end
end

#with_item_keys(item_type, item_id) ⇒ Object



45
46
47
# File 'lib/paper_trail/version_concern.rb', line 45

def with_item_keys(item_type, item_id)
  where item_type: item_type, item_id: item_id
end