[phase-23]Add csv file importing

This commit is contained in:
TAKAHASHI,Toru 2019-12-01 21:51:18 +09:00
parent 517decb464
commit fd7c3c3deb
7 changed files with 43 additions and 2 deletions

View File

@ -64,6 +64,11 @@ class GlossaryTermsController < ApplicationController
render_404 render_404
end end
def import
GlossaryTerm.import(params[:file], @project)
redirect_to project_glossary_terms_path(@project)
end
private private
def glossary_term_params def glossary_term_params

View File

@ -1,3 +1,5 @@
require 'csv'
class GlossaryTerm < ActiveRecord::Base class GlossaryTerm < ActiveRecord::Base
belongs_to :category, class_name: 'GlossaryCategory', foreign_key: 'category_id' belongs_to :category, class_name: 'GlossaryCategory', foreign_key: 'category_id'
belongs_to :project belongs_to :project
@ -37,4 +39,21 @@ class GlossaryTerm < ActiveRecord::Base
where 'rubi like ?', "#{sanitize_sql_like(keyword)}%" where 'rubi like ?', "#{sanitize_sql_like(keyword)}%"
} }
def self.csv_attributes
["name", "name_en", "datatype", "codename", "description", "rubi", "abbr_whole"]
end
def self.import(file, project)
CSV.foreach(file.path, headers: true, encoding: "CP932:UTF-8" ) do |row|
term = new
term.attributes = row.to_hash.slice(*csv_attributes)
term.project = project
unless row["category"].blank?
term.category = GlossaryCategory.find_by(name: row["category"]) ||
GlossaryCategory.create(name: row["category"], project: project)
end
term.save!
end
end
end end

View File

@ -17,6 +17,18 @@
{ controller: :glossary_terms, action: :new, project_id: @project }, { controller: :glossary_terms, action: :new, project_id: @project },
class: 'icon icon-add' %></p> class: 'icon icon-add' %></p>
<fieldset class="collapsible collapsed">
<legend onclick="toggleFieldset(this);" class="icon icon-collapsed">
<%=l :label_glossary_term_import_csv %>
</legend>
<div style="display: none;">
<%= form_with url: import_project_glossary_terms_path, method: :post, local: true do |form| %>
<%= form.file_field :file %>
<%= form.submit l(:label_import) %>
<% end %>
</div>
</fieldset>
<h3><%=l :label_glossary_category %></h3> <h3><%=l :label_glossary_category %></h3>
<p><%= link_to_if_authorized l(:label_glossary_category_new), <p><%= link_to_if_authorized l(:label_glossary_category_new),
{ controller: :glossary_categories, action: :new, project_id: @project}, { controller: :glossary_categories, action: :new, project_id: @project},

View File

@ -3,6 +3,7 @@ en:
label_glossary_terms: "Glossary terms" label_glossary_terms: "Glossary terms"
label_glossary_term: "Glossary term" label_glossary_term: "Glossary term"
label_glossary_term_new: "New glossary term" label_glossary_term_new: "New glossary term"
label_glossary_term_import_csv: "Import from CSV"
label_glossary_categories: "Glossary categories" label_glossary_categories: "Glossary categories"
label_glossary_category: "Glossary category" label_glossary_category: "Glossary category"
label_glossary_category_new: "New glossary category" label_glossary_category_new: "New glossary category"
@ -11,6 +12,7 @@ en:
project_module_glossary: Glossary project_module_glossary: Glossary
label_view: View label_view: View
label_import: Import
label_glossary_index: index label_glossary_index: index
label_not_categorized: Not categorized label_not_categorized: Not categorized
label_grouping: Grouping label_grouping: Grouping

View File

@ -2,6 +2,7 @@ ja:
label_glossary_terms: "用語集" label_glossary_terms: "用語集"
label_glossary_term: "用語" label_glossary_term: "用語"
label_glossary_term_new: "用語の作成" label_glossary_term_new: "用語の作成"
label_glossary_term_import_csv: "CSVからインポート"
label_glossary_categories: "用語のカテゴリ一覧" label_glossary_categories: "用語のカテゴリ一覧"
label_glossary_category: "用語のカテゴリ" label_glossary_category: "用語のカテゴリ"
label_glossary_category_new: "カテゴリの作成" label_glossary_category_new: "カテゴリの作成"
@ -10,7 +11,8 @@ ja:
project_module_glossary: 用語集 project_module_glossary: 用語集
label_view: 表示 label_view: 表示
label_glossary_index: 索引 label_import: インポート
label_glossary_index: 索引
label_not_categorized: 未分類 label_not_categorized: 未分類
label_grouping: グループ化 label_grouping: グループ化
label_categorized: カテゴリで分類 label_categorized: カテゴリで分類

View File

@ -9,6 +9,7 @@ Rails.application.routes.draw do
end end
collection do collection do
post 'preview' post 'preview'
post 'import'
end end
end end
resources :glossary_categories resources :glossary_categories

View File

@ -19,7 +19,7 @@ Redmine::Plugin.register :redmine_glossary do
glossary_categories: [:index, :show] glossary_categories: [:index, :show]
} }
permission :manage_glossary_terms, { permission :manage_glossary_terms, {
glossary_terms: [:new, :create, :edit, :update, :destroy], glossary_terms: [:new, :create, :edit, :update, :destroy, :import],
glossary_categories: [:new, :create, :edit, :update, :destroy], glossary_categories: [:new, :create, :edit, :update, :destroy],
}, },
require: :member require: :member