Module: IronTrail::ChangeModelConcern::ClassMethods

Defined in:
lib/iron_trail/change_model_concern.rb

Instance Method Summary collapse

Instance Method Details

#where_object_changes_from(args = {}) ⇒ Object



56
57
58
# File 'lib/iron_trail/change_model_concern.rb', line 56

def where_object_changes_from(args = {})
  _where_object_changes(0, args)
end

#where_object_changes_to(args = {}) ⇒ Object



52
53
54
# File 'lib/iron_trail/change_model_concern.rb', line 52

def where_object_changes_to(args = {})
  _where_object_changes(1, args)
end

#with_delta_other_than(*columns) ⇒ Object

Allows filtering out updates that changed just a certain set of columns. This could be useful, for instance, to filter out updates made with ActiveRecord's #touch method, which changes only the updated_at column. In that case, calling .with_delta_other_than(:updated_at) would exclude such changes from the result.

This works by inspecting whether there are any keys in the rec_delta column other than the columns specified in the columns parameter.



68
69
70
71
72
73
74
75
76
# File 'lib/iron_trail/change_model_concern.rb', line 68

def with_delta_other_than(*columns)
  quoted_columns = with_connection do |conn|
    columns.map { |col_name| conn.quote(col_name) }
  end
  exclude_array = "ARRAY[#{quoted_columns.join(', ')}]::text[]"

  sql = "rec_delta IS NULL OR (rec_delta - #{exclude_array}) <> '{}'::jsonb"
  where(::Arel::Nodes::SqlLiteral.new(sql))
end