[logaling-commit] logaling/logaling-server [apply-design] Extend term as search results

Zurück zum Archiv-Index

SHIMADA Koji null+****@clear*****
Wed Sep 26 23:57:34 JST 2012


SHIMADA Koji	2012-09-26 23:57:34 +0900 (Wed, 26 Sep 2012)

  New Revision: 52f97514c1782efb79fca7b3d5b836594c6f1220
  https://github.com/logaling/logaling-server/commit/52f97514c1782efb79fca7b3d5b836594c6f1220

  Log:
    Extend term as search results

  Added files:
    app/models/additional_information_as_search_results.rb
  Modified files:
    app/controllers/search_controller.rb
    app/models/user_glossary.rb
    app/views/shared/_term.html.haml

  Modified: app/controllers/search_controller.rb (+6 -1)
===================================================================
--- app/controllers/search_controller.rb    2012-09-26 22:26:52 +0900 (549cbd7)
+++ app/controllers/search_controller.rb    2012-09-26 23:57:34 +0900 (b42aab7)
@@ -3,6 +3,11 @@ class SearchController < ApplicationController
     @query = params[:query]
     priority_glossary = signed_in? ? current_user.priority_glossary : nil
 
-    @terms = LogalingServer.repository.lookup(@query, priority_glossary)
+    @terms = LogalingServer.repository.lookup(@query, priority_glossary).map do |t|
+      Term.new do |o|
+        o.extend AdditionalInformationAsSearchResults
+        o.attributes = t
+      end
+    end
   end
 end

  Added: app/models/additional_information_as_search_results.rb (+20 -0) 100644
===================================================================
--- /dev/null
+++ app/models/additional_information_as_search_results.rb    2012-09-26 23:57:34 +0900 (5052c06)
@@ -0,0 +1,20 @@
+#coding: utf-8
+module AdditionalInformationAsSearchResults
+  attr_accessor :glossary_name
+  attr_accessor :source_language
+  attr_accessor :target_language
+  attr_accessor :snipped_source_term
+
+  def split_glossary_name_to_user_id_and_name
+    glossary_name.split("-", 2)
+  end
+
+  def owner?(user)
+    owner_id = split_glossary_name_to_user_id_and_name[0]
+    user.id == owner_id.to_i
+  end
+
+  def glossary_name_without_user_id
+    split_glossary_name_to_user_id_and_name[1]
+  end
+end

  Modified: app/models/user_glossary.rb (+13 -8)
===================================================================
--- app/models/user_glossary.rb    2012-09-26 22:26:52 +0900 (cd4ec57)
+++ app/models/user_glossary.rb    2012-09-26 23:57:34 +0900 (367433c)
@@ -1,14 +1,11 @@
 #coding: utf-8
 class UserGlossary < ActiveRecord::Base
   class << self
-    def find_by_term(term_hash, user)
-      conditions = {}
-      user_id, conditions[:name] = term_hash[:glossary_name].split("-", 2)
-      return nil if user.id != user_id.to_i
-      conditions[:source_language] = term_hash[:source_language]
-      conditions[:target_language] = term_hash[:target_language]
-      conditions[:user_id] = user.id
-      UserGlossary.find(:first, :conditions => conditions)
+    def find_by_term_and_user(term, user)
+      return nil unless term.owner?(user)
+      user.user_glossaries.with_name(term.glossary_name_without_user_id)
+                          .of_bilingualr_pair(term.source_language, term.target_language)
+                          .first
     end
   end
 
@@ -21,6 +18,14 @@ class UserGlossary < ActiveRecord::Base
 
   after_create :create_personal_project!
 
+  scope :with_name, lambda {|name|
+    where(name: name)
+  }
+
+  scope :of_bilingualr_pair, lambda {|source_language, target_language|
+    where(source_language: source_language, target_language: target_language)
+  }
+
   def glossary_name
     "%05d-%s" % [user_id, name]
   end

  Modified: app/views/shared/_term.html.haml (+12 -12)
===================================================================
--- app/views/shared/_term.html.haml    2012-09-26 22:26:52 +0900 (0919838)
+++ app/views/shared/_term.html.haml    2012-09-26 23:57:34 +0900 (025f758)
@@ -2,25 +2,25 @@
   .row
     .span9
       %span.label<>
-        = term[:source_language]
-      = render_sinpped(term[:snipped_source_term])
+        = term.source_language
+      = render_sinpped(term.snipped_source_term)
       %span.label<>
-        = term[:target_language]
-      = term[:target_term]
-      - unless term[:note].empty?
+        = term.target_language
+      = term.target_term
+      - unless term.note.empty?
         %span.label<>
           = "note"
-        = term[:note]
+        = term.note
     .span2.property
-      - if github_project?(term[:glossary_name])
+      - if github_project?(term.glossary_name)
         %p.githubProject
-          = link_to term[:glossary_name], '#'
+          = link_to term.glossary_name, '#'
       - else
-        - user_glossary = UserGlossary.find_by_term(term, current_user)
+        - user_glossary = UserGlossary.find_by_term_and_user(term, current_user)
         %p.logalingProject
-          = link_to term[:glossary_name], '#'
+          = link_to term.glossary_name, '#'
         - if user_glossary
           %p.btn-group.actions
-            = link_to raw("<i class=icon-pencil></i>"), edit_user_glossary_term_path(current_user, user_glossary, get_term_id(term)), :class => 'btn btn-mini'
-            = link_to raw("<i class=icon-trash></i>"), user_glossary_term_path(current_user, user_glossary, get_term_id(term)), method: :delete, :class => 'btn btn-mini'
+            = link_to raw("<i class=icon-pencil></i>"), edit_user_glossary_term_path(current_user, user_glossary, term.id), :class => 'btn btn-mini'
+            = link_to raw("<i class=icon-trash></i>"), user_glossary_term_path(current_user, user_glossary, term.id), method: :delete, :class => 'btn btn-mini'
 
-------------- next part --------------
An HTML attachment was scrubbed...
Download 



More information about the logaling-commit mailing list
Zurück zum Archiv-Index