[logaling-commit] logaling/logaling-server [enable-copy-user-glossary] Check if original user glossary id exists

Back to archive index

SHIMADA Koji null+****@clear*****
Fri Sep 28 15:10:10 JST 2012


SHIMADA Koji	2012-09-28 15:10:10 +0900 (Fri, 28 Sep 2012)

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

  Log:
    Check if original user glossary id exists

  Modified files:
    app/controllers/user_glossaries_controller.rb
    app/models/user_glossary.rb

  Modified: app/controllers/user_glossaries_controller.rb (+2 -3)
===================================================================
--- app/controllers/user_glossaries_controller.rb    2012-09-28 14:29:20 +0900 (49bd1ea)
+++ app/controllers/user_glossaries_controller.rb    2012-09-28 15:10:10 +0900 (37d6dc7)
@@ -13,9 +13,8 @@ class UserGlossariesController < ApplicationController
 
   # GET /user_glossaries/new
   def new
-    @user_glossary = UserGlossary.new
-    if params[:original_user_glossary_id].present?
-      @user_glossary.original_user_glossary_id = params[:original_user_glossary_id]
+    @user_glossary = UserGlossary.new do |u|
+      u.set_original_user_glossary_id(params[:original_user_glossary_id])
     end
   end
 

  Modified: app/models/user_glossary.rb (+15 -0)
===================================================================
--- app/models/user_glossary.rb    2012-09-28 14:29:20 +0900 (3ff0406)
+++ app/models/user_glossary.rb    2012-09-28 15:10:10 +0900 (20026b7)
@@ -17,6 +17,9 @@ class UserGlossary < ActiveRecord::Base
 
   validates_presence_of :name, :source_language, :target_language
   validates_uniqueness_of :name, scope: [:user_id, :source_language, :target_language]
+  validate :original_user_glossary_id_must_exist,
+    if: "original_user_glossary_id.present?",
+    on: :create
 
   after_create :create_personal_project!
   after_destroy :remove_personal_project!
@@ -89,7 +92,19 @@ class UserGlossary < ActiveRecord::Base
     terms = glossary.terms(annotation).map { |term_attrs| GlossaryEntry.new(term_attrs) }
   end
 
+  def set_original_user_glossary_id(user_glossary_id)
+    if UserGlossary.find_by_id(user_glossary_id).present?
+      @original_user_glossary_id = user_glossary_id
+    end
+  end
+
   private
+  def original_user_glossary_id_must_exist
+    unless UserGlossary.find_by_id(@original_user_glossary_id).present?
+      errors.add :original_user_glossary_id, "の指定が正しくありません"
+    end
+  end
+
   def find_glossary
     LogalingServer.repository.find_glossary(glossary_name, source_language, target_language)
   end
-------------- next part --------------
An HTML attachment was scrubbed...
下載 



More information about the logaling-commit mailing list
Back to archive index