Skip to content
This repository has been archived by the owner on Nov 22, 2017. It is now read-only.

Commit

Permalink
Specify what gets included into Goods code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
saulius committed Sep 12, 2013
1 parent 45a7703 commit f6e70ab
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/models/change.rb
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions app/models/chapter.rb
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions app/models/commodity.rb
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion app/models/heading.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/sequel/plugins/oplog.rb
Expand Up @@ -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)
Expand Down
73 changes: 73 additions & 0 deletions spec/models/chapter_spec.rb
Expand Up @@ -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
40 changes: 40 additions & 0 deletions spec/models/commodity_spec.rb
Expand Up @@ -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
63 changes: 63 additions & 0 deletions spec/models/heading_spec.rb
Expand Up @@ -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

0 comments on commit f6e70ab

Please sign in to comment.