Permalink
Browse files

list of words page ready

Signed-off-by: Balint Erdi <balint.erdi@gmail.com>
  • Loading branch information...
1 parent 7d2f754 commit 98698b3f5f0bbcadb13a2f0351a40ff4d0c3ea2d @balinterdi committed Nov 21, 2008
@@ -28,7 +28,8 @@ def create
end
def index
- @words = Word.find(:all, :conditions => [ "user_id = ?", current_user_id])
+ user = User.find(current_user_id)
+ @word_pairs = user.get_word_pairs(user.first_language, user.default_language)
end
def search
View
@@ -85,6 +85,21 @@ def set_default_language
add_as_default_language(default_lang) if learnings.select{ |l| l.is_default_language }.empty?
end
+ def get_words_in_language(lang)
+ words.select { |w| w.language == lang }
+ end
+
+ def get_word_pairs(from_lang, to_lang)
+ # puts "get words in language: #{get_words_in_language(from_lang).inspect}"
+ pairs = Array.new
+ word_pairs = get_words_in_language(from_lang).each do |from_word|
+ # puts "Word: #{from_word.inspect} Synonyms: #{from_word.synonyms.inspect}"
+ to_word = from_word.find_first_synonym_in_language(to_lang)
+ pairs << [from_word, to_word] unless to_word.nil?
+ end
+ pairs
+ end
+
def self.random_string(len)
rnd_string = []
chars = ('a'..'z').to_a + (0..9).to_a
View
@@ -35,9 +35,21 @@ def find_foreign_synonyms
find_synonyms { |w| w.language.code != language.code }
end
+ def find_synonyms_in_language(lang)
+ find_synonyms { |w| w.language == lang }
+ end
+
+ def find_first_synonym_in_language(lang)
+ find_synonyms_in_language(lang).first
+ end
+
def find_synonyms(&blk)
return synonyms unless block_given?
return synonyms.select(&blk)
end
+ # def find_synonyms_in_language_and_user(language_id, user_id)
+ # Find
+ # end
+
end
@@ -1,11 +1,11 @@
<%= content_tag :h2, "All words" %>
<div id="word-list" class="word_list">
<ul>
-<% @word_items.each do |wp| %>
+<% @word_pairs.each do |wp| %>
<li>
<div class="word_marker">
- <%= content_tag :div, wp[@from_lang].name, :class => "dict_left" %>
- <%= content_tag :div, wp[@to_lang].name , :class => "dict_right" %>
+ <%= content_tag :div, wp[0].name, :class => "dict_left" %>
+ <%= content_tag :div, wp[1].name , :class => "dict_right" %>
<div class="actions">
<%= link_to "edit", :controller => "word", :action => "edit", :id => wp[@from_lang].id %>
<%= link_to "delete", :controller => "word", :action => "delete" %>
@@ -1,18 +1,19 @@
<%= content_tag :h2, "All words" %>
<div id="word-list" class="word_list">
<ul>
-<% @words.each do |wp| %>
+<% @word_pairs.each do |wp| %>
<li>
<div class="word_marker">
- <%= content_tag :div, "English", :class => "dict_left" %>
- <%= content_tag :div, "Hungarian", :class => "dict_right" %>
+ <%= content_tag :div, wp[0].name, :class => "dict_left" %>
+ <%= content_tag :div, wp[1].name , :class => "dict_right" %>
<div class="actions">
- <%= link_to "edit", :controller => "word", :action => "edit" %>
- <%= link_to "delete", :controller => "word", :action => "delete" %>
+ <!--
+ <%= link_to "edit", :controller => "word", :action => "edit", :id => wp[0].id %>
+ <%= link_to "delete", :controller => "word", :action => "delete" %> %>
+ -->
</div>
</div>
</li>
<% end %>
</ul>
-
</div>
@@ -1,3 +1,5 @@
+# "default language" is considered the mother tongue of the user
+# "first language" is the one he is actually studying (a user can learn several languages at a time, but can only have one first language at any given point)
class AddFirstAndDefaultLanguageToLearning < ActiveRecord::Migration
def self.up
add_column :learnings, :is_first_language, :boolean, :null => false, :default => false
@@ -5,6 +5,7 @@ class UserTest < Test::Unit::TestCase
def setup
@english = Language.find_by_code("en")
@hungarian = Language.find_by_code("hu")
+ @french = Language.find_by_code("fr")
user_attributes = {
:email => 'john@company.com',
:login => 'john',
@@ -13,6 +14,14 @@ def setup
user_with_first_language_attributes = user_attributes.merge({:first_language_id => 1})
@user = User.create(user_attributes)
@user.languages.reload
+
+ @refuse = Word.create( :name => 'refuse', :language => @english, :user => @user )
+ @decline = Word.create( :name => 'decline', :language => @english, :user => @user )
+ @refuser = Word.create( :name => 'refuser', :language => @french, :user => @user )
+ @box = Word.create( :name => 'box', :language => @english, :user => @user )
+ @boite = Word.create( :name => 'boite', :language => @french, :user => @user )
+ # @refuse.stubs(:synonyms).returns([@decline, @refuser])
+
end
def test_should_require_login
@@ -128,7 +137,6 @@ def test_add_language
@user.languages.reload
assert_equal(langs_length + 1, @user.languages.length)
assert @user.languages.include?(@english)
- # assert_equal([@english], @user.languages)
end
def test_should_not_allow_multiple_learnings_of_same_langauge
@@ -139,7 +147,24 @@ def test_should_not_allow_multiple_learnings_of_same_langauge
def test_hungarian_should_be_default_language_if_not_given_after_saved
# @user.save
- # @user is saved when created in setup
+ # @user is already saved when created in setup
assert_equal(@hungarian, @user.default_language)
end
+
+ def test_get_words_in_language
+ my_words = @user.get_words_in_language(@english)
+ assert my_words.include?(@box)
+ assert my_words.include?(@refuse)
+ assert my_words.include?(@decline)
+ end
+
+ def test_get_word_pairs
+ @refuse.synonym = @refuser
+ @box.synonym = @boite
+ @refuse.save ; @box.save; #@refuser.save; @refuse.synonyms.reload; @box.synonyms.reload
+ word_pairs = @user.get_word_pairs(@english, @french)
+ assert word_pairs.include?([@refuse, @refuser])
+ assert word_pairs.include?([@box, @boite])
+ end
+
end
@@ -18,6 +18,7 @@ def setup
@refuse = Word.create( :name => 'refuse', :language => @english, :user => @user )
@decline = Word.create( :name => 'decline', :language => @english, :user => @user )
@refuser = Word.create( :name => 'refuser', :language => @french, :user => @user )
+ @visszautasit = Word.create( :name => 'visszautasit', :language => @hungarian, :user => @user )
@box = Word.create( :name => 'box', :language => @english, :user => @user )
@boite = Word.create( :name => 'boite', :language => @french, :user => @user )
@refuse.stubs(:synonyms).returns([@decline, @refuser])
@@ -91,6 +92,18 @@ def test_find_foreign_synonyms_not_symmetrical
assert_equal([], syns)
end
+ def test_find_synonyms_in_language
+ @refuser.synonym = @refuse
+ @refuser.synonym = @decline
+ @refuser.synonym = @visszautasit
+ @refuser.save
+ eng_synonyms = @refuser.find_synonyms_in_language(@english)
+ assert eng_synonyms.include?(@refuse)
+ assert eng_synonyms.include?(@decline)
+ assert !eng_synonyms.include?(@box)
+ assert !eng_synonyms.include?(@visszautasit)
+ end
+
def test_synonym?
assert @refuse.synonym?(@decline)
end
@@ -106,5 +119,11 @@ def test_set_synonym_attributes
@box.save
assert @box.synonyms.collect(&:name).include?("doboz")
end
-
+
+ # def test_get_word_pair_in_language
+ # eng_synonyms = @refuser.find_synonyms(@english)
+ # asssert eng_synoyms.include?(@refuse)
+ # asssert eng_synoyms.include?(@decline)
+ # end
+
end

0 comments on commit 98698b3

Please sign in to comment.