SUZUKI Miho
null+****@clear*****
Mon Dec 17 16:03:29 JST 2012
SUZUKI Miho 2012-12-17 16:03:29 +0900 (Mon, 17 Dec 2012) New Revision: 989e089578999c8841484eab983d480f91d8a1d7 https://github.com/logaling/logaling-server/commit/989e089578999c8841484eab983d480f91d8a1d7 Log: Add validation for multiple input Modified files: app/controllers/terms_controller.rb app/controllers/user_glossaries_controller.rb app/models/user_glossary.rb app/views/terms/_form.html.haml Modified: app/controllers/terms_controller.rb (+6 -4) =================================================================== --- app/controllers/terms_controller.rb 2012-12-17 16:02:41 +0900 (a156efe) +++ app/controllers/terms_controller.rb 2012-12-17 16:03:29 +0900 (b4bbd1b) @@ -5,18 +5,20 @@ class TermsController < ApplicationController def new @term = GlossaryEntry.new + @terms = [GlossaryEntry.new] end def create + @terms = [] params[:glossary_entry].each do |key, glossary_entry| next if glossary_entry[:source_term].empty? && glossary_entry[:target_term].empty? - @term = GlossaryEntry.new(glossary_entry) - # TODO: 毎回command側のindexが走るので注意。あとで改善する - @user_glossary.add!(@term) + @terms << GlossaryEntry.new(glossary_entry) end + @user_glossary.add!(@terms) redirect_to user_glossary_path(current_user, @user_glossary), notice: 'Term was successfully added.' - rescue ArgumentError, Logaling::GlossaryNotFound + rescue ArgumentError, Logaling::GlossaryNotFound => e + logger.debug e.message render action: "new" end Modified: app/controllers/user_glossaries_controller.rb (+1 -0) =================================================================== --- app/controllers/user_glossaries_controller.rb 2012-12-17 16:02:41 +0900 (306b5ce) +++ app/controllers/user_glossaries_controller.rb 2012-12-17 16:03:29 +0900 (d5f4cc4) @@ -7,6 +7,7 @@ class UserGlossariesController < ApplicationController def show @user = User.find_by_name(params[:user_id]) @term = GlossaryEntry.new + @terms = [GlossaryEntry.new] @user_glossary =****@user*****_glossary(params[:id]) @registered_terms = Kaminari.paginate_array(@user_glossary.terms).page(params[:page]) rescue ActiveRecord::RecordNotFound Modified: app/models/user_glossary.rb (+17 -6) =================================================================== --- app/models/user_glossary.rb 2012-12-17 16:02:41 +0900 (af6aa63) +++ app/models/user_glossary.rb 2012-12-17 16:03:29 +0900 (8a5f927) @@ -51,17 +51,28 @@ class UserGlossary < ActiveRecord::Base "%s/%s" % [name, "#{source_language}-#{target_language}"] end - def add!(term) - raise ArgumentError unless term.valid? + def add!(terms) + valid = true + terms.each do |term| + valid = false unless term.valid? + end + raise ArgumentError unless valid#terms.all?(&:valid?) glossary = find_glossary raise Logaling::GlossaryNotFound unless glossary - glossary.add(term.source_term, term.target_term, term.note) + terms.each do |term| + if glossary.bilingual_pair_exists?(term.source_term, term.target_term, term.note) + term.errors.add(:target_term, " #{term.target_term} は既に登録されています") + raise ArgumentError + end + end - rescue Logaling::TermError - term.errors.add(:source_term, "term '#{term.source_term}: #{term.target_term}' already exists in '#{name}'") - raise ArgumentError + terms.each do |term| + p term + glossary.add(term.source_term, term.target_term, term.note, false) + end + glossary.index! end def update(term, new_term) Modified: app/views/terms/_form.html.haml (+8 -12) =================================================================== --- app/views/terms/_form.html.haml 2012-12-17 16:02:41 +0900 (2ce1afa) +++ app/views/terms/_form.html.haml 2012-12-17 16:03:29 +0900 (26c1958) @@ -1,20 +1,16 @@ -= form_for @term, url: user_glossary_terms_path(current_user, @user_glossary), :html => { :class => 'form-horizontal' } 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 - += form_for @terms, url: user_glossary_terms_path(current_user, @user_glossary), :html => { :class => 'form-horizontal' } do |f| .control-group = f.label :source_term, :class => 'control-label required' = f.label :target_term, :class => 'control-label required' = f.label :note, :class => 'control-label' - .control-group#term1 - = f.text_field :source_term, :id => 'source_term1', :class => 'source_term', :name => 'glossary_entry[1][source_term]' - = f.text_field :target_term, :id => 'target_term1', :class => 'target_term', :name => 'glossary_entry[1][target_term]' - = f.text_field :note, :id => 'note1', :class => 'note', :name => 'glossary_entry[1][note]' + -****@terms*****_with_index do |term, i| + .control-group{:id => "term#{i+1}"} + = text_field_tag :source_term, term.source_term, :id => 'source_term1', :class => 'source_term', :name => "glossary_entry[#{i+1}][source_term]" + = text_field_tag :target_term, term.target_term, :id => 'target_term1', :class => 'target_term', :name => "glossary_entry[#{i+1}][target_term]" + = text_field_tag :note, term.note,:id => 'note1', :class => 'note', :name => "glossary_entry[#{i+1}][note]" + - term.errors.full_messages.each do |msg| + %span.text-error= msg .control-group = f.button 'フォームを追加する', :id => 'add_btn', :class => 'btn btn-small', :type => 'button' -------------- next part -------------- An HTML attachment was scrubbed... Download