[logaling-commit] logaling/logaling-server [enable-creating-user-glossary] Enable destroy term in user glossary

Zurück zum Archiv-Index

SHIMADA Koji null+****@clear*****
Wed Sep 19 23:14:52 JST 2012


SHIMADA Koji	2012-09-19 23:14:52 +0900 (Wed, 19 Sep 2012)

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

  Log:
    Enable destroy term in user glossary

  Modified files:
    app/controllers/terms_controller.rb
    app/models/term.rb
    app/models/user_glossary.rb
    app/views/user_glossaries/show.html.haml
    config/routes.rb

  Modified: app/controllers/terms_controller.rb (+3 -0)
===================================================================
--- app/controllers/terms_controller.rb    2012-09-19 20:44:28 +0900 (09d37f0)
+++ app/controllers/terms_controller.rb    2012-09-19 23:14:52 +0900 (6ace06b)
@@ -23,6 +23,9 @@ class TermsController < ApplicationController
   end
 
   def destroy
+    @term = Term.find(params[:id])
+    @user_glossary.delete(@term)
+    redirect_to user_glossary_path(current_user, @user_glossary), notice: 'Term was successfully destroyed.'
   end
 
   private

  Modified: app/models/term.rb (+24 -7)
===================================================================
--- app/models/term.rb    2012-09-19 20:44:28 +0900 (70f8443)
+++ app/models/term.rb    2012-09-19 23:14:52 +0900 (acff1e0)
@@ -1,5 +1,27 @@
 #coding: utf-8
 class Term
+  class << self
+    def set_value(hash_value)
+      hash = hash_value.with_indifferent_access
+      term = Term.new
+      self.attributes.each do |key, value|
+        term[key] = hash[key] if hash[key].present?
+      end
+      term
+    end
+
+    def find(id)
+      source_term, target_term = id_to_source_term_and_target_term(id)
+      Term.new do |t|
+        t.source_term = source_term
+        t.target_term = target_term
+      end
+    end
+
+    def id_to_source_term_and_target_term(id)
+      id.split("source:")[1].split(" target:", 2)
+    end
+  end
   include ActiveAttr::Model
 
   attribute :source_term, type: String, default: ''
@@ -8,12 +30,7 @@ class Term
 
   validates_presence_of :source_term, :target_term
 
-  def self.set_value(hash_value)
-    hash = hash_value.with_indifferent_access
-    term = Term.new
-    self.attributes.each do |key, value|
-      term[key] = hash[key] if hash[key].present?
-    end
-    term
+  def id
+    "source:#{source_term} target:#{target_term}"
   end
 end

  Modified: app/models/user_glossary.rb (+15 -0)
===================================================================
--- app/models/user_glossary.rb    2012-09-19 20:44:28 +0900 (7a45487)
+++ app/models/user_glossary.rb    2012-09-19 23:14:52 +0900 (3eb2ca7)
@@ -28,6 +28,21 @@ class UserGlossary < ActiveRecord::Base
     LogalingServer.repository.index
   end
 
+  def delete(term)
+    project = LogalingServer.repository.find_project(glossary_name)
+    raise Logaling::ProjectNotFound unless project
+    raise Logaling::ProjectNotFound if project.class.name == 'Logaling::ImportedProject'
+
+    raise Logaling::TermError unless term.valid?
+    glossary = project.glossary(source_language, target_language)
+    unless glossary.bilingual_pair_exists?(term.source_term, term.target_term)
+      raise Logaling::TermError, "term '#{term.source_term}: #{term.target_term}' doesn't exist in '#{name}'"
+    end
+
+    glossary.delete(term.source_term, term.target_term)
+    LogalingServer.repository.index
+  end
+
   def terms(annotation=nil)
     glossary = LogalingServer.repository.find_glossary(glossary_name, source_language, target_language)
     raise Logaling::GlossaryNotFound unless glossary

  Modified: app/views/user_glossaries/show.html.haml (+1 -0)
===================================================================
--- app/views/user_glossaries/show.html.haml    2012-09-19 20:44:28 +0900 (6751d70)
+++ app/views/user_glossaries/show.html.haml    2012-09-19 23:14:52 +0900 (e827cac)
@@ -11,3 +11,4 @@
         = term.source_term
         = term.target_term
         = term.note
+        = link_to "Delete", user_glossary_term_path(current_user, @user_glossary, term.id), method: :delete

  Modified: config/routes.rb (+1 -10)
===================================================================
--- config/routes.rb    2012-09-19 20:44:28 +0900 (41faa90)
+++ config/routes.rb    2012-09-19 23:14:52 +0900 (7857ccf)
@@ -17,16 +17,7 @@ LogalingServer::Application.routes.draw do
       :path => 'glossaries',
       :only => [:new, :create, :show],
       :as => :glossaries do
-      resources :terms,
-        :path => 'terms',
-        :as => :terms,
-        :only => [:new, :create] do
-        collection do
-          delete "destroy"
-          post "edit"
-          put "update"
-        end
-      end
+        resources :terms
     end
   end
 
-------------- next part --------------
An HTML attachment was scrubbed...
Download 



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