Permalink
Browse files

Move from regex to array based solution for checking hidden codes.

Prevent exact search from matching hidden goods codes.
  • Loading branch information...
1 parent 33c566f commit 37df73816aeace2e4ab52b99a59db0e8d113893d @saulius saulius committed Dec 4, 2012
@@ -36,7 +36,7 @@ def find_commodity
.by_code(params[:id])
.take
- raise Sequel::RecordNotFound if @commodity.goods_nomenclature_item_id =~ HiddenGoodsNomenclature.to_pattern
+ raise Sequel::RecordNotFound if @commodity.goods_nomenclature_item_id.in? HiddenGoodsNomenclature.codes
end
end
end
@@ -33,8 +33,7 @@ def show
else
@commodities = GoodsNomenclatureMapper.new(@heading.commodities_dataset.eager(:goods_nomenclature_indent,
:goods_nomenclature_description)
- .all
- .delete_if{|c| c.goods_nomenclature_item_id =~ HiddenGoodsNomenclature.to_pattern}).all
+ .all).all
end
View
@@ -15,6 +15,7 @@ class Chapter < GoodsNomenclature
one_to_many :headings, dataset: -> {
actual(Heading).filter("goods_nomenclature_item_id LIKE ? AND goods_nomenclature_item_id NOT LIKE '__00______'", relevant_headings)
+ .where(~{goods_nomenclatures__goods_nomenclature_item_id: HiddenGoodsNomenclature.codes })
}
one_to_one :chapter_note, dataset: -> {
View
@@ -14,6 +14,7 @@ class Heading < GoodsNomenclature
one_to_many :commodities, dataset: -> {
actual(Commodity).filter("goods_nomenclatures.goods_nomenclature_item_id LIKE ?", heading_id)
+ .where(~{goods_nomenclatures__goods_nomenclature_item_id: HiddenGoodsNomenclature.codes })
}
one_to_one :chapter, dataset: -> {
@@ -1,19 +1,13 @@
class HiddenGoodsNomenclature < Sequel::Model
plugin :timestamps
- set_dataset order(:goods_code_identifier.asc)
+ set_dataset order(:goods_nomenclature_item_id.asc)
validates do
- presence_of :goods_code_identifier
+ presence_of :goods_nomenclature_item_id
end
- def self.to_pattern
- all.tap! { |patterns|
- if patterns.any?
- /^(#{patterns.map(&:goods_code_identifier).join("|")})/
- else
- /(?!.*)/ # does not match anything
- end
- }
+ def self.codes
+ all.map(&:goods_nomenclature_item_id)
end
end
View
@@ -7,6 +7,7 @@ class Section < Sequel::Model
Chapter.join_table(:inner, :chapters_sections, chapters_sections__goods_nomenclature_sid: :goods_nomenclatures__goods_nomenclature_sid)
.join_table(:inner, :sections, chapters_sections__section_id: :sections__id)
.with_actual(Chapter)
+ .where(~{goods_nomenclatures__goods_nomenclature_item_id: HiddenGoodsNomenclature.codes })
.where(sections__id: id)
}, eager_loader: (proc do |eo|
eo[:rows].each{|section| section.associations[:chapters] = []}
@@ -17,6 +18,7 @@ class Section < Sequel::Model
.join_table(:inner, :sections, chapters_sections__section_id: :sections__id)
.eager(:goods_nomenclature_description)
.with_actual(Chapter)
+ .where(~{goods_nomenclatures__goods_nomenclature_item_id: HiddenGoodsNomenclature.codes })
.where(sections__id: id_map.keys).all do |chapter|
if sections = id_map[chapter[:section_id]]
sections.each do |section|
@@ -25,22 +25,27 @@ def present?
class ExactSearch < BaseSearch
def search!
@results = case query_string
- when /^[0-9]{1,3}$/
- Chapter.actual.by_code(query_string).first
- when /^[0-9]{4,9}$/
- Heading.actual.by_code(query_string).first
- when /^[0-9]{10}$/
- Commodity.actual.by_code(query_string).declarable.first.presence ||
- Heading.actual.by_declarable_code(query_string).declarable.first.presence
- when /^\d{2}\s*\d{2}\s*\d{2}\s*\d{2}\s*\d{2}$/
- Commodity.actual.by_code(query_string.gsub(/\s+/, '')).declarable.first.presence ||
- Heading.actual.by_declarable_code(query_string.gsub(/\s+/, '')).declarable.first.presence
- when /^[0-9]{11,12}$/
- Commodity.actual.by_code(query_string).declarable.first
- end
+ when /^[0-9]{1,3}$/
+ Chapter.actual.by_code(query_string).first
+ when /^[0-9]{4,9}$/
+ Heading.actual.by_code(query_string).first
+ when /^[0-9]{10}$/
+ Commodity.actual.by_code(query_string).declarable.first.presence ||
+ Heading.actual.by_declarable_code(query_string).declarable.first.presence
+ when /^\d{2}\s*\d{2}\s*\d{2}\s*\d{2}\s*\d{2}$/
+ Commodity.actual.by_code(query_string.gsub(/\s+/, '')).declarable.first.presence ||
+ Heading.actual.by_declarable_code(query_string.gsub(/\s+/, '')).declarable.first.presence
+ when /^[0-9]{11,12}$/
+ Commodity.actual.by_code(query_string).declarable.first
+ end
+
self
end
+ def present?
+ !query_string.in?(HiddenGoodsNomenclature.codes) && @results.present?
+ end
+
def serializable_hash
{
type: "exact_match",
@@ -1,7 +1,7 @@
Sequel.migration do
change do
create_table :hidden_goods_nomenclatures do
- String :goods_code_identifier
+ String :goods_nomenclature_item_id
DateTime :updated_at
DateTime :created_at
end
View
@@ -808,6 +808,12 @@
index [:goods_nomenclature_sid], :name=>:primary_key, :unique=>true
end
+ create_table(:hidden_goods_nomenclatures) do
+ column :goods_nomenclature_item_id, "varchar(255)"
+ column :updated_at, "datetime"
+ column :created_at, "datetime"
+ end
+
create_table(:language_descriptions) do
column :language_code_id, "varchar(255)"
column :language_id, "varchar(5)"
@@ -1547,6 +1553,7 @@
self[:schema_migrations].insert(:filename => "20121109121107_fix_chief_last_effective_dates.rb")
self[:schema_migrations].insert(:filename => "20121109121219_remove_invalid_measures.rb")
self[:schema_migrations].insert(:filename => "20121129094209_add_invalidated_columns_to_measures.rb")
+ self[:schema_migrations].insert(:filename => "20121204130816_create_hidden_goods_nomenclatures.rb")
create_table(:search_references) do
primary_key :id, :type=>"int(11)"
@@ -34,7 +34,7 @@
end
context 'when record is hidden' do
- let!(:hidden_goods_nomenclature) { create :hidden_goods_nomenclature, goods_code_identifier: commodity.goods_nomenclature_item_id }
+ let!(:hidden_goods_nomenclature) { create :hidden_goods_nomenclature, goods_nomenclature_item_id: commodity.goods_nomenclature_item_id }
it 'returns not found' do
expect { get :show, id: commodity.goods_nomenclature_item_id, format: :json }.to raise_error Sequel::RecordNotFound
@@ -29,7 +29,7 @@
let!(:commodity1) { create :commodity, :with_indent, :with_description, :with_chapter, :declarable, goods_nomenclature_item_id: "#{heading.short_code}010000"}
let!(:commodity2) { create :commodity, :with_indent, :with_description, :with_chapter, :declarable, goods_nomenclature_item_id: "#{heading.short_code}020000"}
- let!(:hidden_goods_nomenclature) { create :hidden_goods_nomenclature, goods_code_identifier: commodity2.goods_nomenclature_item_id }
+ let!(:hidden_goods_nomenclature) { create :hidden_goods_nomenclature, goods_nomenclature_item_id: commodity2.goods_nomenclature_item_id }
it 'does not include hidden commodities in the response' do
get :show, id: heading, format: :json
@@ -1,13 +1,13 @@
FactoryGirl.define do
factory :hidden_goods_nomenclature do
- goods_code_identifier { 10.times.map{ Random.rand(9) }.join }
+ goods_nomenclature_item_id { 10.times.map{ Random.rand(9) }.join }
trait :chapter do
- goods_code_identifier { "#{2.times.map{ Random.rand(9) }.join}00000000" }
+ goods_nomenclature_item_id { "#{2.times.map{ Random.rand(9) }.join}00000000" }
end
trait :heading do
- goods_code_identifier { "#{4.times.map{ Random.rand(8)+1 }.join}000000" }
+ goods_nomenclature_item_id { "#{4.times.map{ Random.rand(8)+1 }.join}000000" }
end
end
end
@@ -1,14 +1,15 @@
require 'spec_helper'
describe HiddenGoodsNomenclature do
- it { should validate_presence.of(:goods_code_identifier) }
+ it { should validate_presence.of(:goods_nomenclature_item_id) }
- describe '.to_pattern' do
- let!(:hidden_gono1) { create :hidden_goods_nomenclature, goods_code_identifier: "99" }
- let!(:hidden_gono2) { create :hidden_goods_nomenclature, goods_code_identifier: "0101" }
+ describe '.codes' do
+ let!(:hidden_gono1) { create :hidden_goods_nomenclature, goods_nomenclature_item_id: "9900000000" }
+ let!(:hidden_gono2) { create :hidden_goods_nomenclature, goods_nomenclature_item_id: "0101000000" }
- it 'returns regexp pattern of all goods code identifiers' do
- HiddenGoodsNomenclature.to_pattern.should eq /^(0101|99)/
+ it 'returns array of hidden codes' do
+ HiddenGoodsNomenclature.codes.should eq [hidden_gono2.goods_nomenclature_item_id,
+ hidden_gono1.goods_nomenclature_item_id]
end
end
end
@@ -1,6 +1,13 @@
require 'spec_helper'
describe Section do
+ describe 'associations' do
+ describe 'chapters' do
+ it 'does not include HiddenGoodsNomenclatures' do
+ end
+ end
+ end
+
describe '.to_param' do
let(:section) { create :section }
@@ -81,7 +81,7 @@
context 'commodities' do
let(:commodity) { create :commodity, :declarable }
- let(:heading) { create :heading, :declarable }
+ let(:heading) { create :heading, :declarable }
let(:commodity_pattern) {
{
type: 'exact_match',
@@ -147,6 +147,30 @@
result.should match_json_expression heading_pattern
end
end
+
+ context 'hidden commodities' do
+ let!(:commodity) { create :commodity, :declarable }
+ let!(:hidden_gono) { create :hidden_goods_nomenclature, goods_nomenclature_item_id: commodity.goods_nomenclature_item_id }
+
+ let(:commodity_pattern) {
+ {
+ type: 'exact_match',
+ entry: {
+ endpoint: 'commodities',
+ id: commodity.goods_nomenclature_item_id.first(10)
+ }
+ }
+ }
+
+ before {
+ @result = SearchService.new(t: commodity.goods_nomenclature_item_id.first(10),
+ as_of: Date.today).to_json
+ }
+
+ it 'does not return hidden commodity as exact match' do
+ @result.should_not match_json_expression commodity_pattern
+ end
+ end
end
# Searching in ElasticSearch index

0 comments on commit 37df738

Please sign in to comment.