diff --git a/app/models/language.rb b/app/models/language.rb index c014d77dbf..f5250c541b 100644 --- a/app/models/language.rb +++ b/app/models/language.rb @@ -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 diff --git a/lib/tasks/initialization/languages.rake b/lib/tasks/initialization/languages.rake index aab8107c4f..199f85a96b 100644 --- a/lib/tasks/initialization/languages.rake +++ b/lib/tasks/initialization/languages.rake @@ -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? diff --git a/spec/factories/language_factory.rb b/spec/factories/language_factory.rb index 24f7d3588b..2b2024d0ee 100644 --- a/spec/factories/language_factory.rb +++ b/spec/factories/language_factory.rb @@ -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 diff --git a/spec/models/language_spec.rb b/spec/models/language_spec.rb index f4860efba6..0d867c54ea 100644 --- a/spec/models/language_spec.rb +++ b/spec/models/language_spec.rb @@ -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