Skip to content

Commit

Permalink
simplify taxonomy repository find method (#2589)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagoamaro authored and Gustavo Robles committed Mar 7, 2017
1 parent 03919b4 commit 2a2768f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
8 changes: 1 addition & 7 deletions app/repositories/taxonomy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions spec/repositories/taxonomy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
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')

result = described_class.get_taxon_by_name('gReAt')
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
Expand Down

0 comments on commit 2a2768f

Please sign in to comment.