diff --git a/app/models/check.rb b/app/models/check.rb index eff3996..ec88238 100644 --- a/app/models/check.rb +++ b/app/models/check.rb @@ -5,7 +5,7 @@ class Check < ApplicationRecord def reference!(content) target = self.target reference = target.extract content - self.update! reference: reference, content: nil, checked_at: Time.now, changed_at: nil, last_error: nil + self.update! reference: reference, content: reference, checked_at: Time.now, changed_at: nil, last_error: nil end def diff!(content, debug: false) @@ -16,7 +16,7 @@ class Check < ApplicationRecord begin target = self.target - reference = Utils.utf8! self.reference + reference = Utils.utf8! self.content content = target.extract content changed = reference != content if changed diff --git a/app/models/site.rb b/app/models/site.rb index 73d9462..aba7a7e 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -46,7 +46,7 @@ class Site < ApplicationRecord end def reference!(content) - self.update! reference: content, content: nil, checked_at: Time.now, changed_at: nil, last_error: nil + self.update! reference: content, content: content, checked_at: Time.now, changed_at: nil, last_error: nil self.checks.each { |c| c.reference! content } end @@ -63,7 +63,7 @@ class Site < ApplicationRecord state = :unchanged begin - reference = Utils.utf8! self.reference + reference = Utils.utf8! self.content checks = self.checks if checks.empty? if reference != content diff --git a/db/migrate/20180510000002_create_sites.rb b/db/migrate/20180510000002_create_sites.rb index 3d2bc4c..b350aab 100644 --- a/db/migrate/20180510000002_create_sites.rb +++ b/db/migrate/20180510000002_create_sites.rb @@ -4,8 +4,8 @@ class CreateSites < ActiveRecord::Migration[5.1] t.string :url, null: false t.string :name, index: true - t.binary :reference - t.binary :content + t.binary :reference, null: false + t.binary :content, null: false t.belongs_to :group, index: true, foreign_key: true t.belongs_to :template, index: true, foreign_key: true diff --git a/db/migrate/20180510000004_create_checks.rb b/db/migrate/20180510000004_create_checks.rb index 93ff1e5..b9e54fc 100644 --- a/db/migrate/20180510000004_create_checks.rb +++ b/db/migrate/20180510000004_create_checks.rb @@ -1,8 +1,8 @@ class CreateChecks < ActiveRecord::Migration[5.1] def change create_table :checks do |t| - t.binary :reference - t.binary :content + t.binary :reference, null: false + t.binary :content, null: false t.belongs_to :target, index: true, foreign_key: true t.belongs_to :site, index: true, foreign_key: true diff --git a/spec/models/site_spec.rb b/spec/models/site_spec.rb index 8d35ff2..cb6c283 100644 --- a/spec/models/site_spec.rb +++ b/spec/models/site_spec.rb @@ -1,8 +1,10 @@ RSpec.describe Site, type: :model do - REFERENCE = 'foo
bar
' + REFERENCE_TARGET = '
bar
' + REFERENCE = "foo #{REFERENCE_TARGET}" CHANGE_OUTSIDE_TARGET = 'baz
bar
' - CHANGE_INSIDE_TARGET = 'foo
baz
' + CHANGE_TARGET = '
baz
' + CHANGE_INSIDE_TARGET = "foo #{CHANGE_TARGET}" let :site do Site.create! url: 'http://localhost/' @@ -32,7 +34,7 @@ RSpec.describe Site, type: :model do expect(status).to be :unchanged expect(site.changed_at).to be_nil - expect(site.content).to be_nil + expect(site.content).to eq REFERENCE end it 'must not change if no change with checks' do @@ -42,10 +44,10 @@ RSpec.describe Site, type: :model do expect(status).to be :unchanged expect(site.changed_at).to be_nil - expect(site.content).to be_nil + expect(site.content).to eq REFERENCE expect(check.changed_at).to be_nil - expect(check.content).to be_nil + expect(check.content).to eq REFERENCE_TARGET end it 'must change if change with no check' do @@ -53,7 +55,7 @@ RSpec.describe Site, type: :model do expect(status).to be :changed expect(site.changed_at).not_to be_nil - expect(site.content).not_to be_nil + expect(site.content).not_to eq REFERENCE end it 'must not change if change but no check changed' do @@ -62,10 +64,10 @@ RSpec.describe Site, type: :model do expect(status).to be :unchanged expect(site.changed_at).to be_nil - expect(site.content).to be_nil + expect(site.content).to be REFERENCE expect(check.changed_at).to be_nil - expect(check.content).to be_nil + expect(check.content).to eq REFERENCE_TARGET end it 'must change if check changed' do @@ -74,9 +76,9 @@ RSpec.describe Site, type: :model do expect(status).to be :changed expect(site.changed_at).not_to be_nil - expect(site.content).not_to be_nil + expect(site.content).to eq CHANGE_INSIDE_TARGET expect(check.changed_at).not_to be_nil - expect(check.content).not_to be_nil + expect(check.content).to eq CHANGE_TARGET end end