From f6e70ab15e97e44ab20eca54c31214e993fd591a Mon Sep 17 00:00:00 2001 From: Saulius Grigaliunas Date: Thu, 12 Sep 2013 14:46:26 +0300 Subject: [PATCH] Specify what gets included into Goods code changes --- app/models/change.rb | 4 ++ app/models/chapter.rb | 9 ++++- app/models/commodity.rb | 5 +++ app/models/heading.rb | 7 +++- lib/sequel/plugins/oplog.rb | 4 +- spec/models/chapter_spec.rb | 73 +++++++++++++++++++++++++++++++++++ spec/models/commodity_spec.rb | 40 +++++++++++++++++++ spec/models/heading_spec.rb | 63 ++++++++++++++++++++++++++++++ 8 files changed, 200 insertions(+), 5 deletions(-) diff --git a/app/models/change.rb b/app/models/change.rb index b31bf2c2..e3ab809b 100644 --- a/app/models/change.rb +++ b/app/models/change.rb @@ -7,6 +7,10 @@ def initialize(attributes = {}) end end + def model=(model) + @model = model.constantize + end + def operation_record @operation_record ||= operation_class.find(oid: oid) end diff --git a/app/models/chapter.rb b/app/models/chapter.rb index 9176d006..0c871f1f 100644 --- a/app/models/chapter.rb +++ b/app/models/chapter.rb @@ -85,11 +85,16 @@ def changes(depth = 1) :operation, Sequel.as(depth, :depth) ).where(pk_hash) - .union(Heading.changes_for(depth + 1, ["goods_nomenclature_item_id LIKE ?", relevant_headings])) - .union(Commodity.changes_for(depth + 1, ["goods_nomenclature_item_id LIKE ?", relevant_commodities])) + .union(Heading.changes_for(depth + 1, ["goods_nomenclature_item_id LIKE ? AND goods_nomenclature_item_id NOT LIKE ?", relevant_headings, '__00______'])) + .union(Commodity.changes_for(depth + 1, ["goods_nomenclature_item_id LIKE ? AND goods_nomenclature_item_id NOT LIKE ?", relevant_commodities, '____000000'])) .union(Measure.changes_for(depth +1, ["goods_nomenclature_item_id LIKE ?", relevant_commodities])) .from_self .where(Sequel.~(operation_date: nil)) + .tap! { |criteria| + # if Chapter did not come from initial seed, filter by its + # create/update date + criteria.where{ |o| o.>=(:operation_date, operation_date) } unless operation_date.blank? + } .limit(depth * 10) .order(Sequel.function(:isnull, :operation_date), Sequel.desc(:operation_date), Sequel.desc(:depth)) .all diff --git a/app/models/commodity.rb b/app/models/commodity.rb index d50eaa96..08381849 100644 --- a/app/models/commodity.rb +++ b/app/models/commodity.rb @@ -190,6 +190,11 @@ def changes(depth = 1) .union(Measure.changes_for(depth + 1, measures_oplog__measure_sid: measures.map(&:measure_sid))) .from_self .where(Sequel.~(operation_date: nil)) + .tap! { |criteria| + # if Commodity did not come from initial seed, filter by its + # create/update date + criteria.where{ |o| o.>=(:operation_date, operation_date) } unless operation_date.blank? + } .limit(depth * 10) .order(Sequel.function(:isnull, :operation_date), Sequel.desc(:operation_date), Sequel.desc(:depth)) .all diff --git a/app/models/heading.rb b/app/models/heading.rb index 3f9de72d..a3910e25 100644 --- a/app/models/heading.rb +++ b/app/models/heading.rb @@ -126,10 +126,15 @@ def changes(depth = 1) :operation, Sequel.as(depth, :depth) ).where(pk_hash) - .union(Commodity.changes_for(depth + 1, ["goods_nomenclature_item_id LIKE ?", relevant_commodities])) + .union(Commodity.changes_for(depth + 1, ["goods_nomenclature_item_id LIKE ? AND goods_nomenclature_item_id NOT LIKE ?", relevant_commodities, '____000000'])) .union(Measure.changes_for(depth +1, ["goods_nomenclature_item_id LIKE ?", relevant_commodities])) .from_self .where(Sequel.~(operation_date: nil)) + .tap! { |criteria| + # if Heading did not come from initial seed, filter by its + # create/update date + criteria.where{ |o| o.>=(:operation_date, operation_date) } unless operation_date.blank? + } .limit(depth * 10) .order(Sequel.function(:isnull, :operation_date), Sequel.desc(:operation_date), Sequel.desc(:depth)) .all diff --git a/lib/sequel/plugins/oplog.rb b/lib/sequel/plugins/oplog.rb index d8ef468b..01b45ff0 100644 --- a/lib/sequel/plugins/oplog.rb +++ b/lib/sequel/plugins/oplog.rb @@ -10,8 +10,8 @@ def self.configure(model, options = {}) operation_class = Class.new(Sequel::Model(:"#{model.table_name}_oplog")) operation_class.one_to_one( :record, - key: primary_key, - primary_key: primary_key, + key: model.primary_key, + primary_key: model.primary_key, class_name: model ) operation_class.set_primary_key(primary_key) diff --git a/spec/models/chapter_spec.rb b/spec/models/chapter_spec.rb index 2448447c..ff96ad81 100644 --- a/spec/models/chapter_spec.rb +++ b/spec/models/chapter_spec.rb @@ -72,4 +72,77 @@ chapter.to_param.should == chapter.goods_nomenclature_item_id.first(2) end end + + describe '#changes' do + let(:chapter) { create :chapter } + + it 'returns instance of ChangeLog' do + expect(chapter.changes).to be_kind_of ChangeLog + end + + context 'with Chapter changes' do + let!(:chapter) { create :chapter, operation_date: Date.today } + + it 'includes Chapter changes' do + expect( + chapter.changes.select { |change| + change.oid == chapter.oid && + change.model == Chapter + } + ).to be_present + end + + context 'with Heading changes' do + let!(:heading) { + create :heading, + operation_date: Date.today, + goods_nomenclature_item_id: "#{chapter.short_code}01000000" + } + + it 'includes Heading changes' do + expect( + chapter.changes.select { |change| + change.oid == heading.oid && + change.model == Heading + } + ).to be_present + end + + context 'with associated Commodity changes' do + let!(:commodity) { + create :commodity, + operation_date: Date.today, + goods_nomenclature_item_id: "#{heading.short_code}000001" + } + + it 'includes Commodity changes' do + expect( + chapter.changes.select { |change| + change.oid == commodity.oid && + change.model == Commodity + } + ).to be_present + end + + context 'with associated Measure (through Commodity) changes' do + let!(:measure) { + create :measure, + goods_nomenclature: commodity, + goods_nomenclature_item_id: commodity.goods_nomenclature_item_id, + operation_date: Date.today + } + + it 'includes Measure changes' do + expect( + heading.changes.select { |change| + change.oid == measure.oid && + change.model == Measure + } + ).to be_present + end + end + end + end + end + end end diff --git a/spec/models/commodity_spec.rb b/spec/models/commodity_spec.rb index b6fe5f43..6a11459b 100644 --- a/spec/models/commodity_spec.rb +++ b/spec/models/commodity_spec.rb @@ -318,4 +318,44 @@ end end end + + describe '#changes' do + let(:commodity) { create :commodity } + + it 'returns instance of ChangeLog' do + expect(commodity.changes).to be_kind_of ChangeLog + end + + context 'with commodity changes' do + let!(:commodity) { create :commodity, operation_date: Date.today } + + it 'includes commodity changes' do + expect( + commodity.changes.select { |change| + change.oid == commodity.oid && + change.model == GoodsNomenclature + } + ).to be_present + end + end + + context 'with associated measure changes' do + let!(:commodity) { create :commodity, operation_date: Date.yesterday } + let!(:measure) { + create :measure, + goods_nomenclature: commodity, + goods_nomenclature_item_id: commodity.goods_nomenclature_item_id, + operation_date: Date.today + } + + it 'includes measure changes' do + expect( + commodity.changes.select { |change| + change.oid == measure.oid && + change.model == Measure + } + ).to be_present + end + end + end end diff --git a/spec/models/heading_spec.rb b/spec/models/heading_spec.rb index af00cc61..b392da29 100644 --- a/spec/models/heading_spec.rb +++ b/spec/models/heading_spec.rb @@ -147,4 +147,67 @@ end end end + + describe '#changes' do + let(:heading) { create :heading } + + it 'returns instance of ChangeLog' do + expect(heading.changes).to be_kind_of ChangeLog + end + + context 'with Heading changes' do + let!(:heading) { create :heading, operation_date: Date.today } + + it 'includes Heading changes' do + expect( + heading.changes.select { |change| + change.oid == heading.oid && + change.model == Heading + } + ).to be_present + end + end + + context 'with associated Commodity changes' do + let!(:heading) { create :heading, operation_date: Date.yesterday } + let!(:commodity) { + create :commodity, + operation_date: Date.yesterday, + goods_nomenclature_item_id: "#{heading.short_code}000001" + } + + it 'includes Commodity changes' do + expect( + heading.changes.select { |change| + change.oid == commodity.oid && + change.model == Commodity + } + ).to be_present + end + + context 'with associated Measure (through Commodity) changes' do + let!(:heading) { create :heading, operation_date: Date.yesterday } + let!(:commodity) { + create :commodity, + operation_date: Date.yesterday, + goods_nomenclature_item_id: "#{heading.short_code}000001" + } + let!(:measure) { + create :measure, + goods_nomenclature: commodity, + goods_nomenclature_item_id: commodity.goods_nomenclature_item_id, + operation_date: Date.yesterday + } + + it 'includes Measure changes' do + expect( + heading.changes.select { |change| + change.oid == measure.oid && + change.model == Measure + } + ).to be_present + end + end + end + end end