diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 4b169cda..902038fb 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -48,6 +48,10 @@ cd ../.. # Run tests DB=sqlite bundle exec appraisal ar-4.2 rake + +# Run a single test file +DB=sqlite bundle exec appraisal ar-4.2 ruby -I test test/unit/associations_test.rb +DB=sqlite bundle exec appraisal ar-4.2 rspec spec/paper_trail/serializers/json_spec.rb ``` ### Test sqlite, AR 5 diff --git a/.rubocop.yml b/.rubocop.yml index a872758a..9e1f51b7 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -15,7 +15,7 @@ AllCops: - test/dummy/db/schema.rb # Generated, out of our control # Set to lowest supported version - TargetRubyVersion: 1.9 + TargetRubyVersion: 2.1 # Migrations often contain long up/down methods, and extracting smaller methods # from these is of questionable value. @@ -23,6 +23,10 @@ Metrics/AbcSize: Exclude: - 'test/dummy/db/migrate/*' +# Not a useful metric compared to, e.g. `AbcSize`. +Metrics/BlockLength: + Enabled: false + # Not a useful metric compared to, e.g. `AbcSize`. Metrics/ClassLength: Enabled: false @@ -43,6 +47,13 @@ Metrics/MethodLength: Metrics/ModuleLength: Enabled: false +# In an ideal world, each example has a single expectation. In the real world, +# sometimes setup is a pain and you don't want to duplicate setup in multiple +# examples, or make the specs more confusing with many nested `context`s, and +# the practical thing is to have multiple expectations. +RSpec/MultipleExpectations: + Enabled: false + Style/AlignParameters: EnforcedStyle: with_fixed_indentation @@ -59,17 +70,25 @@ Style/DotPosition: Style/DoubleNegation: Enabled: false +Style/FileName: + Exclude: + - Appraisals + # The decision of when to use a guard clause to improve readability is subtle, # and it's not clear that it can be linted. Certainly, the default # `MinBodyLength` of 1 can actually hurt readability. Style/GuardClause: - MinBodyLength: 3 + Enabled: false # Use postfix (modifier) conditionals for one-liners, unless doing so would # exceed 60 characters. Style/IfUnlessModifier: MaxLineLength: 60 +Style/IndentHeredoc: + Exclude: + - paper_trail.gemspec + # The Ruby Style Guide says: # # > Use \ instead of + or << to concatenate two string literals at line end. diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index d3e8dfc9..fb310989 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -3,6 +3,9 @@ require: rubocop-rspec # Remove these configuration records # one by one as the offenses are removed from the code base. +Lint/AmbiguousBlockAssociation: + Enabled: false + Metrics/AbcSize: Max: 22 # Goal: 15 @@ -15,20 +18,35 @@ Metrics/PerceivedComplexity: Style/FrozenStringLiteralComment: Enabled: false -RSpec/InstanceVariable: - Enabled: false - -RSpec/ExampleWording: +RSpec/BeforeAfterAll: Enabled: false RSpec/DescribedClass: Enabled: false +RSpec/ExampleWording: + Enabled: false + RSpec/ExampleLength: Enabled: false +RSpec/ExpectActual: + Enabled: false + +RSpec/FilePath: + Enabled: false + +RSpec/MessageSpies: + Enabled: false + +RSpec/NamedSubject: + Enabled: false + +RSpec/NestedGroups: + Enabled: false + RSpec/NotToNot: Enabled: false -RSpec/FilePath: +Security/YAMLLoad: Enabled: false diff --git a/Appraisals b/Appraisals index 12009e91..339dd857 100644 --- a/Appraisals +++ b/Appraisals @@ -18,7 +18,7 @@ end appraise "ar-5.0" do gem "activerecord", "~> 5.0.0" gem "rspec-rails", "~> 3.5.1" - gem 'rails-controller-testing' + gem "rails-controller-testing" end appraise "ar_master" do diff --git a/Rakefile b/Rakefile index 77e36939..e256dd34 100644 --- a/Rakefile +++ b/Rakefile @@ -27,4 +27,4 @@ require "rubocop/rake_task" RuboCop::RakeTask.new desc "Default: run all available test suites" -task default: [:rubocop, :prepare, :test, :spec] +task default: %i(rubocop prepare test spec) diff --git a/doc/bug_report_template.rb b/doc/bug_report_template.rb index cdcae1e1..2f17675a 100644 --- a/doc/bug_report_template.rb +++ b/doc/bug_report_template.rb @@ -34,7 +34,7 @@ ActiveRecord::Schema.define do t.integer :transaction_id t.datetime :created_at end - add_index :versions, [:item_type, :item_id] + add_index :versions, %i(item_type item_id) add_index :versions, [:transaction_id] create_table :version_associations do |t| @@ -43,7 +43,7 @@ ActiveRecord::Schema.define do t.integer :foreign_key_id end add_index :version_associations, [:version_id] - add_index :version_associations, [:foreign_key_name, :foreign_key_id], + add_index :version_associations, %i(foreign_key_name foreign_key_id), name: "index_version_associations_on_foreign_key" end ActiveRecord::Base.logger = Logger.new(STDOUT) diff --git a/lib/generators/paper_trail/templates/create_version_associations.rb b/lib/generators/paper_trail/templates/create_version_associations.rb index e15132c3..7009a5ae 100644 --- a/lib/generators/paper_trail/templates/create_version_associations.rb +++ b/lib/generators/paper_trail/templates/create_version_associations.rb @@ -9,7 +9,7 @@ class CreateVersionAssociations < ActiveRecord::Migration end add_index :version_associations, [:version_id] add_index :version_associations, - [:foreign_key_name, :foreign_key_id], + %i(foreign_key_name foreign_key_id), name: "index_version_associations_on_foreign_key" end diff --git a/lib/generators/paper_trail/templates/create_versions.rb b/lib/generators/paper_trail/templates/create_versions.rb index 47301d56..900cb1a7 100644 --- a/lib/generators/paper_trail/templates/create_versions.rb +++ b/lib/generators/paper_trail/templates/create_versions.rb @@ -38,7 +38,7 @@ class CreateVersions < ActiveRecord::Migration # t.datetime :created_at end - add_index :versions, [:item_type, :item_id] + add_index :versions, %i(item_type item_id) end private diff --git a/lib/paper_trail/model_config.rb b/lib/paper_trail/model_config.rb index d4ef0fb6..f626cb98 100644 --- a/lib/paper_trail/model_config.rb +++ b/lib/paper_trail/model_config.rb @@ -76,7 +76,7 @@ module PaperTrail # "class attributes", instance methods, and more. # @api private def setup(options = {}) - options[:on] ||= [:create, :update, :destroy] + options[:on] ||= %i(create update destroy) options[:on] = Array(options[:on]) # Support single symbol @model_class.send :include, ::PaperTrail::Model::InstanceMethods if ::ActiveRecord::VERSION::STRING < "4.2" @@ -167,7 +167,7 @@ module PaperTrail @model_class.class_attribute :paper_trail_options @model_class.paper_trail_options = options.dup - [:ignore, :skip, :only].each do |k| + %i(ignore skip only).each do |k| @model_class.paper_trail_options[k] = [@model_class.paper_trail_options[k]]. flatten. compact. diff --git a/lib/paper_trail/serializers/yaml.rb b/lib/paper_trail/serializers/yaml.rb index e054e969..ee54e6cb 100644 --- a/lib/paper_trail/serializers/yaml.rb +++ b/lib/paper_trail/serializers/yaml.rb @@ -24,33 +24,10 @@ module PaperTrail # value in the serialized `object_changes`. def where_object_changes_condition(arel_field, field, value) # Need to check first (before) and secondary (after) fields - m1 = nil - m2 = nil - case yaml_engine_id - when :psych - m1 = "%\n#{field}:\n- #{value}\n%" - m2 = "%\n#{field}:\n-%\n- #{value}\n%" - when :syck - # Syck adds extra spaces into array dumps - m1 = "%\n#{field}: \n%- #{value}\n%" - m2 = "%\n#{field}: \n-%\n- #{value}\n%" - else - raise "Unknown yaml engine" - end + m1 = "%\n#{field}:\n- #{value}\n%" + m2 = "%\n#{field}:\n-%\n- #{value}\n%" arel_field.matches(m1).or(arel_field.matches(m2)) end - - # Returns a symbol identifying the YAML engine. Syck was removed from - # the ruby stdlib in ruby 2.0, but is still available as a gem. - # @api private - def yaml_engine_id - if (defined?(::YAML::ENGINE) && ::YAML::ENGINE.yamler == "psych") || - (defined?(::Psych) && ::YAML == ::Psych) - :psych - else - :syck - end - end end end end diff --git a/lib/paper_trail/version_concern.rb b/lib/paper_trail/version_concern.rb index 7718eb37..5ab7326b 100644 --- a/lib/paper_trail/version_concern.rb +++ b/lib/paper_trail/version_concern.rb @@ -162,13 +162,13 @@ module PaperTrail # Returns whether the `object` column is using the `json` type supported # by PostgreSQL. def object_col_is_json? - [:json, :jsonb].include?(columns_hash["object"].type) + %i(json jsonb).include?(columns_hash["object"].type) end # Returns whether the `object_changes` column is using the `json` type # supported by PostgreSQL. def object_changes_col_is_json? - [:json, :jsonb].include?(columns_hash["object_changes"].try(:type)) + %i(json jsonb).include?(columns_hash["object_changes"].try(:type)) end end @@ -306,8 +306,7 @@ module PaperTrail end end - # Checks that a value has been set for the `version_limit` config - # option, and if so enforces it. + # Enforces the `version_limit`, if set. Default: no limit. # @api private def enforce_version_limit! limit = PaperTrail.config.version_limit diff --git a/paper_trail.gemspec b/paper_trail.gemspec index 07219a68..b2c72d44 100644 --- a/paper_trail.gemspec +++ b/paper_trail.gemspec @@ -41,8 +41,8 @@ has been destroyed. s.add_development_dependency "generator_spec", "~> 0.9.3" s.add_development_dependency "database_cleaner", "~> 1.2" s.add_development_dependency "pry-nav", "~> 0.2.4" - s.add_development_dependency "rubocop", "~> 0.41.2" - s.add_development_dependency "rubocop-rspec", "~> 1.5.1" + s.add_development_dependency "rubocop", "~> 0.48.0" + s.add_development_dependency "rubocop-rspec", "~> 1.15.0" s.add_development_dependency "timecop", "~> 0.8.0" s.add_development_dependency "sqlite3", "~> 1.2" s.add_development_dependency "pg", "~> 0.19.0" diff --git a/spec/models/boolit_spec.rb b/spec/models/boolit_spec.rb index 543d48dd..c5d6fb93 100644 --- a/spec/models/boolit_spec.rb +++ b/spec/models/boolit_spec.rb @@ -10,6 +10,7 @@ describe Boolit, type: :model do describe "Versioning", versioning: true do subject { Boolit.create! } + before { subject.update_attributes!(name: FFaker::Name.name) } it "should have versions" do diff --git a/spec/models/callback_modifier_spec.rb b/spec/models/callback_modifier_spec.rb index bb7134ad..9956ad7a 100644 --- a/spec/models/callback_modifier_spec.rb +++ b/spec/models/callback_modifier_spec.rb @@ -62,7 +62,7 @@ describe CallbackModifier, type: :model do context "when no callback-method used" do it "should set paper_trail_options[:on] to [:create, :update, :destroy]" do modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence) - expect(modifier.paper_trail_options[:on]).to eq [:create, :update, :destroy] + expect(modifier.paper_trail_options[:on]).to eq %i(create update destroy) end it "should default to track destroy" do diff --git a/spec/models/version_spec.rb b/spec/models/version_spec.rb index 16784ad2..41b8d040 100644 --- a/spec/models/version_spec.rb +++ b/spec/models/version_spec.rb @@ -46,13 +46,15 @@ describe PaperTrail::Version, type: :model do end context "Has previous version", versioning: true do + subject { widget.versions.last } + let(:name) { FFaker::Name.name } let(:widget) { Widget.create!(name: FFaker::Name.name) } + before do widget.versions.first.update_attributes!(whodunnit: name) widget.update_attributes!(name: FFaker::Name.first_name) end - subject { widget.versions.last } specify { expect(subject.previous).to be_instance_of(PaperTrail::Version) } @@ -79,9 +81,10 @@ describe PaperTrail::Version, type: :model do end describe "#terminator" do - let(:attributes) { { whodunnit: FFaker::Name.first_name } } subject { PaperTrail::Version.new attributes } + let(:attributes) { { whodunnit: FFaker::Name.first_name } } + it { is_expected.to respond_to(:terminator) } it "is an alias for the `whodunnit` attribute" do diff --git a/spec/models/widget_spec.rb b/spec/models/widget_spec.rb index f2caf427..422fd0b7 100644 --- a/spec/models/widget_spec.rb +++ b/spec/models/widget_spec.rb @@ -51,7 +51,7 @@ describe Widget, type: :model do end describe "Callbacks", versioning: true do - describe :before_save do + describe "before_save" do context ":on => :update" do before { widget.update_attributes!(name: "Foobar") } @@ -66,7 +66,7 @@ describe Widget, type: :model do end end - describe :after_create do + describe "after_create" do let(:widget) { Widget.create!(name: "Foobar", created_at: Time.now - 1.week) } it "corresponding version should use the widget's `updated_at`" do @@ -74,7 +74,7 @@ describe Widget, type: :model do end end - describe :after_update do + describe "after_update" do before { widget.update_attributes!(name: "Foobar", updated_at: Time.now + 1.week) } subject { widget.versions.last.reify } @@ -91,7 +91,7 @@ describe Widget, type: :model do end end - describe :after_destroy do + describe "after_destroy" do it "should create a version for that event" do expect { widget.destroy }.to change(widget.versions, :count).by(1) end @@ -104,7 +104,7 @@ describe Widget, type: :model do end end - describe :after_rollback do + describe "after_rollback" do let(:rolled_back_name) { "Big Moo" } before do @@ -194,7 +194,10 @@ describe Widget, type: :model do describe "return value" do let(:orig_name) { FFaker::Name.name } let(:new_name) { FFaker::Name.name } - before { PaperTrail.whodunnit = orig_name } + + before do + PaperTrail.whodunnit = orig_name + end context "accessed from live model instance" do specify { expect(widget.paper_trail).to be_live } diff --git a/spec/modules/version_concern_spec.rb b/spec/modules/version_concern_spec.rb index 0611c071..10574084 100644 --- a/spec/modules/version_concern_spec.rb +++ b/spec/modules/version_concern_spec.rb @@ -18,14 +18,11 @@ describe PaperTrail::VersionConcern do end describe "persistence", versioning: true do - before do - @foo_doc = Foo::Document.create!(name: "foobar") - @bar_doc = Bar::Document.create!(name: "raboof") - end - it "should store versions in the correct corresponding db location" do - expect(@foo_doc.versions.first).to be_instance_of(Foo::Version) - expect(@bar_doc.versions.first).to be_instance_of(Bar::Version) + foo_doc = Foo::Document.create!(name: "foobar") + bar_doc = Bar::Document.create!(name: "raboof") + expect(foo_doc.versions.first).to be_instance_of(Foo::Version) + expect(bar_doc.versions.first).to be_instance_of(Bar::Version) end end end diff --git a/spec/modules/version_number_spec.rb b/spec/modules/version_number_spec.rb index 85896bfe..632b2ba4 100644 --- a/spec/modules/version_number_spec.rb +++ b/spec/modules/version_number_spec.rb @@ -4,25 +4,25 @@ describe PaperTrail::VERSION do describe "Constants" do subject { PaperTrail::VERSION } - describe :MAJOR do + describe "MAJOR" do it { is_expected.to be_const_defined(:MAJOR) } it { expect(subject::MAJOR).to be_a(Integer) } end - describe :MINOR do + describe "MINOR" do it { is_expected.to be_const_defined(:MINOR) } it { expect(subject::MINOR).to be_a(Integer) } end - describe :TINY do + describe "TINY" do it { is_expected.to be_const_defined(:TINY) } it { expect(subject::TINY).to be_a(Integer) } end - describe :PRE do + describe "PRE" do it { is_expected.to be_const_defined(:PRE) } if PaperTrail::VERSION::PRE it { expect(subject::PRE).to be_instance_of(String) } end end - describe :STRING do + describe "STRING" do it { is_expected.to be_const_defined(:STRING) } it { expect(subject::STRING).to be_instance_of(String) } diff --git a/spec/paper_trail/cleaner_spec.rb b/spec/paper_trail/cleaner_spec.rb index e1048c84..747c052f 100644 --- a/spec/paper_trail/cleaner_spec.rb +++ b/spec/paper_trail/cleaner_spec.rb @@ -2,34 +2,36 @@ require "rails_helper" module PaperTrail RSpec.describe Cleaner, versioning: true do - def populate_db! - @animals = [@animal = Animal.new, @dog = Dog.new, @cat = Cat.new] - @animals.each do |animal| - 3.times { animal.update_attribute(:name, FFaker::Name.name) } - end - end - describe "clean_versions!" do + let(:animal) { ::Animal.new } + let(:dog) { ::Dog.new } + let(:cat) { ::Cat.new } + let(:animals) { [animal, dog, cat] } + before do - populate_db! + animals.each do |animal| + 3.times do + animal.update_attribute(:name, FFaker::Name.name) + end + end end it "baseline test setup" do expect(PaperTrail::Version.count).to(eq(9)) - @animals.each { |animal| expect(animal.versions.size).to(eq(3)) } + animals.each { |animal| expect(animal.versions.size).to(eq(3)) } end context "no options provided" do it "removes extra versions for each item" do PaperTrail.clean_versions! expect(PaperTrail::Version.count).to(eq(3)) - @animals.each { |animal| expect(animal.versions.size).to(eq(1)) } + animals.each { |animal| expect(animal.versions.size).to(eq(1)) } end it "removes the earliest version(s)" do - before = @animals.map { |animal| animal.versions.last.reify.name } + before = animals.map { |animal| animal.versions.last.reify.name } PaperTrail.clean_versions! - after = @animals.map { |animal| animal.versions.last.reify.name } + after = animals.map { |animal| animal.versions.last.reify.name } expect(after).to(eq(before)) end end @@ -38,44 +40,44 @@ module PaperTrail it "keeps two records, instead of the usual one" do PaperTrail.clean_versions!(keeping: 2) expect(PaperTrail::Version.all.count).to(eq(6)) - @animals.each { |animal| expect(animal.versions.size).to(eq(2)) } + animals.each { |animal| expect(animal.versions.size).to(eq(2)) } end end context "with the :date option" do it "only deletes versions created on the given date" do - @animal.versions.each do |ver| + animal.versions.each do |ver| ver.update_attribute(:created_at, (ver.created_at - 1.day)) end - @date = @animal.versions.first.created_at.to_date - @animal.update_attribute(:name, FFaker::Name.name) + date = animal.versions.first.created_at.to_date + animal.update_attribute(:name, FFaker::Name.name) expect(PaperTrail::Version.count).to(eq(10)) - expect(@animal.versions.size).to(eq(4)) - expect(@animal.paper_trail.versions_between(@date, (@date + 1.day)).size).to(eq(3)) - PaperTrail.clean_versions!(date: @date) + expect(animal.versions.size).to(eq(4)) + expect(animal.paper_trail.versions_between(date, (date + 1.day)).size).to(eq(3)) + PaperTrail.clean_versions!(date: date) expect(PaperTrail::Version.count).to(eq(8)) - expect(@animal.versions.reload.size).to(eq(2)) - expect(@animal.versions.first.created_at.to_date).to(eq(@date)) + expect(animal.versions.reload.size).to(eq(2)) + expect(animal.versions.first.created_at.to_date).to(eq(date)) # Why use `equal?` here instead of something less strict? # Doesn't `to_date` always produce a new date object? - expect(@date.equal?(@animal.versions.last.created_at.to_date)).to eq(false) + expect(date.equal?(animal.versions.last.created_at.to_date)).to eq(false) end end context "with the :item_id option" do context "single ID received" do it "only deletes the versions for the Item with that ID" do - PaperTrail.clean_versions!(item_id: @animal.id) - expect(@animal.versions.size).to(eq(1)) + PaperTrail.clean_versions!(item_id: animal.id) + expect(animal.versions.size).to(eq(1)) expect(PaperTrail::Version.count).to(eq(7)) end end context "collection of IDs received" do it "only deletes versions for the Item(s) with those IDs" do - PaperTrail.clean_versions!(item_id: [@animal.id, @dog.id]) - expect(@animal.versions.size).to(eq(1)) - expect(@dog.versions.size).to(eq(1)) + PaperTrail.clean_versions!(item_id: [animal.id, dog.id]) + expect(animal.versions.size).to(eq(1)) + expect(dog.versions.size).to(eq(1)) expect(PaperTrail::Version.count).to(eq(5)) end end @@ -84,30 +86,31 @@ module PaperTrail context "options combinations" do context ":date" do before do - [@animal, @dog].each do |animal| + [animal, dog].each do |animal| animal.versions.each do |ver| ver.update_attribute(:created_at, (ver.created_at - 1.day)) end animal.update_attribute(:name, FFaker::Name.name) end - @date = @animal.versions.first.created_at.to_date end it "baseline test setup" do + date = animal.versions.first.created_at.to_date expect(PaperTrail::Version.count).to(eq(11)) - [@animal, @dog].each do |animal| + [animal, dog].each do |animal| expect(animal.versions.size).to(eq(4)) - expect(animal.versions.between(@date, (@date + 1.day)).size).to(eq(3)) + expect(animal.versions.between(date, (date + 1.day)).size).to(eq(3)) end end context "and :keeping" do it "restrict cleaning properly" do - PaperTrail.clean_versions!(date: @date, keeping: 2) - [@animal, @dog].each do |animal| + date = animal.versions.first.created_at.to_date + PaperTrail.clean_versions!(date: date, keeping: 2) + [animal, dog].each do |animal| animal.versions.reload expect(animal.versions.size).to(eq(3)) - expect(animal.versions.between(@date, (@date + 1.day)).size).to(eq(2)) + expect(animal.versions.between(date, (date + 1.day)).size).to(eq(2)) end expect(PaperTrail::Version.count).to(eq(9)) end @@ -115,20 +118,22 @@ module PaperTrail context "and :item_id" do it "restrict cleaning properly" do - PaperTrail.clean_versions!(date: @date, item_id: @dog.id) - @dog.versions.reload - expect(@dog.versions.size).to(eq(2)) - expect(@dog.versions.between(@date, (@date + 1.day)).size).to(eq(1)) + date = animal.versions.first.created_at.to_date + PaperTrail.clean_versions!(date: date, item_id: dog.id) + dog.versions.reload + expect(dog.versions.size).to(eq(2)) + expect(dog.versions.between(date, (date + 1.day)).size).to(eq(1)) expect(PaperTrail::Version.count).to(eq(9)) end end context ", :item_id, and :keeping" do it "restrict cleaning properly" do - PaperTrail.clean_versions!(date: @date, item_id: @dog.id, keeping: 2) - @dog.versions.reload - expect(@dog.versions.size).to(eq(3)) - expect(@dog.versions.between(@date, (@date + 1.day)).size).to(eq(2)) + date = animal.versions.first.created_at.to_date + PaperTrail.clean_versions!(date: date, item_id: dog.id, keeping: 2) + dog.versions.reload + expect(dog.versions.size).to(eq(3)) + expect(dog.versions.between(date, (date + 1.day)).size).to(eq(2)) expect(PaperTrail::Version.count).to(eq(10)) end end @@ -136,8 +141,8 @@ module PaperTrail context ":keeping and :item_id" do it "restrict cleaning properly" do - PaperTrail.clean_versions!(keeping: 2, item_id: @animal.id) - expect(@animal.versions.size).to(eq(2)) + PaperTrail.clean_versions!(keeping: 2, item_id: animal.id) + expect(animal.versions.size).to(eq(2)) expect(PaperTrail::Version.count).to(eq(8)) end end diff --git a/spec/paper_trail/serializers/custom_yaml_serializer_spec.rb b/spec/paper_trail/serializers/custom_yaml_serializer_spec.rb index cd438a41..1a14318a 100644 --- a/spec/paper_trail/serializers/custom_yaml_serializer_spec.rb +++ b/spec/paper_trail/serializers/custom_yaml_serializer_spec.rb @@ -18,26 +18,27 @@ module CustomYamlSerializer end RSpec.describe CustomYamlSerializer do - before do - @hash = {} - (1..4).each { |i| @hash["key#{i}"] = [FFaker::Lorem.word, nil].sample } - @hash["tkey"] = nil - @hash[""] = "foo" - @hash_as_yaml = @hash.to_yaml - end + let(:word_hash) { + { + "key1" => ::FFaker::Lorem.word, + "key2" => nil, + "tkey" => nil, + "" => "foo" + } + } context(".load") do it("deserializes YAML to Ruby, removing pairs with blank keys or values") do - expect(CustomYamlSerializer.load(@hash_as_yaml)).to eq( - @hash.reject { |k, v| (k.blank? || v.blank?) } + expect(CustomYamlSerializer.load(word_hash.to_yaml)).to eq( + word_hash.reject { |k, v| (k.blank? || v.blank?) } ) end end context(".dump") do it("serializes Ruby to YAML, removing pairs with nil values") do - expect(CustomYamlSerializer.dump(@hash)).to eq( - @hash.reject { |_k, v| v.nil? }.to_yaml + expect(CustomYamlSerializer.dump(word_hash)).to eq( + word_hash.reject { |_k, v| v.nil? }.to_yaml ) end end diff --git a/spec/paper_trail/serializers/json_spec.rb b/spec/paper_trail/serializers/json_spec.rb index 29c5eec7..f1685d33 100644 --- a/spec/paper_trail/serializers/json_spec.rb +++ b/spec/paper_trail/serializers/json_spec.rb @@ -3,33 +3,29 @@ require "rails_helper" module PaperTrail module Serializers RSpec.describe JSON do - before do - @hash = {} - (1..4).each { |i| @hash["key#{i}"] = FFaker::Lorem.word } - @hash_as_json = @hash.to_json - @array = [] - (rand(5) + 4).times { (@array << FFaker::Lorem.word) } - @array_as_json = @array.to_json - end + let(:word_hash) { + (1..4).each_with_object({}) { |i, a| a["key#{i}"] = ::FFaker::Lorem.word } + } + let(:word_array) { [].fill(0, rand(5) + 4) { ::FFaker::Lorem.word } } describe ".load" do it "deserialize JSON to Ruby" do - expect(PaperTrail::Serializers::JSON.load(@hash_as_json)).to(eq(@hash)) - expect(PaperTrail::Serializers::JSON.load(@array_as_json)).to(eq(@array)) + expect(described_class.load(word_hash.to_json)).to eq(word_hash) + expect(described_class.load(word_array.to_json)).to eq(word_array) end end describe ".dump" do it "serializes Ruby to JSON" do - expect(PaperTrail::Serializers::JSON.dump(@hash)).to(eq(@hash_as_json)) - expect(PaperTrail::Serializers::JSON.dump(@array)).to(eq(@array_as_json)) + expect(described_class.dump(word_hash)).to eq(word_hash.to_json) + expect(described_class.dump(word_array)).to eq(word_array.to_json) end end describe ".where_object_condition" do context "when value is a string" do it "construct correct WHERE query" do - matches = PaperTrail::Serializers::JSON. + matches = described_class. where_object_condition(PaperTrail::Version.arel_table[:object], :arg1, "Val 1") expect(matches.instance_of?(Arel::Nodes::Matches)).to(eq(true)) expect("%\"arg1\":\"Val 1\"%").to(eq(matches.right.val)) @@ -38,7 +34,7 @@ module PaperTrail context "when value is null" do it "construct correct WHERE query" do - matches = PaperTrail::Serializers::JSON. + matches = described_class. where_object_condition(PaperTrail::Version.arel_table[:object], :arg1, nil) expect(matches.instance_of?(Arel::Nodes::Matches)).to(eq(true)) expect("%\"arg1\":null%").to(eq(matches.right.val)) @@ -47,7 +43,7 @@ module PaperTrail context "when value is a number" do it "construct correct WHERE query" do - grouping = PaperTrail::Serializers::JSON. + grouping = described_class. where_object_condition(PaperTrail::Version.arel_table[:object], :arg1, -3.5) expect(grouping.instance_of?(Arel::Nodes::Grouping)).to(eq(true)) matches = grouping.select { |v| v.instance_of?(Arel::Nodes::Matches) } diff --git a/spec/paper_trail/version_limit_spec.rb b/spec/paper_trail/version_limit_spec.rb index b0bce620..36ee46bc 100644 --- a/spec/paper_trail/version_limit_spec.rb +++ b/spec/paper_trail/version_limit_spec.rb @@ -2,11 +2,8 @@ require "rails_helper" module PaperTrail RSpec.describe Cleaner, versioning: true do - before do - @last_limit = PaperTrail.config.version_limit - end after do - PaperTrail.config.version_limit = @last_limit + PaperTrail.config.version_limit = nil end it "cleans up old versions" do diff --git a/spec/paper_trail_spec.rb b/spec/paper_trail_spec.rb index 5b4a6ce4..e5f0fb23 100644 --- a/spec/paper_trail_spec.rb +++ b/spec/paper_trail_spec.rb @@ -61,12 +61,12 @@ describe PaperTrail do end end - describe :version do + describe ".version" do it { expect(PaperTrail).to respond_to(:version) } it { expect(PaperTrail.version).to eq(PaperTrail::VERSION::STRING) } end - describe :whodunnit do + describe ".whodunnit" do before(:all) { PaperTrail.whodunnit = "foobar" } it "should get set to `nil` by default" do @@ -74,7 +74,7 @@ describe PaperTrail do end end - describe :controller_info do + describe ".controller_info" do before(:all) { ::PaperTrail.controller_info = { foo: "bar" } } it "should get set to an empty hash before each test" do diff --git a/test/dummy/app/models/custom_primary_key_record.rb b/test/dummy/app/models/custom_primary_key_record.rb index 0c8e9ed1..8ed95736 100644 --- a/test/dummy/app/models/custom_primary_key_record.rb +++ b/test/dummy/app/models/custom_primary_key_record.rb @@ -1,11 +1,13 @@ require "securerandom" + class CustomPrimaryKeyRecord < ActiveRecord::Base self.primary_key = :uuid has_paper_trail class_name: "CustomPrimaryKeyRecordVersion" - # this unusual default_scope is to test the case of the Version#item association + + # This default_scope is to test the case of the Version#item association # not returning the item due to unmatched default_scope on the model. - default_scope -> { where(name: "custom_primary_key_record") } + default_scope { where(name: "custom_primary_key_record") } before_create do self.uuid ||= SecureRandom.uuid diff --git a/test/dummy/app/models/document.rb b/test/dummy/app/models/document.rb index c79984ab..67056c23 100644 --- a/test/dummy/app/models/document.rb +++ b/test/dummy/app/models/document.rb @@ -1,6 +1,6 @@ class Document < ActiveRecord::Base has_paper_trail( versions: :paper_trail_versions, - on: [:create, :update] + on: %i(create update) ) end diff --git a/test/dummy/app/models/not_on_update.rb b/test/dummy/app/models/not_on_update.rb index c288084d..9f341b07 100644 --- a/test/dummy/app/models/not_on_update.rb +++ b/test/dummy/app/models/not_on_update.rb @@ -1,4 +1,4 @@ # This model does not record versions when updated. class NotOnUpdate < ActiveRecord::Base - has_paper_trail on: [:create, :destroy] + has_paper_trail on: %i(create destroy) end diff --git a/test/dummy/app/models/song.rb b/test/dummy/app/models/song.rb index 1a118ef4..2f603a41 100644 --- a/test/dummy/app/models/song.rb +++ b/test/dummy/app/models/song.rb @@ -1,4 +1,21 @@ -# Example from 'Overwriting default accessors' in ActiveRecord::Base. +module OverrideSongAttributesTheRails4Way + def attributes + if name + super.merge(name: name) + else + super + end + end + + def changed_attributes + if name + super.merge(name: name) + else + super + end + end +end + class Song < ActiveRecord::Base has_paper_trail @@ -15,32 +32,6 @@ class Song < ActiveRecord::Base attribute :name, :string else attr_accessor :name - - # override attributes hashes like some libraries do - def attributes_with_name - if name - attributes_without_name.merge(name: name) - else - attributes_without_name - end - end - - # `alias_method_chain` is deprecated in rails 5, but we cannot use the - # suggested replacement, `Module#prepend`, because we still support ruby 1.9. - alias attributes_without_name attributes - alias attributes attributes_with_name - - def changed_attributes_with_name - if name - changed_attributes_without_name.merge(name: name) - else - changed_attributes_without_name - end - end - - # `alias_method_chain` is deprecated in rails 5, but we cannot use the - # suggested replacement, `Module#prepend`, because we still support ruby 1.9. - alias changed_attributes_without_name changed_attributes - alias changed_attributes changed_attributes_with_name + prepend OverrideSongAttributesTheRails4Way end end diff --git a/test/dummy/config/application.rb b/test/dummy/config/application.rb index f4511be2..afd6e3e8 100644 --- a/test/dummy/config/application.rb +++ b/test/dummy/config/application.rb @@ -4,54 +4,24 @@ require File.expand_path("../boot", __FILE__) require "active_record/railtie" require "action_controller/railtie" -Bundler.require(:default, Rails.env) if defined?(Bundler) +Bundler.require(:default, Rails.env) require "paper_trail" module Dummy class Application < Rails::Application - # Settings in config/environments/* take precedence over those specified here. - # Application configuration should go into files in config/initializers - # -- all .rb files in that directory are automatically loaded. - - # Custom directories with classes and modules you want to be autoloadable. - # config.autoload_paths += %W(#{config.root}/extras) - - # Only load the plugins named here, in the order given (default is alphabetical). - # :all can be used as a placeholder for all plugins not explicitly named. - # config.plugins = [ :exception_notification, :ssl_requirement, :all ] - - # Activate observers that should always be running. - # config.active_record.observers = :cacher, :garbage_collector, :forum_observer - - # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. - # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. - # config.time_zone = 'Central Time (US & Canada)' - - # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. - # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] - # config.i18n.default_locale = :de - - # Configure the default encoding used in templates for Ruby 1.9. config.encoding = "utf-8" - - # Configure sensitive parameters which will be filtered from the log file. config.filter_parameters += [:password] - - # Enable escaping HTML in JSON. config.active_support.escape_html_entities_in_json = true + config.active_support.test_order = :sorted - # Use SQL instead of Active Record's schema dumper when creating the database. - # This is necessary if your schema can't be completely dumped by the schema dumper, - # like if you have constraints or database-specific column types - # config.active_record.schema_format = :sql + # Disable assets in rails 4.2. In rails 5, config does not respond to + # assets, probably because it was moved out of railties to some other gem, + # and we only have dev. dependencies on railties, not all of rails. When + # we drop support for rails 4.2, we can remove this whole conditional. + if config.respond_to?(:assets) + config.assets.enabled = false + end - # `config.assets` is a `NoMethodError` in rails 5. - config.assets.enabled = false if config.respond_to?(:assets) - - # Version of your assets, change this if you want to expire all your assets - # config.assets.version = '1.0' - - # Rails 4 key for generating secret key config.secret_key_base = "A fox regularly kicked the screaming pile of biscuits." # `raise_in_transactional_callbacks` was added in rails 4, then deprecated @@ -65,8 +35,5 @@ module Dummy config.active_record.time_zone_aware_types = [:datetime] end end - - # Set test order for Test::Unit if possible - config.active_support.test_order = :sorted if config.active_support.respond_to?(:test_order=) end end diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index 29f9452e..cef3c857 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -1,4 +1,4 @@ Dummy::Application.routes.draw do resources :articles, only: [:create] - resources :widgets, only: [:create, :update, :destroy] + resources :widgets, only: %i(create update destroy) end diff --git a/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb b/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb index 026282da..c6389cfa 100644 --- a/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +++ b/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb @@ -73,7 +73,7 @@ class SetUpTestTables < ActiveRecord::Migration t.string :ip t.string :user_agent end - add_index :versions, [:item_type, :item_id] + add_index :versions, %i(item_type item_id) create_table :version_associations do |t| t.integer :version_id @@ -82,7 +82,7 @@ class SetUpTestTables < ActiveRecord::Migration end add_index :version_associations, [:version_id] add_index :version_associations, - [:foreign_key_name, :foreign_key_id], + %i(foreign_key_name foreign_key_id), name: "index_version_associations_on_foreign_key" create_table :post_versions, force: true do |t| @@ -97,7 +97,7 @@ class SetUpTestTables < ActiveRecord::Migration t.string :ip t.string :user_agent end - add_index :post_versions, [:item_type, :item_id] + add_index :post_versions, %i(item_type item_id) if ENV["DB"] == "postgres" && ::ActiveRecord::VERSION::MAJOR >= 4 create_table :json_versions, force: true do |t| @@ -109,7 +109,7 @@ class SetUpTestTables < ActiveRecord::Migration t.json :object_changes t.datetime :created_at end - add_index :json_versions, [:item_type, :item_id] + add_index :json_versions, %i(item_type item_id) end create_table :not_on_updates, force: true do |t| @@ -128,7 +128,7 @@ class SetUpTestTables < ActiveRecord::Migration t.text :object t.datetime :created_at end - add_index :banana_versions, [:item_type, :item_id] + add_index :banana_versions, %i(item_type item_id) create_table :wotsits, force: true do |t| t.integer :widget_id @@ -305,7 +305,7 @@ class SetUpTestTables < ActiveRecord::Migration t.text :object t.datetime :created_at end - add_index :custom_primary_key_record_versions, [:item_type, :item_id], name: "idx_cust_pk_item" + add_index :custom_primary_key_record_versions, %i(item_type item_id), name: "idx_cust_pk_item" end def down diff --git a/test/unit/associations_test.rb b/test/unit/associations_test.rb index 1395bdb0..ae472642 100644 --- a/test/unit/associations_test.rb +++ b/test/unit/associations_test.rb @@ -43,10 +43,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @widget_0 = @widget.versions.last.reify(has_one: true) } + setup { @widget0 = @widget.versions.last.reify(has_one: true) } should "see the associated as it was at the time" do - assert_nil @widget_0.wotsit + assert_nil @widget0.wotsit end should "not persist changes to the live association" do @@ -63,10 +63,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @widget_0 = @widget.versions.last.reify(has_one: true) } + setup { @widget0 = @widget.versions.last.reify(has_one: true) } should "see the associated as it was at the time" do - assert_equal "wotsit_0", @widget_0.wotsit.name + assert_equal "wotsit_0", @widget0.wotsit.name end should "not persist changes to the live association" do @@ -84,10 +84,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @widget_1 = @widget.versions.last.reify(has_one: true) } + setup { @widget1 = @widget.versions.last.reify(has_one: true) } should "see the associated as it was at the time" do - assert_equal "wotsit_2", @widget_1.wotsit.name + assert_equal "wotsit_2", @widget1.wotsit.name end should "not persist changes to the live association" do @@ -96,10 +96,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified opting out of has_one reification" do - setup { @widget_1 = @widget.versions.last.reify(has_one: false) } + setup { @widget1 = @widget.versions.last.reify(has_one: false) } should "see the associated as it is live" do - assert_equal "wotsit_3", @widget_1.wotsit.name + assert_equal "wotsit_3", @widget1.wotsit.name end end end @@ -110,10 +110,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reify" do - setup { @widget_1 = @widget.versions.last.reify(has_one: true) } + setup { @widget1 = @widget.versions.last.reify(has_one: true) } should "see the associated as it was at the time" do - assert_equal @wotsit, @widget_1.wotsit + assert_equal @wotsit, @widget1.wotsit end should "not persist changes to the live association" do @@ -128,10 +128,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @widget_2 = @widget.versions.last.reify(has_one: true) } + setup { @widget2 = @widget.versions.last.reify(has_one: true) } should "see the associated as it was at the time" do - assert_nil @widget_2.wotsit + assert_nil @widget2.wotsit end end end @@ -149,10 +149,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @customer_0 = @customer.versions.last.reify(has_many: true) } + setup { @customer0 = @customer.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal [], @customer_0.orders + assert_equal [], @customer0.orders end should "not persist changes to the live association" do @@ -162,11 +162,11 @@ class AssociationsTest < ActiveSupport::TestCase context "when reified with option mark_for_destruction" do should "mark the associated for destruction" do - @customer_0 = @customer.versions.last.reify( + @customer0 = @customer.versions.last.reify( has_many: true, mark_for_destruction: true ) - assert_equal [true], @customer_0.orders.map(&:marked_for_destruction?) + assert_equal [true], @customer0.orders.map(&:marked_for_destruction?) end end end @@ -179,10 +179,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @customer_0 = @customer.versions.last.reify(has_many: true) } + setup { @customer0 = @customer.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal ["order_date_0"], @customer_0.orders.map(&:order_date) + assert_equal ["order_date_0"], @customer0.orders.map(&:order_date) end end @@ -192,10 +192,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @customer_0 = @customer.versions.last.reify(has_many: true) } + setup { @customer0 = @customer.versions.last.reify(has_many: true) } should "see the live version of the nested association" do - assert_equal ["product_0"], @customer_0.orders.first.line_items.map(&:product) + assert_equal ["product_0"], @customer0.orders.first.line_items.map(&:product) end end end @@ -210,10 +210,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @customer_1 = @customer.versions.last.reify(has_many: true) } + setup { @customer1 = @customer.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal ["order_date_2"], @customer_1.orders.map(&:order_date) + assert_equal ["order_date_2"], @customer1.orders.map(&:order_date) end should "not persist changes to the live association" do @@ -222,10 +222,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified opting out of has_many reification" do - setup { @customer_1 = @customer.versions.last.reify(has_many: false) } + setup { @customer1 = @customer.versions.last.reify(has_many: false) } should "see the associated as it is live" do - assert_equal ["order_date_3"], @customer_1.orders.map(&:order_date) + assert_equal ["order_date_3"], @customer1.orders.map(&:order_date) end end @@ -235,10 +235,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @customer_1 = @customer.versions.last.reify(has_many: true) } + setup { @customer1 = @customer.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal ["order_date_2"], @customer_1.orders.map(&:order_date) + assert_equal ["order_date_2"], @customer1.orders.map(&:order_date) end should "not persist changes to the live association" do @@ -254,10 +254,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @customer_1 = @customer.versions.last.reify(has_many: true) } + setup { @customer1 = @customer.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal [@order.order_date], @customer_1.orders.map(&:order_date) + assert_equal [@order.order_date], @customer1.orders.map(&:order_date) end should "not persist changes to the live association" do @@ -274,10 +274,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @customer_1 = @customer.versions.last.reify(has_many: true) } + setup { @customer1 = @customer.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal [], @customer_1.orders + assert_equal [], @customer1.orders end end end @@ -288,10 +288,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @customer_0 = @customer.versions.last.reify(has_many: true) } + setup { @customer0 = @customer.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal ["order_date_0"], @customer_0.orders.map(&:order_date) + assert_equal ["order_date_0"], @customer0.orders.map(&:order_date) end should "not persist changes to the live association" do @@ -302,11 +302,11 @@ class AssociationsTest < ActiveSupport::TestCase context "when reified with option mark_for_destruction" do should "mark the newly associated for destruction" do - @customer_0 = @customer.versions.last.reify( + @customer0 = @customer.versions.last.reify( has_many: true, mark_for_destruction: true ) - assert @customer_0. + assert @customer0. orders. detect { |o| o.order_date == "order_date_1" }. marked_for_destruction? @@ -327,10 +327,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @book_0 = @book.versions.last.reify(has_many: true) } + setup { @book0 = @book.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal [], @book_0.authors + assert_equal [], @book0.authors end should "not persist changes to the live association" do @@ -340,18 +340,18 @@ class AssociationsTest < ActiveSupport::TestCase context "when reified with option mark_for_destruction" do setup do - @book_0 = @book.versions.last.reify( + @book0 = @book.versions.last.reify( has_many: true, mark_for_destruction: true ) end should "mark the associated for destruction" do - assert_equal [true], @book_0.authors.map(&:marked_for_destruction?) + assert_equal [true], @book0.authors.map(&:marked_for_destruction?) end should "mark the associated-through for destruction" do - assert_equal [true], @book_0.authorships.map(&:marked_for_destruction?) + assert_equal [true], @book0.authorships.map(&:marked_for_destruction?) end end end @@ -366,28 +366,28 @@ class AssociationsTest < ActiveSupport::TestCase context "when reified" do setup do - @book_0 = @book.versions.last.reify(has_many: true) + @book0 = @book.versions.last.reify(has_many: true) end should "see the associated as it was at the time" do - assert_equal [], @book_0.authors + assert_equal [], @book0.authors end end context "when reified with option mark_for_destruction" do setup do - @book_0 = @book.versions.last.reify( + @book0 = @book.versions.last.reify( has_many: true, mark_for_destruction: true ) end should "not mark the associated for destruction" do - assert_equal [false], @book_0.authors.map(&:marked_for_destruction?) + assert_equal [false], @book0.authors.map(&:marked_for_destruction?) end should "mark the associated-through for destruction" do - assert_equal [true], @book_0.authorships.map(&:marked_for_destruction?) + assert_equal [true], @book0.authorships.map(&:marked_for_destruction?) end end end @@ -401,10 +401,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @book_0 = @book.versions.last.reify(has_many: true) } + setup { @book0 = @book.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal ["author_0"], @book_0.authors.map(&:name) + assert_equal ["author_0"], @book0.authors.map(&:name) end end @@ -418,10 +418,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @book_1 = @book.versions.last.reify(has_many: true) } + setup { @book1 = @book.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal ["author_2"], @book_1.authors.map(&:name) + assert_equal ["author_2"], @book1.authors.map(&:name) end should "not persist changes to the live association" do @@ -430,10 +430,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified opting out of has_many reification" do - setup { @book_1 = @book.versions.last.reify(has_many: false) } + setup { @book1 = @book.versions.last.reify(has_many: false) } should "see the associated as it is live" do - assert_equal ["author_3"], @book_1.authors.map(&:name) + assert_equal ["author_3"], @book1.authors.map(&:name) end end end @@ -444,10 +444,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @book_1 = @book.versions.last.reify(has_many: true) } + setup { @book1 = @book.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal [@author.name], @book_1.authors.map(&:name) + assert_equal [@author.name], @book1.authors.map(&:name) end should "not persist changes to the live association" do @@ -464,10 +464,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @book_1 = @book.versions.last.reify(has_many: true) } + setup { @book1 = @book.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal [], @book_1.authors + assert_equal [], @book1.authors end end end @@ -480,10 +480,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @book_1 = @book.versions.last.reify(has_many: true) } + setup { @book1 = @book.versions.last.reify(has_many: true) } should "see the associated as it was at the time" do - assert_equal [], @book_1.authors + assert_equal [], @book1.authors end end end @@ -494,10 +494,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @book_0 = @book.versions.last.reify(has_many: true) } + setup { @book0 = @book.versions.last.reify(has_many: true) } should "only see the first associated" do - assert_equal ["author_0"], @book_0.authors.map(&:name) + assert_equal ["author_0"], @book0.authors.map(&:name) end should "not persist changes to the live association" do @@ -507,21 +507,21 @@ class AssociationsTest < ActiveSupport::TestCase context "when reified with option mark_for_destruction" do setup do - @book_0 = @book.versions.last.reify( + @book0 = @book.versions.last.reify( has_many: true, mark_for_destruction: true ) end should "mark the newly associated for destruction" do - assert @book_0. + assert @book0. authors. detect { |a| a.name == "author_1" }. marked_for_destruction? end should "mark the newly associated-through for destruction" do - assert @book_0. + assert @book0. authorships. detect { |as| as.author.name == "author_1" }. marked_for_destruction? @@ -535,10 +535,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @book_0 = @book.versions.last.reify(has_many: true) } + setup { @book0 = @book.versions.last.reify(has_many: true) } should "only see the first associated" do - assert_equal ["author_0"], @book_0.authors.map(&:name) + assert_equal ["author_0"], @book0.authors.map(&:name) end should "not persist changes to the live association" do @@ -548,21 +548,21 @@ class AssociationsTest < ActiveSupport::TestCase context "when reified with option mark_for_destruction" do setup do - @book_0 = @book.versions.last.reify( + @book0 = @book.versions.last.reify( has_many: true, mark_for_destruction: true ) end should "not mark the newly associated for destruction" do - assert !@book_0. + assert !@book0. authors. detect { |a| a.name == "person_existing" }. marked_for_destruction? end should "mark the newly associated-through for destruction" do - assert @book_0. + assert @book0. authorships. detect { |as| as.author.name == "person_existing" }. marked_for_destruction? @@ -578,10 +578,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @book_0 = @book.versions.last.reify(has_many: true) } + setup { @book0 = @book.versions.last.reify(has_many: true) } should "see the live association" do - assert_equal ["editor_0"], @book_0.editors.map(&:name) + assert_equal ["editor_0"], @book0.editors.map(&:name) end end end @@ -782,10 +782,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @wotsit_0 = @wotsit.versions.last.reify(belongs_to: true) } + setup { @wotsit0 = @wotsit.versions.last.reify(belongs_to: true) } should "see the associated as it was at the time" do - assert_nil @wotsit_0.widget + assert_nil @wotsit0.widget end should "not persist changes to the live association" do @@ -803,10 +803,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: true) } + setup { @wotsit1 = @wotsit.versions.last.reify(belongs_to: true) } should "see the associated as it was at the time" do - assert_equal "widget_2", @wotsit_1.widget.name + assert_equal "widget_2", @wotsit1.widget.name end should "not persist changes to the live association" do @@ -815,10 +815,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified opting out of belongs_to reification" do - setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: false) } + setup { @wotsit1 = @wotsit.versions.last.reify(belongs_to: false) } should "see the associated as it is live" do - assert_equal "widget_3", @wotsit_1.widget.name + assert_equal "widget_3", @wotsit1.widget.name end end end @@ -830,10 +830,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @wotsit_2 = @wotsit.versions.last.reify(belongs_to: true) } + setup { @wotsit2 = @wotsit.versions.last.reify(belongs_to: true) } should "see the associated as it was at the time" do - assert_equal @widget, @wotsit_2.widget + assert_equal @widget, @wotsit2.widget end should "not persist changes to the live association" do @@ -848,10 +848,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @wotsit_2 = @wotsit.versions.last.reify(belongs_to: true) } + setup { @wotsit2 = @wotsit.versions.last.reify(belongs_to: true) } should "see the associated as it was the time" do - assert_nil @wotsit_2.widget + assert_nil @wotsit2.widget end end end @@ -867,10 +867,10 @@ class AssociationsTest < ActiveSupport::TestCase end context "when reified" do - setup { @wotsit_0 = @wotsit.versions.last.reify(belongs_to: true) } + setup { @wotsit0 = @wotsit.versions.last.reify(belongs_to: true) } should "see the association as it was at the time" do - assert_equal "widget_0", @wotsit_0.widget.name + assert_equal "widget_0", @wotsit0.widget.name end should "not persist changes to the live association" do @@ -880,7 +880,7 @@ class AssociationsTest < ActiveSupport::TestCase context "when reified with option mark_for_destruction" do setup do - @wotsit_0 = @wotsit.versions.last. + @wotsit0 = @wotsit.versions.last. reify(belongs_to: true, mark_for_destruction: true) end