SUZUKI Miho
null+****@clear*****
Wed Sep 19 16:34:29 JST 2012
SUZUKI Miho 2012-09-19 16:34:29 +0900 (Wed, 19 Sep 2012) New Revision: 28393004aaa971b0908b216fa1c1cbedd9ae2804 https://github.com/logaling/logaling-server/commit/28393004aaa971b0908b216fa1c1cbedd9ae2804 Merged c5d082c: Merge pull request #13 from logaling/enable-creating-user-glossary Log: Add term to user_glossary Added files: app/models/term.rb app/views/terms/_form.html.haml Modified files: app/controllers/terms_controller.rb app/models/user_glossary.rb app/views/terms/new.html.haml Modified: app/controllers/terms_controller.rb (+21 -0) =================================================================== --- app/controllers/terms_controller.rb 2012-09-18 11:12:45 +0900 (c82a718) +++ app/controllers/terms_controller.rb 2012-09-19 16:34:29 +0900 (0857913) @@ -1,8 +1,29 @@ +#coding: utf-8 class TermsController < ApplicationController def new + @term = Term.new + + respond_to do |format| + format.html + format.json { render json: @term } + end end def create + @term = Term.new(params[:term]) + @user_glossary = UserGlossary.find(params[:glossary_id]) + @user_glossary.add!(@term) + + respond_to do |format| + format.html { redirect_to user_glossary_path(current_user, @user_glossary), notice: 'Term was successfully added.' } + format.json { render json: @user_glossary, status: :created, location: @user_glossary } + end + rescue => e + respond_to do |format| + @term.errors.add(:source_term, e) if****@term*****? + format.html { render action: "new" } + format.json { render json: @term.errors, status: :unprocessable_entity } + end end def edit Added: app/models/term.rb (+20 -0) 100644 =================================================================== --- /dev/null +++ app/models/term.rb 2012-09-19 16:34:29 +0900 (8613803) @@ -0,0 +1,20 @@ +#coding: utf-8 +class Term + include ActiveModel::Validations + include ActiveModel::Conversion + extend ActiveModel::Naming + + attr_accessor :source_term, :target_term, :note + + validates_presence_of :source_term, :target_term + + def initialize(attributes = {}) + attributes.each do |name, value| + send("#{name}=", value) + end + end + + def persisted? + false + end +end Modified: app/models/user_glossary.rb (+25 -4) =================================================================== --- app/models/user_glossary.rb 2012-09-18 11:12:45 +0900 (534049a) +++ app/models/user_glossary.rb 2012-09-19 16:34:29 +0900 (7a593ed) @@ -1,3 +1,4 @@ +#coding: utf-8 class UserGlossary < ActiveRecord::Base attr_accessible :name, :source_language, :target_language @@ -8,12 +9,32 @@ class UserGlossary < ActiveRecord::Base after_create :create_personal_project! + def glossary_name + "%05d-%s" % [user_id, name] + end + + def add!(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) + if glossary.bilingual_pair_exists?(term.source_term, term.target_term) + raise Logaling::TermError, "term '#{term.source_term}: #{term.target_term}' already exists in '#{name}'" + end + + #FIXME: tentative treatment :-< + _default_internal = Encoding.default_internal + Encoding.default_internal = nil + glossary.add(term.source_term, term.target_term, term.note) + LogalingServer.repository.index + ensure + Encoding.default_internal = _default_internal if _default_internal + end + private def create_personal_project! LogalingServer.repository.create_personal_project(glossary_name, source_language, target_language) end - - def glossary_name - "%05d-%s" % [user_id, name] - end end Added: app/views/terms/_form.html.haml (+19 -0) 100644 =================================================================== --- /dev/null +++ app/views/terms/_form.html.haml 2012-09-19 16:34:29 +0900 (a3b99de) @@ -0,0 +1,19 @@ += form_for @term, url: {action: 'create'} do |f| + -if****@term*****? + #error_explanation + %h2= "#{pluralize(@term.errors.count, "error")} prohibited this term from being saved:" + %ul + -****@term*****_messages.each do |msg| + %li= msg + + .well + = f.label :source_term, "Source term:" + = f.text_field :source_term + = f.label :target_term, "Target term:" + = f.text_field :target_term + = f.label :note, "Note:" + = f.text_field :note + + .actions + = f.submit 'Save', :class => 'btn btn-primary' + Modified: app/views/terms/new.html.haml (+2 -2) =================================================================== --- app/views/terms/new.html.haml 2012-09-18 11:12:45 +0900 (c4a0945) +++ app/views/terms/new.html.haml 2012-09-19 16:34:29 +0900 (85ce62c) @@ -1,2 +1,2 @@ -%h1 Terms#new -%p Find me in app/views/terms/new.html.haml \ No newline at end of file +%h1 New Term += render 'form' -------------- next part -------------- An HTML attachment was scrubbed... Download