PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/spanish_word_for.rb

https://bitbucket.org/kapilnakhwa/demo-teachme
Ruby | 37 lines | 31 code | 3 blank | 3 comment | 2 complexity | 1be8b838d2855d3c50afbed5cd554164 MD5 | raw file
  1. class SpanishWordFor < ActiveRecord::Base
  2. # fields: english_word, spanish_word
  3. set_table_name 'spanish_word_for'
  4. set_primary_key 'id'
  5. def self.random_words(num, seed_id=500)
  6. ## filter using id, so that all records do not need to be searched and sorted by MySQL
  7. start_idx = seed_id - 500
  8. start_idx=0 if start_idx < 0
  9. end_idx = seed_id + 500
  10. end_idx = 800 if end_idx < 800
  11. conditions = "id > #{start_idx} AND id < #{end_idx}"
  12. SpanishWordFor.where(conditions).order('rand()').limit(num).all
  13. end
  14. ## iterates over all records and saves lookup_keys
  15. def self.generate_lookup_keys
  16. SpanishWordFor.where("lookup_key = ''").all.each do |word|
  17. lookup = word.english_word.gsub(/\(.*?\)/, '') # remove anything in parentheses
  18. lookup.gsub!(/'|!|\?/, '')
  19. lookup.gsub!(/ +/, ' ')
  20. word.lookup_key = SpanishDictionary.map_word(lookup)
  21. # split words at comma
  22. lookup_arr = lookup.split(",").collect do |lk|
  23. lk.strip!
  24. lk.gsub!(/ /, '-')
  25. SpanishDictionary.map_word(lk)
  26. end
  27. word.lookup_key = lookup_arr.join(",")
  28. word.save
  29. print word.lookup_key + "\n"
  30. end
  31. end
  32. end