Skip to content

Commit

Permalink
update languages - added search functions & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BethFrank committed Apr 17, 2014
1 parent 1e8083c commit 5a6532d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
30 changes: 30 additions & 0 deletions app/models/language.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
# This is a 1:1 representation of ISO 639-2.
# It is built on initialization with a rake task, and not further touched.
# attribute alpha_3_bibliographic always has a distinct 3 character value
# alpha_2 will have either a 2 character value or and empty string ('')
# english_name or french_name may be more than one word long
# (e.g. 'English, Middle (1100-1500)', 'Filipino; Pilipino','Finno-Ugrian languages')
# and many of the languages have multiple version (e.g. there are 4 variations of German)
class Language < ActiveRecord::Base

include Housekeeping::Users

# Scopes
scope :eng_name_contains, ->(name) {where('english_name ILIKE ?', "%#{name}%")} # non-case sensitive comparison

#Validations
validates_presence_of :english_name, :alpha_3_bibliographic

#returns nil or single object
def self.exact_abr(abbr)
a = where('alpha_3_bibliographic = ?', abbr).to_a
if a.count == 0
return nil
else
return a[0]
end
end

#returns nil or single object
def self.exact_eng(name)
a = where('english_name = ?', name).to_a
if a.count == 0
return nil
else
return a[0]
end

end
end
2 changes: 1 addition & 1 deletion lib/tasks/initialization/languages.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace :tw do
namespace :initialization do

desc 'call like "rake tw:initialization:build_languages[/Users/matt/Downloads/ISO-639-2_utf-8.txt]"'
task :build_languages, [:data_directory] => [:environment] do |t, args|
task :build_languages, [:data_directory] => [:environment, :user_id, :project_id] do |t, args|
args.with_defaults(:data_directory => './ISO-639-2_utf-8.txt')

# TODO: check checksums of incoming files?
Expand Down
27 changes: 25 additions & 2 deletions spec/factories/language_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,31 @@
FactoryGirl.define do
factory :language, traits: [:creator_and_updater] do
factory :valid_language do
alpha_3_bibliographic "ABC"
english_name "English"
alpha_3_bibliographic 'ABC'
english_name 'English'
end

factory :english do
alpha_3_bibliographic 'eng'
alpha_2 'en'
english_name 'English'
french_name 'anglais'
end

factory :russian do
alpha_3_bibliographic 'rus'
alpha_2 'ru'
english_name 'Russian'
french_name 'russe'
end

factory :creole_eng do
alpha_3_bibliographic 'cpe'
# alpha_2 nil - no 2 letter abbreviation
english_name '"Creoles and pidgins, English based"'
french_name 'russe'
end

end

end
36 changes: 36 additions & 0 deletions spec/models/language_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,40 @@
expect(language.errors.include?(:alpha_3_bibliographic)).to be_true
end
end

context 'find values' do
before(:each) do
@eng = FactoryGirl.build(:english)
@eng.save
@rus = FactoryGirl.build(:russian)
@rus.save
@cre = FactoryGirl.build(:creole_eng)
@cre.save
end

specify 'eng_name scope should return a list of matching languages(like)' do
lang_a = Language.eng_name_contains('russ')
expect(lang_a.count).to eq(1)
expect(lang_a[0].id).to eq(@rus.id)
lang_a = Language.eng_name_contains('Englis')
expect(lang_a.count).to eq(2)
expect(lang_a[0].id).to eq(@eng.id)
expect(lang_a[1].id).to eq(@cre.id)
end

specify 'exact_abr should return a single object or nil' do
result = Language.exact_abr('cpe')
expect(result.id).to eq(@cre.id)
result = Language.exact_abr('not')
expect(result).to be_nil
end

specify 'exact_eng_name should return a single object or nil' do
result = Language.exact_eng('English')
expect(result.id).to eq(@eng.id)
result = Language.exact_eng('English,')
expect(result).to be_nil
end
end

end

0 comments on commit 5a6532d

Please sign in to comment.