[logaling-commit] logaling/logaling-server [master] Change uri for user glossary

Back to archive index

SHIMADA Koji null+****@clear*****
Mon Oct 8 11:13:29 JST 2012


SHIMADA Koji	2012-10-08 11:13:29 +0900 (Mon, 08 Oct 2012)

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

  Merged 7321e38: Merge pull request #26 from logaling/improve-glossary-uri

  Log:
    Change uri for user glossary
    
    before: /users/:username/glossaries/:id/
    after:  /users/:username/glossaries/:glossary_name/:source_language-:target_language/

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

  Modified: app/controllers/user_glossaries_controller.rb (+2 -1)
===================================================================
--- app/controllers/user_glossaries_controller.rb    2012-10-08 10:17:45 +0900 (a6460fd)
+++ app/controllers/user_glossaries_controller.rb    2012-10-08 11:13:29 +0900 (5cf9572)
@@ -5,8 +5,9 @@ class UserGlossariesController < ApplicationController
 
   # GET /user_glossaries/1
   def show
+    @user = User.find_by_name(params[:user_id])
     @term = GlossaryEntry.new
-    @user_glossary = UserGlossary.find(params[:id])
+    @user_glossary = UserGlossary.of(params[:id], @user)
     @registered_terms = Kaminari.paginate_array(@user_glossary.terms).page(params[:page])
   rescue ActiveRecord::RecordNotFound
     render :file => 'public/404.html', :status => 404, :layout => false

  Modified: app/models/user_glossary.rb (+14 -0)
===================================================================
--- app/models/user_glossary.rb    2012-10-08 10:17:45 +0900 (a1994e1)
+++ app/models/user_glossary.rb    2012-10-08 11:13:29 +0900 (ed9d8eb)
@@ -1,6 +1,16 @@
 #coding: utf-8
 class UserGlossary < ActiveRecord::Base
   class << self
+    def of(id, user)
+      name, bilingualr_pair = id.split('/', 2)
+      source_language, target_language = bilingualr_pair.split('-', 2)
+      glossary = self.with_name(name).of_bilingualr_pair(source_language, target_language).where(user_id: user).first
+      unless glossary
+        raise ActiveRecord::RecordNotFound
+      end
+      glossary
+    end
+
     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)
@@ -36,6 +46,10 @@ class UserGlossary < ActiveRecord::Base
     "%05d-%s" % [user_id, name]
   end
 
+  def to_param
+    "%s/%s" % [name, "#{source_language}-#{target_language}"]
+  end
+
   def add!(term)
     raise ArgumentError unless term.valid?
 

  Modified: app/views/user_glossaries/show.html.haml (+1 -1)
===================================================================
--- app/views/user_glossaries/show.html.haml    2012-10-08 10:17:45 +0900 (f081c23)
+++ app/views/user_glossaries/show.html.haml    2012-10-08 11:13:29 +0900 (5a5711b)
@@ -1,7 +1,7 @@
 - @title = "#{@user_glossary.title_text} の用語一覧"
 .container
   %h1
-    = @user_glossary.title_text
+    = "#{@user.name} / #{@user_glossary.title_text}"
   %p
     = @user_glossary.description
   %h2

  Modified: config/routes.rb (+3 -1)
===================================================================
--- config/routes.rb    2012-10-08 10:17:45 +0900 (dad192f)
+++ config/routes.rb    2012-10-08 11:13:29 +0900 (5b6192c)
@@ -17,9 +17,11 @@ LogalingServer::Application.routes.draw do
       :only => [:new, :create, :destroy]
     resources :user_glossaries,
       :path => 'glossaries',
+      :constraints => {:id => %r{[^/]+/[^/]+}},
       :only => [:new, :create, :show, :destroy],
       :as => :glossaries do
-        resources :terms
+        resources :terms,
+          :constraints => {:id => %r{[^/]+}, :glossary_id => %r{[^/]+/[^/]+}}
     end
     resources :user_configs,
       :path => 'configs',
-------------- next part --------------
An HTML attachment was scrubbed...
Download 



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