logidze 0.10.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +81 -4
- data/LICENSE.txt +1 -1
- data/README.md +291 -106
- data/lib/generators/logidze/fx_helper.rb +17 -0
- data/lib/generators/logidze/inject_sql.rb +18 -0
- data/lib/generators/logidze/install/USAGE +6 -1
- data/lib/generators/logidze/install/functions/logidze_compact_history.sql +38 -0
- data/lib/generators/logidze/install/functions/logidze_filter_keys.sql +27 -0
- data/lib/generators/logidze/install/functions/logidze_logger.sql +150 -0
- data/lib/generators/logidze/install/functions/logidze_snapshot.sql +32 -0
- data/lib/generators/logidze/install/functions/logidze_version.sql +20 -0
- data/lib/generators/logidze/install/install_generator.rb +43 -1
- data/lib/generators/logidze/install/templates/hstore.rb.erb +1 -1
- data/lib/generators/logidze/install/templates/migration.rb.erb +19 -232
- data/lib/generators/logidze/install/templates/migration_fx.rb.erb +41 -0
- data/lib/generators/logidze/model/model_generator.rb +53 -13
- data/lib/generators/logidze/model/templates/migration.rb.erb +57 -36
- data/lib/generators/logidze/model/triggers/logidze.sql +6 -0
- data/lib/logidze.rb +37 -31
- data/lib/logidze/engine.rb +11 -0
- data/lib/logidze/has_logidze.rb +5 -7
- data/lib/logidze/history.rb +1 -10
- data/lib/logidze/ignore_log_data.rb +5 -40
- data/lib/logidze/meta.rb +43 -16
- data/lib/logidze/model.rb +50 -43
- data/lib/logidze/utils/check_pending.rb +57 -0
- data/lib/logidze/utils/function_definitions.rb +49 -0
- data/lib/logidze/utils/pending_migration_error.rb +25 -0
- data/lib/logidze/version.rb +1 -1
- metadata +65 -76
- data/.gitattributes +0 -3
- data/.github/ISSUE_TEMPLATE.md +0 -20
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -29
- data/.gitignore +0 -40
- data/.rubocop.yml +0 -55
- data/.travis.yml +0 -42
- data/Gemfile +0 -15
- data/Rakefile +0 -28
- data/assets/pg_log_data_chart.png +0 -0
- data/bench/performance/README.md +0 -109
- data/bench/performance/diff_bench.rb +0 -38
- data/bench/performance/insert_bench.rb +0 -22
- data/bench/performance/memory_profile.rb +0 -56
- data/bench/performance/setup.rb +0 -315
- data/bench/performance/update_bench.rb +0 -38
- data/bench/triggers/Makefile +0 -56
- data/bench/triggers/Readme.md +0 -58
- data/bench/triggers/bench.sql +0 -6
- data/bench/triggers/hstore_trigger_setup.sql +0 -38
- data/bench/triggers/jsonb_minus_2_setup.sql +0 -47
- data/bench/triggers/jsonb_minus_setup.sql +0 -49
- data/bench/triggers/keys2_trigger_setup.sql +0 -44
- data/bench/triggers/keys_trigger_setup.sql +0 -50
- data/bin/console +0 -8
- data/bin/setup +0 -9
- data/gemfiles/rails42.gemfile +0 -6
- data/gemfiles/rails5.gemfile +0 -6
- data/gemfiles/rails52.gemfile +0 -6
- data/gemfiles/rails6.gemfile +0 -6
- data/gemfiles/railsmaster.gemfile +0 -7
- data/lib/logidze/ignore_log_data/association.rb +0 -11
- data/lib/logidze/ignore_log_data/default_scope_patch.rb +0 -25
- data/lib/logidze/ignore_log_data/ignored_columns.rb +0 -46
- data/lib/logidze/ignore_log_data/missing_attribute_patch.rb +0 -18
- data/lib/logidze/migration.rb +0 -20
- data/logidze.gemspec +0 -41
@@ -0,0 +1,41 @@
|
|
1
|
+
class <%= @migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
2
|
+
def change
|
3
|
+
<%- if update? -%>
|
4
|
+
reversible do |dir|
|
5
|
+
dir.up do
|
6
|
+
# Drop legacy functions (<1.0)
|
7
|
+
execute <<~SQL
|
8
|
+
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb);
|
9
|
+
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb);
|
10
|
+
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb, text[]);
|
11
|
+
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb, text[]);
|
12
|
+
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb, timestamp with time zone, text[]);
|
13
|
+
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb, text, text[]);
|
14
|
+
DROP FUNCTION IF EXISTS logidze_exclude_keys(jsonb, VARIADIC text[]);
|
15
|
+
DROP FUNCTION IF EXISTS logidze_compact_history(jsonb);
|
16
|
+
SQL
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
<%- end -%>
|
21
|
+
<%- function_definitions.each do |f| -%>
|
22
|
+
<%- previous_version = previous_version_for(f.name) -%>
|
23
|
+
<%- if previous_version -%>
|
24
|
+
<%- if previous_version != f.version -%>
|
25
|
+
update_function :<%= f.name %>, version: <%= f.version %>, revert_to_version: <%= previous_version %>
|
26
|
+
<%- end -%>
|
27
|
+
<%- else -%>
|
28
|
+
reversible do |dir|
|
29
|
+
dir.up do
|
30
|
+
create_function :<%= f.name %>, version: <%= f.version %>
|
31
|
+
end
|
32
|
+
|
33
|
+
dir.down do
|
34
|
+
execute "DROP FUNCTION IF EXISTS <%= f.name %>(<%= f.signature %>) CASCADE"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
<%- end -%>
|
39
|
+
<%- end -%>
|
40
|
+
end
|
41
|
+
end
|
@@ -2,11 +2,19 @@
|
|
2
2
|
|
3
3
|
require "rails/generators"
|
4
4
|
require "rails/generators/active_record/migration/migration_generator"
|
5
|
+
require_relative "../inject_sql"
|
6
|
+
require_relative "../fx_helper"
|
7
|
+
|
8
|
+
using RubyNext
|
5
9
|
|
6
10
|
module Logidze
|
7
11
|
module Generators
|
8
12
|
class ModelGenerator < ::ActiveRecord::Generators::Base # :nodoc:
|
13
|
+
include InjectSql
|
14
|
+
include FxHelper
|
15
|
+
|
9
16
|
source_root File.expand_path("templates", __dir__)
|
17
|
+
source_paths << File.expand_path("triggers", __dir__)
|
10
18
|
|
11
19
|
class_option :limit, type: :numeric, optional: true, desc: "Specify history size limit"
|
12
20
|
|
@@ -21,23 +29,31 @@ module Logidze
|
|
21
29
|
|
22
30
|
class_option :path, type: :string, optional: true, desc: "Specify path to the model file"
|
23
31
|
|
24
|
-
class_option :
|
25
|
-
class_option :
|
32
|
+
class_option :except, type: :array, optional: true
|
33
|
+
class_option :only, type: :array, optional: true
|
26
34
|
|
27
35
|
class_option :timestamp_column, type: :string, optional: true,
|
28
36
|
desc: "Specify timestamp column"
|
29
37
|
|
38
|
+
class_option :name, type: :string, optional: true, desc: "Migration name"
|
39
|
+
|
30
40
|
class_option :update, type: :boolean, optional: true,
|
31
41
|
desc: "Define whether this is an update migration"
|
32
42
|
|
33
43
|
def generate_migration
|
34
|
-
if options[:
|
35
|
-
warn "Use only one: --
|
44
|
+
if options[:except] && options[:only]
|
45
|
+
warn "Use only one: --only or --except"
|
36
46
|
exit(1)
|
37
47
|
end
|
38
48
|
migration_template "migration.rb.erb", "db/migrate/#{migration_file_name}"
|
39
49
|
end
|
40
50
|
|
51
|
+
def generate_fx_trigger
|
52
|
+
return unless fx?
|
53
|
+
|
54
|
+
template "logidze.sql", "db/triggers/logidze_on_#{table_name}_v#{next_version.to_s.rjust(2, "0")}.sql"
|
55
|
+
end
|
56
|
+
|
41
57
|
def inject_logidze_to_model
|
42
58
|
return if update?
|
43
59
|
|
@@ -48,6 +64,8 @@ module Logidze
|
|
48
64
|
|
49
65
|
no_tasks do
|
50
66
|
def migration_name
|
67
|
+
return options[:name] if options[:name].present?
|
68
|
+
|
51
69
|
if update?
|
52
70
|
"update_logidze_for_#{plural_table_name}"
|
53
71
|
else
|
@@ -75,14 +93,13 @@ module Logidze
|
|
75
93
|
options[:update]
|
76
94
|
end
|
77
95
|
|
78
|
-
def
|
79
|
-
|
80
|
-
|
81
|
-
else
|
82
|
-
class_name.constantize.column_names - options[:whitelist]
|
83
|
-
end
|
96
|
+
def filtered_columns
|
97
|
+
format_pgsql_array(options[:only] || options[:except])
|
98
|
+
end
|
84
99
|
|
85
|
-
|
100
|
+
def include_columns
|
101
|
+
return unless options[:only] || options[:except]
|
102
|
+
options[:only].present?
|
86
103
|
end
|
87
104
|
|
88
105
|
def timestamp_column
|
@@ -96,12 +113,35 @@ module Logidze
|
|
96
113
|
options[:debounce_time]
|
97
114
|
end
|
98
115
|
|
116
|
+
def previous_version
|
117
|
+
@previous_version ||= all_triggers.filter_map { |path| Regexp.last_match[1].to_i if path =~ %r{logidze_on_#{table_name}_v(\d+).sql} }.max
|
118
|
+
end
|
119
|
+
|
120
|
+
def next_version
|
121
|
+
previous_version&.next || 1
|
122
|
+
end
|
123
|
+
|
124
|
+
def all_triggers
|
125
|
+
@all_triggers ||=
|
126
|
+
begin
|
127
|
+
res = nil
|
128
|
+
in_root do
|
129
|
+
res = if File.directory?("db/triggers")
|
130
|
+
Dir.entries("db/triggers")
|
131
|
+
else
|
132
|
+
[]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
res
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
99
139
|
def logidze_logger_parameters
|
100
|
-
format_pgsql_args(limit, timestamp_column,
|
140
|
+
format_pgsql_args(limit, timestamp_column, filtered_columns, include_columns, debounce_time)
|
101
141
|
end
|
102
142
|
|
103
143
|
def logidze_snapshot_parameters
|
104
|
-
format_pgsql_args("to_jsonb(t)", timestamp_column,
|
144
|
+
format_pgsql_args("to_jsonb(t)", timestamp_column, filtered_columns, include_columns)
|
105
145
|
end
|
106
146
|
|
107
147
|
def format_pgsql_array(ruby_array)
|
@@ -1,43 +1,64 @@
|
|
1
|
-
class <%= @migration_class_name %> < ActiveRecord::Migration<%= ActiveRecord::
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def up
|
6
|
-
<% if update? %>
|
7
|
-
execute "DROP TRIGGER logidze_on_<%= table_name %> on <%= table_name %>;"
|
8
|
-
<% elsif !only_trigger? %>
|
1
|
+
class <%= @migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
2
|
+
def change
|
3
|
+
<%- unless update? || only_trigger? -%>
|
9
4
|
add_column :<%= table_name %>, :log_data, :jsonb
|
10
|
-
|
5
|
+
<%- end -%>
|
11
6
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
7
|
+
<%- if fx? -%>
|
8
|
+
<%- if previous_version -%>
|
9
|
+
update_trigger :logidze_on_<%= table_name %>, on: :<%= table_name %>, version: <%= next_version %>, revert_to_version: <%= previous_version %>
|
10
|
+
<%- else -%>
|
11
|
+
reversible do |dir|
|
12
|
+
dir.up do
|
13
|
+
<%- if update? -%>
|
14
|
+
# Drop legacy trigger if any (<1.0)
|
15
|
+
execute "DROP TRIGGER IF EXISTS logidze_on_<%= table_name %> on <%= table_name %>;"
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
17
|
+
<%- end -%>
|
18
|
+
create_trigger :logidze_on_<%= table_name %>, on: :<%= table_name %>
|
19
|
+
end
|
20
|
+
|
21
|
+
dir.down do
|
22
|
+
execute "DROP TRIGGER IF EXISTS logidze_on_<%= table_name %> on <%= table_name %>;"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
<%- end -%>
|
26
|
+
<%- else -%>
|
27
|
+
reversible do |dir|
|
28
|
+
dir.up do
|
29
|
+
<%- if update? -%>
|
30
|
+
execute "DROP TRIGGER IF EXISTS logidze_on_<%= table_name %> on <%= table_name %>;"
|
31
|
+
|
32
|
+
<%- end -%>
|
33
|
+
execute <<~SQL
|
34
|
+
<%= inject_sql("logidze.sql", indent: 10) %>
|
35
|
+
SQL
|
36
|
+
end
|
26
37
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
38
|
+
dir.down do
|
39
|
+
<%- if update? -%>
|
40
|
+
# NOTE: We have no idea on how to revert the migration
|
41
|
+
# ('cause we don't know the previous trigger params),
|
42
|
+
# but you can do that on your own.
|
43
|
+
#
|
44
|
+
# Uncomment this line if you want to raise an error.
|
45
|
+
# raise ActiveRecord::IrreversibleMigration
|
46
|
+
<%- else -%>
|
47
|
+
execute "DROP TRIGGER IF EXISTS logidze_on_<%= table_name %> on <%= table_name %>;"
|
48
|
+
<%- end -%>
|
49
|
+
end
|
50
|
+
end
|
51
|
+
<%- end -%>
|
52
|
+
<%- if backfill? -%>
|
37
53
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
54
|
+
reversible do |dir|
|
55
|
+
dir.up do
|
56
|
+
execute <<~SQL
|
57
|
+
UPDATE <%= table_name %> as t
|
58
|
+
SET log_data = logidze_snapshot(<%= logidze_snapshot_parameters %>);
|
59
|
+
SQL
|
60
|
+
end
|
61
|
+
end
|
62
|
+
<%- end -%>
|
42
63
|
end
|
43
64
|
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
CREATE TRIGGER logidze_on_<%= table_name %>
|
2
|
+
BEFORE UPDATE OR INSERT ON <%= table_name %> FOR EACH ROW
|
3
|
+
WHEN (coalesce(current_setting('logidze.disabled', true), '') <> 'on')
|
4
|
+
-- Parameters: history_size_limit (integer), timestamp_column (text), filtered_columns (text[]),
|
5
|
+
-- include_columns (boolean), debounce_time_ms (integer)
|
6
|
+
EXECUTE PROCEDURE logidze_logger(<%= logidze_logger_parameters %>);
|
data/lib/logidze.rb
CHANGED
@@ -5,6 +5,7 @@ require "logidze/version"
|
|
5
5
|
# Logidze provides tools for adding in-table JSON-based audit to DB tables
|
6
6
|
# and ActiveRecord extensions to work with changes history.
|
7
7
|
module Logidze
|
8
|
+
require "ruby-next"
|
8
9
|
require "logidze/history"
|
9
10
|
require "logidze/model"
|
10
11
|
require "logidze/versioned_association"
|
@@ -19,48 +20,53 @@ module Logidze
|
|
19
20
|
class << self
|
20
21
|
# Determines if Logidze should append a version to the log after updating an old version.
|
21
22
|
attr_accessor :append_on_undo
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
def associations_versioning
|
26
|
-
@associations_versioning || false
|
27
|
-
end
|
28
|
-
|
23
|
+
# Determines whether associations versioning is enabled or not
|
24
|
+
attr_accessor :associations_versioning
|
29
25
|
# Determines if Logidze should exclude log data from SELECT statements
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
attr_writer :force_load_log_data
|
37
|
-
|
38
|
-
def force_load_log_data
|
39
|
-
@force_load_log_data || false
|
40
|
-
end
|
26
|
+
attr_accessor :ignore_log_data_by_default
|
27
|
+
# Whether #at should return self or nil when log_data is nil
|
28
|
+
attr_accessor :return_self_if_log_data_is_empty
|
29
|
+
# Determines what Logidze should do when upgrade is needed (:raise | :warn | :ignore)
|
30
|
+
attr_reader :on_pending_upgrade
|
41
31
|
|
42
32
|
# Temporary disable DB triggers.
|
43
33
|
#
|
44
34
|
# @example
|
45
35
|
# Logidze.without_logging { Post.update_all(active: true) }
|
46
36
|
def without_logging
|
47
|
-
|
48
|
-
ActiveRecord::Base.connection.execute "SET LOCAL logidze.disabled TO on;"
|
49
|
-
res = yield
|
50
|
-
ActiveRecord::Base.connection.execute "SET LOCAL logidze.disabled TO DEFAULT;"
|
51
|
-
res
|
52
|
-
end
|
37
|
+
with_logidze_setting("logidze.disabled", "on") { yield }
|
53
38
|
end
|
54
39
|
|
55
|
-
#
|
40
|
+
# Instruct Logidze to create a full snapshot for the new versions, not a diff
|
56
41
|
#
|
57
42
|
# @example
|
58
|
-
# Logidze.
|
59
|
-
def
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
43
|
+
# Logidze.with_full_snapshot { post.touch }
|
44
|
+
def with_full_snapshot
|
45
|
+
with_logidze_setting("logidze.full_snapshot", "on") { yield }
|
46
|
+
end
|
47
|
+
|
48
|
+
def on_pending_upgrade=(mode)
|
49
|
+
if %i[raise warn ignore].exclude? mode
|
50
|
+
raise ArgumentError, "Unknown on_pending_upgrade option `#{mode.inspect}`. Expecting :raise, :warn or :ignore"
|
51
|
+
end
|
52
|
+
@on_pending_upgrade = mode
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def with_logidze_setting(name, value)
|
58
|
+
ActiveRecord::Base.transaction do
|
59
|
+
ActiveRecord::Base.connection.execute "SET LOCAL #{name} TO #{value};"
|
60
|
+
res = yield
|
61
|
+
ActiveRecord::Base.connection.execute "SET LOCAL #{name} TO DEFAULT;"
|
62
|
+
res
|
63
|
+
end
|
64
64
|
end
|
65
65
|
end
|
66
|
+
|
67
|
+
self.append_on_undo = false
|
68
|
+
self.associations_versioning = false
|
69
|
+
self.ignore_log_data_by_default = false
|
70
|
+
self.return_self_if_log_data_is_empty = true
|
71
|
+
self.on_pending_upgrade = :ignore
|
66
72
|
end
|
data/lib/logidze/engine.rb
CHANGED
@@ -1,13 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "logidze"
|
4
|
+
require "logidze/utils/check_pending"
|
4
5
|
|
5
6
|
module Logidze
|
6
7
|
class Engine < Rails::Engine # :nodoc:
|
8
|
+
config.logidze = Logidze
|
9
|
+
|
7
10
|
initializer "extend ActiveRecord with Logidze" do |_app|
|
8
11
|
ActiveSupport.on_load(:active_record) do
|
9
12
|
ActiveRecord::Base.send :include, Logidze::HasLogidze
|
10
13
|
end
|
11
14
|
end
|
15
|
+
|
16
|
+
initializer "check Logidze function versions" do |app|
|
17
|
+
if config.logidze.on_pending_upgrade != :ignore
|
18
|
+
ActiveSupport.on_load(:active_record) do
|
19
|
+
app.config.app_middleware.use Logidze::Utils::CheckPending
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
12
23
|
end
|
13
24
|
end
|
data/lib/logidze/has_logidze.rb
CHANGED
@@ -10,19 +10,17 @@ module Logidze
|
|
10
10
|
module ClassMethods # :nodoc:
|
11
11
|
# Include methods to work with history.
|
12
12
|
#
|
13
|
-
def has_logidze(ignore_log_data:
|
14
|
-
include Logidze::Model
|
13
|
+
def has_logidze(ignore_log_data: Logidze.ignore_log_data_by_default)
|
15
14
|
include Logidze::IgnoreLogData
|
15
|
+
include Logidze::Model
|
16
16
|
|
17
17
|
@ignore_log_data = ignore_log_data
|
18
|
+
|
19
|
+
self.ignored_columns += ["log_data"] if @ignore_log_data
|
18
20
|
end
|
19
21
|
|
20
22
|
def ignores_log_data?
|
21
|
-
|
22
|
-
!Logidze.force_load_log_data
|
23
|
-
else
|
24
|
-
@ignore_log_data
|
25
|
-
end
|
23
|
+
@ignore_log_data
|
26
24
|
end
|
27
25
|
end
|
28
26
|
end
|
data/lib/logidze/history.rb
CHANGED
@@ -17,15 +17,6 @@ module Logidze
|
|
17
17
|
delegate :size, to: :versions
|
18
18
|
delegate :responsible_id, :meta, to: :current_version
|
19
19
|
|
20
|
-
### Rails 4 ###
|
21
|
-
def self.dump(object)
|
22
|
-
ActiveSupport::JSON.encode(object)
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.load(json)
|
26
|
-
new(json) if json.present?
|
27
|
-
end
|
28
|
-
|
29
20
|
def initialize(data)
|
30
21
|
@data = data
|
31
22
|
end
|
@@ -108,7 +99,7 @@ module Logidze
|
|
108
99
|
|
109
100
|
# Return nearest (from the bottom) version to the specified time
|
110
101
|
def find_by_time(time)
|
111
|
-
versions.
|
102
|
+
versions.reverse_each.find { |v| v.time <= time }
|
112
103
|
end
|
113
104
|
|
114
105
|
def dup
|
@@ -1,57 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "logidze/ignore_log_data/association"
|
4
|
-
|
5
3
|
module Logidze
|
6
4
|
module IgnoreLogData # :nodoc:
|
7
5
|
extend ActiveSupport::Concern
|
8
6
|
|
9
7
|
included do
|
10
|
-
if Rails::VERSION::MAJOR ==
|
11
|
-
require "logidze/ignore_log_data/
|
12
|
-
|
13
|
-
if Rails::VERSION::MAJOR == 5
|
14
|
-
require "logidze/ignore_log_data/cast_attribute_patch"
|
15
|
-
include CastAttributePatch
|
16
|
-
end
|
17
|
-
|
18
|
-
require "logidze/ignore_log_data/missing_attribute_patch"
|
19
|
-
include MissingAttributePatch
|
20
|
-
|
21
|
-
require "logidze/ignore_log_data/default_scope_patch"
|
22
|
-
include DefaultScopePatch
|
8
|
+
if Rails::VERSION::MAJOR == 5
|
9
|
+
require "logidze/ignore_log_data/cast_attribute_patch"
|
10
|
+
include CastAttributePatch
|
23
11
|
end
|
24
12
|
|
25
|
-
self.ignored_columns += ["log_data"]
|
26
|
-
|
27
13
|
scope :with_log_data, lambda {
|
28
14
|
if ignored_columns == ["log_data"]
|
29
|
-
select(
|
15
|
+
select(arel_table[Arel.star])
|
30
16
|
else
|
31
|
-
select(column_names + [
|
17
|
+
select(column_names + [arel_table[:log_data]])
|
32
18
|
end
|
33
19
|
}
|
34
20
|
end
|
35
|
-
|
36
|
-
module ClassMethods # :nodoc:
|
37
|
-
def self.default_scope
|
38
|
-
ignores_log_data? ? super : super.with_log_data
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def association(name)
|
43
|
-
association = super
|
44
|
-
|
45
|
-
should_load_log_data =
|
46
|
-
attributes["log_data"] &&
|
47
|
-
association.klass.respond_to?(:has_logidze?) &&
|
48
|
-
!association.singleton_class.include?(Logidze::IgnoreLogData::Association)
|
49
|
-
|
50
|
-
return association unless should_load_log_data
|
51
|
-
|
52
|
-
association.singleton_class.prepend(Logidze::IgnoreLogData::Association)
|
53
|
-
|
54
|
-
association
|
55
|
-
end
|
56
21
|
end
|
57
22
|
end
|