Module: PaperTrail::VersionConcern::ClassMethods
- Defined in:
- lib/paper_trail/version_concern.rb
Overview
:nodoc:
Instance Method Summary collapse
- #between(start_time, end_time) ⇒ Object
- #creates ⇒ Object
- #destroys ⇒ Object
- #not_creates ⇒ Object
-
#object_changes_col_is_json? ⇒ Boolean
Returns whether the
object_changescolumn is using thejsontype supported by PostgreSQL. -
#object_col_is_json? ⇒ Boolean
Returns whether the
objectcolumn is using thejsontype supported by PostgreSQL. -
#preceding(obj, timestamp_arg = false) ⇒ Object
Returns versions before
obj. - #primary_key_is_int? ⇒ Boolean
-
#subsequent(obj, timestamp_arg = false) ⇒ Object
Returns versions after
obj. -
#timestamp_sort_order(direction = "asc") ⇒ Object
Defaults to using the primary key as the secondary sort order if possible.
- #updates ⇒ Object
-
#where_object(args = {}) ⇒ Object
Given a hash of attributes like ‘name: ’Joan’‘, query the
versions.objectscolumn. -
#where_object_changes(args = {}) ⇒ Object
Given a hash of attributes like ‘name: ’Joan’‘, query the
versions.objects_changescolumn. - #with_item_keys(item_type, item_id) ⇒ Object
Instance Method Details
#between(start_time, end_time) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/paper_trail/version_concern.rb', line 89 def between(start_time, end_time) where( arel_table[:created_at].gt(start_time). and(arel_table[:created_at].lt(end_time)) ).order() end |
#creates ⇒ Object
40 41 42 |
# File 'lib/paper_trail/version_concern.rb', line 40 def creates where event: "create" end |
#destroys ⇒ Object
48 49 50 |
# File 'lib/paper_trail/version_concern.rb', line 48 def destroys where event: "destroy" end |
#not_creates ⇒ Object
52 53 54 |
# File 'lib/paper_trail/version_concern.rb', line 52 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.
174 175 176 |
# File 'lib/paper_trail/version_concern.rb', line 174 def object_changes_col_is_json? i[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.
168 169 170 |
# File 'lib/paper_trail/version_concern.rb', line 168 def object_col_is_json? i[json jsonb].include?(columns_hash["object"].type) end |
#preceding(obj, timestamp_arg = false) ⇒ Object
Returns versions before obj.
79 80 81 82 83 84 85 86 87 |
# File 'lib/paper_trail/version_concern.rb', line 79 def preceding(obj, = false) if != true && primary_key_is_int? return where(arel_table[primary_key].lt(obj.id)).order(arel_table[primary_key].desc) end obj = obj.send(:created_at) if obj.is_a?(self) where(arel_table[:created_at].lt(obj)). order(("desc")) end |
#primary_key_is_int? ⇒ Boolean
160 161 162 163 164 |
# File 'lib/paper_trail/version_concern.rb', line 160 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) ⇒ Object
Returns versions after obj.
63 64 65 66 67 68 69 70 |
# File 'lib/paper_trail/version_concern.rb', line 63 def subsequent(obj, = false) if != true && primary_key_is_int? return where(arel_table[primary_key].gt(obj.id)).order(arel_table[primary_key].asc) end obj = obj.send(:created_at) if obj.is_a?(self) where(arel_table[:created_at].gt(obj)).order() end |
#timestamp_sort_order(direction = "asc") ⇒ Object
Defaults to using the primary key as the secondary sort order if possible.
98 99 100 101 102 |
# File 'lib/paper_trail/version_concern.rb', line 98 def (direction = "asc") [arel_table[:created_at].send(direction.downcase)].tap do |array| array << arel_table[primary_key].send(direction.downcase) if primary_key_is_int? end end |
#updates ⇒ Object
44 45 46 |
# File 'lib/paper_trail/version_concern.rb', line 44 def updates where event: "update" end |
#where_object(args = {}) ⇒ Object
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 ‘@>`.
124 125 126 127 |
# File 'lib/paper_trail/version_concern.rb', line 124 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 = {}) ⇒ Object
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 ‘@>`.
155 156 157 158 |
# File 'lib/paper_trail/version_concern.rb', line 155 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) ⇒ Object
36 37 38 |
# File 'lib/paper_trail/version_concern.rb', line 36 def with_item_keys(item_type, item_id) where item_type: item_type, item_id: item_id end |