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

Commit

Permalink
Merge pull request #34 from alphagov/sti_fix
Browse files Browse the repository at this point in the history
Fix GoodsNomenclature -> Chapter,Heading,Commodity STI working.
  • Loading branch information
matthewford committed Nov 27, 2012
2 parents fc20488 + 565d119 commit 34c3931
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/models/goods_nomenclature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ class GoodsNomenclature < Sequel::Model
plugin :time_machine, period_start_column: :goods_nomenclatures__validity_start_date,
period_end_column: :goods_nomenclatures__validity_end_date

plugin :sti, class_determinator: ->(record) {
gono_id = record[:goods_nomenclature_item_id].to_s

if gono_id.ends_with?('00000000')
'Chapter'
elsif gono_id.ends_with?('000000') && gono_id.slice(2,2) != '00'
'Heading'
elsif !gono_id.ends_with?('000000')
'Commodity'
else
'GoodsNomenclature'
end
}

set_dataset order(:goods_nomenclatures__goods_nomenclature_item_id.asc)

set_primary_key :goods_nomenclature_sid
Expand Down
33 changes: 33 additions & 0 deletions lib/sequel/plugins/sti.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Based on: https://github.com/jeremyevans/sequel/blob/master/lib/sequel/plugins/single_table_inheritance.rb

module Sequel
module Plugins
module Sti
def self.configure(model, opts={})
model.instance_eval do
@class_determinator = opts[:class_determinator]
dataset.row_proc = lambda{|r| model.sti_load(r) }
end
end

module ClassMethods
attr_reader :class_determinator

def inherited(subclass)
super

cd = class_determinator
rp = dataset.row_proc
subclass.instance_eval do
@class_determinator = cd
dataset.row_proc = lambda{|r| model.sti_load(r) }
end
end

def sti_load(r)
constantize(class_determinator.call(r)).call(r)
end
end
end
end
end
13 changes: 12 additions & 1 deletion lib/sequel/plugins/tariff_validation_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def validates_validity_date_span_of(*atts)
if o.validity_end_date.present? && associated_record.validity_end_date.present?
o.errors.add(a, error_message) if associated_record.validity_end_date < o.validity_end_date
end
if o.validity_start_date.present? && associated_record.validity_end_date.present?
o.errors.add(a, error_message) if associated_record.validity_end_date < o.validity_start_date
end
if o.validity_end_date.present? && associated_record.validity_start_date.present?
o.errors.add(a, error_message) if associated_record.validity_start_date > o.validity_end_date
end
end
end
end
Expand Down Expand Up @@ -85,7 +91,12 @@ def validates_associated(associations, config = {})

validates_each(*atts) do |object, association, value|
if object.send(association).present?
object.errors.add(association, opts[:message] % [association]) unless object.send(opts[:ensure])
if opts[:ensure].present?
object.errors.add(association, opts[:message] % [association]) unless object.send(opts[:ensure])
else
associated_records = [object.send(association)].flatten
object.errors.add(association, opts[:message] % [association]) unless associated_records.all?(&:valid?)
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/factories/chief_record_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
trait :with_goods_nomenclature do
before(:create) { |mfcm|
FactoryGirl.create :goods_nomenclature, :fifteen_years, :declarable,
:with_indent,
goods_nomenclature_item_id: mfcm.cmdty_code
}
end
Expand Down
2 changes: 2 additions & 0 deletions spec/factories/goods_nomenclature_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
trait :with_indent do
after(:create) { |gono, evaluator|
FactoryGirl.create(:goods_nomenclature_indent, goods_nomenclature_sid: gono.goods_nomenclature_sid,
validity_start_date: gono.validity_start_date,
validity_end_date: gono.validity_end_date,
number_indents: evaluator.indents)
}
end
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/measure_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
validity_start_date { Date.today.ago(3.years) }
validity_end_date { nil }

trait :national do
national { true }
end

trait :with_goods_nomenclature do
after(:create) { |measure, evaluator|
FactoryGirl.create(:goods_nomenclature, goods_nomenclature_sid: measure.goods_nomenclature_sid,
Expand Down

0 comments on commit 34c3931

Please sign in to comment.