From 2a2768f61b5aa8021d73e33035c7b9a7a19ebefb Mon Sep 17 00:00:00 2001 From: Tiago Amaro Date: Sat, 18 Feb 2017 09:51:48 -0800 Subject: [PATCH] simplify taxonomy repository find method (#2589) --- app/repositories/taxonomy.rb | 8 +------- spec/repositories/taxonomy_spec.rb | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/repositories/taxonomy.rb b/app/repositories/taxonomy.rb index 7cbe01e6cf..55a4b2c282 100644 --- a/app/repositories/taxonomy.rb +++ b/app/repositories/taxonomy.rb @@ -13,13 +13,7 @@ class Repositories::Taxonomy class << self def get_taxon_by_name(taxon_name) result = Array.wrap(taxon_name).compact.map do |tn| - tn = tn.downcase - taxon = taxons.find{|t| t.name.downcase == tn } - if taxon.nil? && tn.match(/-/) - tn = tn.gsub('-', ' ') - taxon = taxons.find{|t| t.name.downcase == tn } - end - taxon + taxons.find { |t| t.name.parameterize == tn.parameterize } end result.size < 2 ? result.first : result end diff --git a/spec/repositories/taxonomy_spec.rb b/spec/repositories/taxonomy_spec.rb index 4afaa31140..68fe4626ae 100644 --- a/spec/repositories/taxonomy_spec.rb +++ b/spec/repositories/taxonomy_spec.rb @@ -1,12 +1,12 @@ require 'spec_helper' describe Repositories::Taxonomy do - let(:taxonomy) { create(:taxonomy, name: "My Taxonomy") } + let(:taxonomy) { create(:taxonomy, name: 'My Taxonomy') } before(:each) { Repositories::Taxonomy.expire_cache!(true) } - describe ".get_taxon_by_name" do - describe 'find by name' do + describe '.get_taxon_by_name' do + describe 'find taxons by name' do it 'case-insensitive' do taxon = create(:taxon, taxonomy_id: taxonomy.id, name: 'Great') @@ -14,10 +14,17 @@ expect(result.id).to eq(taxon.id) end - it 'find by non-hyphen name' do - taxon = create(:taxon, taxonomy_id: taxonomy.id, name: 'World of something') + it 'has spaces' do + taxon = create(:taxon, taxonomy_id: taxonomy.id, name: 'Long Sleeve') - result = described_class.get_taxon_by_name('World-of-something') + result = described_class.get_taxon_by_name('Long Sleeve') + expect(result.id).to eq(taxon.id) + end + + it 'has spaces and hyphens' do + taxon = create(:taxon, taxonomy_id: taxonomy.id, name: 'The-Evening-Gown') + + result = described_class.get_taxon_by_name('The Evening-Gown') expect(result.id).to eq(taxon.id) end end