paper_trail 4.2.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{CONTRIBUTING.md → .github/CONTRIBUTING.md} +28 -9
- data/.github/ISSUE_TEMPLATE.md +13 -0
- data/.gitignore +2 -1
- data/.rubocop.yml +100 -0
- data/.rubocop_todo.yml +14 -0
- data/.travis.yml +8 -9
- data/Appraisals +41 -0
- data/CHANGELOG.md +49 -9
- data/Gemfile +1 -1
- data/README.md +130 -109
- data/Rakefile +19 -19
- data/doc/bug_report_template.rb +20 -14
- data/gemfiles/ar3.gemfile +10 -53
- data/gemfiles/ar4.gemfile +7 -0
- data/gemfiles/ar5.gemfile +13 -0
- data/lib/generators/paper_trail/install_generator.rb +26 -18
- data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb +4 -2
- data/lib/generators/paper_trail/templates/add_transaction_id_column_to_versions.rb +2 -0
- data/lib/generators/paper_trail/templates/create_version_associations.rb +9 -4
- data/lib/generators/paper_trail/templates/create_versions.rb +39 -5
- data/lib/paper_trail.rb +169 -146
- data/lib/paper_trail/attributes_serialization.rb +89 -17
- data/lib/paper_trail/cleaner.rb +15 -9
- data/lib/paper_trail/config.rb +28 -11
- data/lib/paper_trail/frameworks/active_record.rb +4 -0
- data/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb +5 -1
- data/lib/paper_trail/frameworks/active_record/models/paper_trail/version_association.rb +6 -2
- data/lib/paper_trail/frameworks/cucumber.rb +1 -0
- data/lib/paper_trail/frameworks/rails.rb +2 -7
- data/lib/paper_trail/frameworks/rails/controller.rb +29 -9
- data/lib/paper_trail/frameworks/rails/engine.rb +7 -1
- data/lib/paper_trail/frameworks/rspec.rb +5 -5
- data/lib/paper_trail/frameworks/rspec/helpers.rb +3 -1
- data/lib/paper_trail/frameworks/sinatra.rb +6 -4
- data/lib/paper_trail/has_paper_trail.rb +199 -106
- data/lib/paper_trail/record_history.rb +1 -3
- data/lib/paper_trail/reifier.rb +297 -118
- data/lib/paper_trail/serializers/json.rb +3 -3
- data/lib/paper_trail/serializers/yaml.rb +27 -8
- data/lib/paper_trail/version_association_concern.rb +3 -1
- data/lib/paper_trail/version_concern.rb +75 -35
- data/lib/paper_trail/version_number.rb +6 -9
- data/paper_trail.gemspec +44 -51
- data/spec/generators/install_generator_spec.rb +24 -25
- data/spec/generators/paper_trail/templates/create_versions_spec.rb +51 -0
- data/spec/models/animal_spec.rb +12 -12
- data/spec/models/boolit_spec.rb +8 -8
- data/spec/models/callback_modifier_spec.rb +47 -47
- data/spec/models/car_spec.rb +13 -0
- data/spec/models/fluxor_spec.rb +3 -3
- data/spec/models/gadget_spec.rb +19 -19
- data/spec/models/joined_version_spec.rb +3 -3
- data/spec/models/json_version_spec.rb +23 -24
- data/spec/models/kitchen/banana_spec.rb +3 -3
- data/spec/models/not_on_update_spec.rb +7 -4
- data/spec/models/post_with_status_spec.rb +13 -3
- data/spec/models/skipper_spec.rb +10 -10
- data/spec/models/thing_spec.rb +4 -4
- data/spec/models/truck_spec.rb +5 -0
- data/spec/models/vehicle_spec.rb +5 -0
- data/spec/models/version_spec.rb +103 -59
- data/spec/models/widget_spec.rb +82 -52
- data/spec/modules/paper_trail_spec.rb +2 -2
- data/spec/modules/version_concern_spec.rb +11 -12
- data/spec/modules/version_number_spec.rb +2 -4
- data/spec/paper_trail/config_spec.rb +10 -29
- data/spec/paper_trail_spec.rb +16 -14
- data/spec/rails_helper.rb +10 -9
- data/spec/requests/articles_spec.rb +11 -7
- data/spec/spec_helper.rb +41 -22
- data/spec/support/alt_db_init.rb +8 -13
- data/test/custom_json_serializer.rb +3 -3
- data/test/dummy/Rakefile +2 -2
- data/test/dummy/app/controllers/application_controller.rb +21 -8
- data/test/dummy/app/controllers/articles_controller.rb +11 -8
- data/test/dummy/app/controllers/widgets_controller.rb +13 -12
- data/test/dummy/app/models/animal.rb +1 -1
- data/test/dummy/app/models/article.rb +19 -11
- data/test/dummy/app/models/authorship.rb +1 -1
- data/test/dummy/app/models/bar_habtm.rb +4 -0
- data/test/dummy/app/models/book.rb +4 -4
- data/test/dummy/app/models/boolit.rb +1 -1
- data/test/dummy/app/models/callback_modifier.rb +6 -6
- data/test/dummy/app/models/car.rb +3 -0
- data/test/dummy/app/models/chapter.rb +4 -4
- data/test/dummy/app/models/customer.rb +1 -1
- data/test/dummy/app/models/document.rb +2 -2
- data/test/dummy/app/models/editor.rb +1 -1
- data/test/dummy/app/models/foo_habtm.rb +4 -0
- data/test/dummy/app/models/fruit.rb +2 -2
- data/test/dummy/app/models/gadget.rb +1 -1
- data/test/dummy/app/models/kitchen/banana.rb +1 -1
- data/test/dummy/app/models/legacy_widget.rb +2 -2
- data/test/dummy/app/models/line_item.rb +1 -1
- data/test/dummy/app/models/not_on_update.rb +1 -1
- data/test/dummy/app/models/person.rb +6 -6
- data/test/dummy/app/models/post.rb +1 -1
- data/test/dummy/app/models/post_with_status.rb +1 -1
- data/test/dummy/app/models/quotation.rb +1 -1
- data/test/dummy/app/models/section.rb +1 -1
- data/test/dummy/app/models/skipper.rb +2 -2
- data/test/dummy/app/models/song.rb +13 -4
- data/test/dummy/app/models/thing.rb +2 -2
- data/test/dummy/app/models/translation.rb +2 -2
- data/test/dummy/app/models/truck.rb +4 -0
- data/test/dummy/app/models/vehicle.rb +4 -0
- data/test/dummy/app/models/whatchamajigger.rb +1 -1
- data/test/dummy/app/models/widget.rb +7 -6
- data/test/dummy/app/versions/joined_version.rb +4 -3
- data/test/dummy/app/versions/json_version.rb +1 -1
- data/test/dummy/app/versions/kitchen/banana_version.rb +1 -1
- data/test/dummy/app/versions/post_version.rb +2 -2
- data/test/dummy/config.ru +1 -1
- data/test/dummy/config/application.rb +20 -9
- data/test/dummy/config/boot.rb +5 -5
- data/test/dummy/config/environment.rb +1 -1
- data/test/dummy/config/environments/development.rb +4 -3
- data/test/dummy/config/environments/production.rb +3 -2
- data/test/dummy/config/environments/test.rb +15 -5
- data/test/dummy/config/initializers/backtrace_silencers.rb +4 -2
- data/test/dummy/config/initializers/paper_trail.rb +1 -2
- data/test/dummy/config/initializers/secret_token.rb +3 -1
- data/test/dummy/config/initializers/session_store.rb +1 -1
- data/test/dummy/config/routes.rb +2 -2
- data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +120 -74
- data/test/dummy/db/schema.rb +29 -6
- data/test/dummy/script/rails +6 -4
- data/test/functional/controller_test.rb +34 -35
- data/test/functional/enabled_for_controller_test.rb +6 -7
- data/test/functional/modular_sinatra_test.rb +43 -38
- data/test/functional/sinatra_test.rb +49 -40
- data/test/functional/thread_safety_test.rb +4 -6
- data/test/paper_trail_test.rb +15 -14
- data/test/test_helper.rb +68 -44
- data/test/time_travel_helper.rb +1 -15
- data/test/unit/associations_test.rb +517 -251
- data/test/unit/cleaner_test.rb +66 -60
- data/test/unit/inheritance_column_test.rb +17 -17
- data/test/unit/model_test.rb +611 -504
- data/test/unit/protected_attrs_test.rb +16 -12
- data/test/unit/serializer_test.rb +44 -43
- data/test/unit/serializers/json_test.rb +17 -18
- data/test/unit/serializers/mixin_json_test.rb +15 -14
- data/test/unit/serializers/mixin_yaml_test.rb +20 -16
- data/test/unit/serializers/yaml_test.rb +12 -13
- data/test/unit/timestamp_test.rb +10 -12
- data/test/unit/version_test.rb +7 -7
- metadata +92 -40
@@ -1,47 +1,51 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class ProtectedAttrsTest < ActiveSupport::TestCase
|
4
4
|
subject { ProtectedWidget.new }
|
5
5
|
|
6
|
-
|
6
|
+
# These ActiveModel matchers (provided by shoulda-matchers) only work for
|
7
|
+
# Rails 3.
|
8
|
+
if ActiveRecord::VERSION::MAJOR < 4
|
7
9
|
accessible_attrs = ProtectedWidget.accessible_attributes.to_a
|
8
10
|
accessible_attrs.each do |attr_name|
|
9
11
|
should allow_mass_assignment_of(attr_name.to_sym)
|
10
12
|
end
|
11
|
-
|
13
|
+
inaccessible = ProtectedWidget.
|
14
|
+
column_names.
|
15
|
+
reject { |column_name| accessible_attrs.include?(column_name) }
|
16
|
+
inaccessible.each do |attr_name|
|
12
17
|
should_not allow_mass_assignment_of(attr_name.to_sym)
|
13
18
|
end
|
14
19
|
end
|
15
20
|
|
16
|
-
context
|
21
|
+
context "A model with `attr_accessible` created" do
|
17
22
|
setup do
|
18
|
-
@widget = ProtectedWidget.create! :
|
23
|
+
@widget = ProtectedWidget.create! name: "Henry"
|
19
24
|
@initial_attributes = @widget.attributes
|
20
25
|
end
|
21
26
|
|
22
|
-
should
|
27
|
+
should "be `nil` in its previous version" do
|
23
28
|
assert_nil @widget.previous_version
|
24
29
|
end
|
25
30
|
|
26
|
-
context
|
31
|
+
context "which is then updated" do
|
27
32
|
setup do
|
28
|
-
@widget.assign_attributes(:
|
33
|
+
@widget.assign_attributes(name: "Jeff", a_text: "Short statement")
|
29
34
|
@widget.an_integer = 42
|
30
35
|
@widget.save!
|
31
36
|
end
|
32
37
|
|
33
|
-
should
|
38
|
+
should "not be `nil` in its previous version" do
|
34
39
|
assert_not_nil @widget.previous_version
|
35
40
|
end
|
36
41
|
|
37
|
-
should
|
42
|
+
should "the previous version should contain right attributes" do
|
38
43
|
# For some reason this test seems to be broken in JRuby 1.9 mode in the
|
39
44
|
# test env even though it works in the console. WTF?
|
40
|
-
unless ActiveRecord::VERSION::MAJOR >= 4 && defined?(JRUBY_VERSION)
|
45
|
+
unless ActiveRecord::VERSION::MAJOR >= 4 && defined?(JRUBY_VERSION)
|
41
46
|
assert_attributes_equal @widget.previous_version.attributes, @initial_attributes
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
45
|
-
|
46
50
|
end
|
47
51
|
end
|
@@ -1,36 +1,34 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "custom_json_serializer"
|
3
3
|
|
4
4
|
class SerializerTest < ActiveSupport::TestCase
|
5
|
-
|
6
|
-
context 'YAML Serializer' do
|
5
|
+
context "YAML Serializer" do
|
7
6
|
setup do
|
8
7
|
Fluxor.instance_eval <<-END
|
9
8
|
has_paper_trail
|
10
9
|
END
|
11
10
|
|
12
|
-
@fluxor = Fluxor.create :
|
13
|
-
|
14
|
-
|
11
|
+
@fluxor = Fluxor.create name: "Some text."
|
12
|
+
|
13
|
+
# this is exactly what PaperTrail serializes
|
14
|
+
@original_fluxor_attributes = @fluxor.send(:attributes_before_change)
|
15
|
+
|
16
|
+
@fluxor.update_attributes name: "Some more text."
|
15
17
|
end
|
16
18
|
|
17
|
-
should
|
19
|
+
should "work with the default `YAML` serializer" do
|
18
20
|
# Normal behaviour
|
19
21
|
assert_equal 2, @fluxor.versions.length
|
20
22
|
assert_nil @fluxor.versions[0].reify
|
21
|
-
assert_equal
|
23
|
+
assert_equal "Some text.", @fluxor.versions[1].reify.name
|
22
24
|
|
23
25
|
# Check values are stored as `YAML`.
|
24
26
|
assert_equal @original_fluxor_attributes, YAML.load(@fluxor.versions[1].object)
|
25
|
-
|
26
|
-
# attributes in the `YAML` can't be ensured.
|
27
|
-
if RUBY_VERSION >= '1.9'
|
28
|
-
assert_equal YAML.dump(@original_fluxor_attributes), @fluxor.versions[1].object
|
29
|
-
end
|
27
|
+
assert_equal YAML.dump(@original_fluxor_attributes), @fluxor.versions[1].object
|
30
28
|
end
|
31
29
|
end
|
32
30
|
|
33
|
-
context
|
31
|
+
context "JSON Serializer" do
|
34
32
|
setup do
|
35
33
|
PaperTrail.configure do |config|
|
36
34
|
config.serializer = PaperTrail::Serializers::JSON
|
@@ -40,39 +38,40 @@ class SerializerTest < ActiveSupport::TestCase
|
|
40
38
|
has_paper_trail
|
41
39
|
END
|
42
40
|
|
43
|
-
@fluxor = Fluxor.create :
|
44
|
-
|
45
|
-
|
41
|
+
@fluxor = Fluxor.create name: "Some text."
|
42
|
+
|
43
|
+
# this is exactly what PaperTrail serializes
|
44
|
+
@original_fluxor_attributes = @fluxor.send(:attributes_before_change)
|
45
|
+
|
46
|
+
@fluxor.update_attributes name: "Some more text."
|
46
47
|
end
|
47
48
|
|
48
49
|
teardown do
|
49
50
|
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
|
50
51
|
end
|
51
52
|
|
52
|
-
should
|
53
|
+
should "reify with JSON serializer" do
|
53
54
|
# Normal behaviour
|
54
55
|
assert_equal 2, @fluxor.versions.length
|
55
56
|
assert_nil @fluxor.versions[0].reify
|
56
|
-
assert_equal
|
57
|
+
assert_equal "Some text.", @fluxor.versions[1].reify.name
|
57
58
|
|
58
59
|
# Check values are stored as JSON.
|
59
|
-
assert_equal @original_fluxor_attributes,
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
assert_equal ActiveSupport::JSON.encode(@original_fluxor_attributes), @fluxor.versions[1].object
|
64
|
-
end
|
60
|
+
assert_equal @original_fluxor_attributes,
|
61
|
+
ActiveSupport::JSON.decode(@fluxor.versions[1].object)
|
62
|
+
assert_equal ActiveSupport::JSON.encode(@original_fluxor_attributes),
|
63
|
+
@fluxor.versions[1].object
|
65
64
|
end
|
66
65
|
|
67
|
-
should
|
68
|
-
initial_changeset = {"name" => [nil, "Some text."], "id" => [nil, @fluxor.id]}
|
69
|
-
second_changeset =
|
66
|
+
should "store object_changes" do
|
67
|
+
initial_changeset = { "name" => [nil, "Some text."], "id" => [nil, @fluxor.id] }
|
68
|
+
second_changeset = { "name" => ["Some text.", "Some more text."] }
|
70
69
|
assert_equal initial_changeset, @fluxor.versions[0].changeset
|
71
70
|
assert_equal second_changeset, @fluxor.versions[1].changeset
|
72
71
|
end
|
73
72
|
end
|
74
73
|
|
75
|
-
context
|
74
|
+
context "Custom Serializer" do
|
76
75
|
setup do
|
77
76
|
PaperTrail.configure do |config|
|
78
77
|
config.serializer = CustomJsonSerializer
|
@@ -83,35 +82,37 @@ class SerializerTest < ActiveSupport::TestCase
|
|
83
82
|
END
|
84
83
|
|
85
84
|
@fluxor = Fluxor.create
|
86
|
-
|
87
|
-
|
85
|
+
|
86
|
+
# this is exactly what PaperTrail serializes
|
87
|
+
@original_fluxor_attributes = @fluxor.
|
88
|
+
send(:attributes_before_change).
|
89
|
+
reject { |_k, v| v.nil? }
|
90
|
+
|
91
|
+
@fluxor.update_attributes name: "Some more text."
|
88
92
|
end
|
89
93
|
|
90
94
|
teardown do
|
91
95
|
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
|
92
96
|
end
|
93
97
|
|
94
|
-
should
|
98
|
+
should "reify with custom serializer" do
|
95
99
|
# Normal behaviour
|
96
100
|
assert_equal 2, @fluxor.versions.length
|
97
101
|
assert_nil @fluxor.versions[0].reify
|
98
102
|
assert_nil @fluxor.versions[1].reify.name
|
99
103
|
|
100
104
|
# Check values are stored as JSON.
|
101
|
-
assert_equal @original_fluxor_attributes,
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
assert_equal ActiveSupport::JSON.encode(@original_fluxor_attributes), @fluxor.versions[1].object
|
106
|
-
end
|
105
|
+
assert_equal @original_fluxor_attributes,
|
106
|
+
ActiveSupport::JSON.decode(@fluxor.versions[1].object)
|
107
|
+
assert_equal ActiveSupport::JSON.encode(@original_fluxor_attributes),
|
108
|
+
@fluxor.versions[1].object
|
107
109
|
end
|
108
110
|
|
109
|
-
should
|
110
|
-
initial_changeset = {"id" => [nil, @fluxor.id]}
|
111
|
-
second_changeset =
|
111
|
+
should "store object_changes" do
|
112
|
+
initial_changeset = { "id" => [nil, @fluxor.id] }
|
113
|
+
second_changeset = { "name" => [nil, "Some more text."] }
|
112
114
|
assert_equal initial_changeset, @fluxor.versions[0].changeset
|
113
115
|
assert_equal second_changeset, @fluxor.versions[1].changeset
|
114
116
|
end
|
115
117
|
end
|
116
|
-
|
117
118
|
end
|
@@ -1,50 +1,49 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class JSONTest < ActiveSupport::TestCase
|
4
|
-
|
5
4
|
setup do
|
6
5
|
# Setup a hash with random values
|
7
6
|
@hash = {}
|
8
7
|
(1..4).each do |i|
|
9
|
-
@hash["key#{i}"] =
|
8
|
+
@hash["key#{i}"] = FFaker::Lorem.word
|
10
9
|
end
|
11
10
|
@hash_as_json = @hash.to_json
|
12
11
|
# Setup an array of random words
|
13
12
|
@array = []
|
14
|
-
(rand(5) + 4).times { @array <<
|
13
|
+
(rand(5) + 4).times { @array << FFaker::Lorem.word }
|
15
14
|
@array_as_json = @array.to_json
|
16
15
|
end
|
17
16
|
|
18
|
-
context
|
19
|
-
should
|
17
|
+
context "`load` class method" do
|
18
|
+
should "exist" do
|
20
19
|
assert PaperTrail::Serializers::JSON.respond_to?(:load)
|
21
20
|
end
|
22
21
|
|
23
|
-
should
|
22
|
+
should "`deserialize` JSON to Ruby" do
|
24
23
|
assert_equal @hash, PaperTrail::Serializers::JSON.load(@hash_as_json)
|
25
24
|
assert_equal @array, PaperTrail::Serializers::JSON.load(@array_as_json)
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
29
|
-
context
|
30
|
-
should
|
28
|
+
context "`dump` class method" do
|
29
|
+
should "exist" do
|
31
30
|
assert PaperTrail::Serializers::JSON.respond_to?(:dump)
|
32
31
|
end
|
33
32
|
|
34
|
-
should
|
33
|
+
should "`serialize` Ruby to JSON" do
|
35
34
|
assert_equal @hash_as_json, PaperTrail::Serializers::JSON.dump(@hash)
|
36
35
|
assert_equal @array_as_json, PaperTrail::Serializers::JSON.dump(@array)
|
37
36
|
end
|
38
37
|
end
|
39
38
|
|
40
|
-
context
|
39
|
+
context "`where_object` class method" do
|
41
40
|
context "when value is a string" do
|
42
|
-
should
|
41
|
+
should "construct correct WHERE query" do
|
43
42
|
matches = PaperTrail::Serializers::JSON.where_object_condition(
|
44
43
|
PaperTrail::Version.arel_table[:object], :arg1, "Val 1")
|
45
44
|
|
46
45
|
assert matches.instance_of?(Arel::Nodes::Matches)
|
47
|
-
if Arel::VERSION >=
|
46
|
+
if Arel::VERSION >= "6"
|
48
47
|
assert_equal matches.right.val, "%\"arg1\":\"Val 1\"%"
|
49
48
|
else
|
50
49
|
assert_equal matches.right, "%\"arg1\":\"Val 1\"%"
|
@@ -53,12 +52,12 @@ class JSONTest < ActiveSupport::TestCase
|
|
53
52
|
end
|
54
53
|
|
55
54
|
context "when value is `null`" do
|
56
|
-
should
|
55
|
+
should "construct correct WHERE query" do
|
57
56
|
matches = PaperTrail::Serializers::JSON.where_object_condition(
|
58
57
|
PaperTrail::Version.arel_table[:object], :arg1, nil)
|
59
58
|
|
60
59
|
assert matches.instance_of?(Arel::Nodes::Matches)
|
61
|
-
if Arel::VERSION >=
|
60
|
+
if Arel::VERSION >= "6"
|
62
61
|
assert_equal matches.right.val, "%\"arg1\":null%"
|
63
62
|
else
|
64
63
|
assert_equal matches.right, "%\"arg1\":null%"
|
@@ -67,15 +66,15 @@ class JSONTest < ActiveSupport::TestCase
|
|
67
66
|
end
|
68
67
|
|
69
68
|
context "when value is a number" do
|
70
|
-
should
|
69
|
+
should "construct correct WHERE query" do
|
71
70
|
grouping = PaperTrail::Serializers::JSON.where_object_condition(
|
72
71
|
PaperTrail::Version.arel_table[:object], :arg1, -3.5)
|
73
72
|
|
74
73
|
assert grouping.instance_of?(Arel::Nodes::Grouping)
|
75
74
|
matches = grouping.select { |v| v.instance_of?(Arel::Nodes::Matches) }
|
76
|
-
# Numeric arguments need to ensure that they match for only the number, not the beginning
|
75
|
+
# Numeric arguments need to ensure that they match for only the number, not the beginning
|
77
76
|
# of a #, so it uses an Grouping matcher (See notes on `PaperTrail::Serializers::JSON`)
|
78
|
-
if Arel::VERSION >=
|
77
|
+
if Arel::VERSION >= "6"
|
79
78
|
assert_equal matches.first.right.val, "%\"arg1\":-3.5,%"
|
80
79
|
assert_equal matches.last.right.val, "%\"arg1\":-3.5}%"
|
81
80
|
else
|
@@ -1,36 +1,37 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "test_helper"
|
2
|
+
require "custom_json_serializer"
|
3
3
|
|
4
4
|
class MixinJsonTest < ActiveSupport::TestCase
|
5
|
-
|
6
5
|
setup do
|
7
6
|
# Setup a hash with random values, ensuring some values are nil
|
8
7
|
@hash = {}
|
9
8
|
(1..4).each do |i|
|
10
|
-
@hash["key#{i}"] = [
|
9
|
+
@hash["key#{i}"] = [FFaker::Lorem.word, nil].sample
|
11
10
|
end
|
12
|
-
@hash[
|
13
|
-
@hash[
|
11
|
+
@hash["tkey"] = nil
|
12
|
+
@hash[""] = "foo"
|
14
13
|
@hash_as_json = @hash.to_json
|
15
14
|
end
|
16
15
|
|
17
|
-
context
|
18
|
-
should
|
16
|
+
context "`load` class method" do
|
17
|
+
should "exist" do
|
19
18
|
assert CustomJsonSerializer.respond_to?(:load)
|
20
19
|
end
|
21
20
|
|
22
|
-
should
|
23
|
-
assert_equal @hash.reject { |k,v| k.blank? || v.blank? },
|
21
|
+
should "`deserialize` JSON to Ruby, removing pairs with `blank` keys or values" do
|
22
|
+
assert_equal @hash.reject { |k, v| k.blank? || v.blank? },
|
23
|
+
CustomJsonSerializer.load(@hash_as_json)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
context
|
28
|
-
should
|
27
|
+
context "`dump` class method" do
|
28
|
+
should "exist" do
|
29
29
|
assert CustomJsonSerializer.respond_to?(:dump)
|
30
30
|
end
|
31
31
|
|
32
|
-
should
|
33
|
-
assert_equal @hash.reject { |
|
32
|
+
should "`serialize` Ruby to JSON, removing pairs with `nil` values" do
|
33
|
+
assert_equal @hash.reject { |_k, v| v.nil? }.to_json,
|
34
|
+
CustomJsonSerializer.dump(@hash)
|
34
35
|
end
|
35
36
|
end
|
36
37
|
end
|
@@ -1,49 +1,53 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
module CustomYamlSerializer
|
4
4
|
extend PaperTrail::Serializers::YAML
|
5
5
|
|
6
6
|
def self.load(string)
|
7
7
|
parsed_value = super(string)
|
8
|
-
parsed_value.is_a?(Hash)
|
8
|
+
if parsed_value.is_a?(Hash)
|
9
|
+
parsed_value.reject { |k, v| k.blank? || v.blank? }
|
10
|
+
else
|
11
|
+
parsed_value
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
def self.dump(object)
|
12
|
-
object.is_a?(Hash) ? super(object.reject { |
|
16
|
+
object.is_a?(Hash) ? super(object.reject { |_k, v| v.nil? }) : super
|
13
17
|
end
|
14
18
|
end
|
15
19
|
|
16
20
|
class MixinYamlTest < ActiveSupport::TestCase
|
17
|
-
|
18
21
|
setup do
|
19
22
|
# Setup a hash with random values, ensuring some values are nil
|
20
23
|
@hash = {}
|
21
24
|
(1..4).each do |i|
|
22
|
-
@hash["key#{i}"] = [
|
25
|
+
@hash["key#{i}"] = [FFaker::Lorem.word, nil].sample
|
23
26
|
end
|
24
|
-
@hash[
|
25
|
-
@hash[
|
27
|
+
@hash["tkey"] = nil
|
28
|
+
@hash[""] = "foo"
|
26
29
|
@hash_as_yaml = @hash.to_yaml
|
27
30
|
end
|
28
31
|
|
29
|
-
context
|
30
|
-
should
|
32
|
+
context "`load` class method" do
|
33
|
+
should "exist" do
|
31
34
|
assert CustomYamlSerializer.respond_to?(:load)
|
32
35
|
end
|
33
36
|
|
34
|
-
should
|
35
|
-
assert_equal @hash.reject { |k,v| k.blank? || v.blank? },
|
37
|
+
should "`deserialize` YAML to Ruby, removing pairs with `blank` keys or values" do
|
38
|
+
assert_equal @hash.reject { |k, v| k.blank? || v.blank? },
|
39
|
+
CustomYamlSerializer.load(@hash_as_yaml)
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
39
|
-
context
|
40
|
-
should
|
43
|
+
context "`dump` class method" do
|
44
|
+
should "exist" do
|
41
45
|
assert CustomYamlSerializer.respond_to?(:dump)
|
42
46
|
end
|
43
47
|
|
44
|
-
should
|
45
|
-
assert_equal @hash.reject { |
|
48
|
+
should "`serialize` Ruby to YAML, removing pairs with `nil` values" do
|
49
|
+
assert_equal @hash.reject { |_k, v| v.nil? }.to_yaml,
|
50
|
+
CustomYamlSerializer.dump(@hash)
|
46
51
|
end
|
47
52
|
end
|
48
|
-
|
49
53
|
end
|
@@ -1,48 +1,47 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class YamlTest < ActiveSupport::TestCase
|
4
|
-
|
5
4
|
setup do
|
6
5
|
# Setup a hash with random values
|
7
6
|
@hash = {}
|
8
7
|
(1..4).each do |i|
|
9
|
-
@hash["key#{i}".to_sym] =
|
8
|
+
@hash["key#{i}".to_sym] = FFaker::Lorem.word
|
10
9
|
end
|
11
10
|
@hash_as_yaml = @hash.to_yaml
|
12
11
|
# Setup an array of random words
|
13
12
|
@array = []
|
14
|
-
(rand(5) + 4).times { @array <<
|
13
|
+
(rand(5) + 4).times { @array << FFaker::Lorem.word }
|
15
14
|
@array_as_yaml = @array.to_yaml
|
16
15
|
end
|
17
16
|
|
18
|
-
context
|
19
|
-
should
|
17
|
+
context "`load` class method" do
|
18
|
+
should "exist" do
|
20
19
|
assert PaperTrail::Serializers::YAML.respond_to?(:load)
|
21
20
|
end
|
22
21
|
|
23
|
-
should
|
22
|
+
should "deserialize `YAML` to Ruby" do
|
24
23
|
assert_equal @hash, PaperTrail::Serializers::YAML.load(@hash_as_yaml)
|
25
24
|
assert_equal @array, PaperTrail::Serializers::YAML.load(@array_as_yaml)
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
29
|
-
context
|
30
|
-
should
|
28
|
+
context "`dump` class method" do
|
29
|
+
should "exist" do
|
31
30
|
assert PaperTrail::Serializers::YAML.respond_to?(:dump)
|
32
31
|
end
|
33
32
|
|
34
|
-
should
|
33
|
+
should "serialize Ruby to `YAML`" do
|
35
34
|
assert_equal @hash_as_yaml, PaperTrail::Serializers::YAML.dump(@hash)
|
36
35
|
assert_equal @array_as_yaml, PaperTrail::Serializers::YAML.dump(@array)
|
37
36
|
end
|
38
37
|
end
|
39
38
|
|
40
|
-
context
|
41
|
-
should
|
39
|
+
context "`where_object` class method" do
|
40
|
+
should "construct correct WHERE query" do
|
42
41
|
matches = PaperTrail::Serializers::YAML.where_object_condition(
|
43
42
|
PaperTrail::Version.arel_table[:object], :arg1, "Val 1")
|
44
43
|
assert matches.instance_of?(Arel::Nodes::Matches)
|
45
|
-
if Arel::VERSION >=
|
44
|
+
if Arel::VERSION >= "6"
|
46
45
|
assert_equal matches.right.val, "%\narg1: Val 1\n%"
|
47
46
|
else
|
48
47
|
assert_equal matches.right, "%\narg1: Val 1\n%"
|