From fd7c3c3deb9a4f62d4c84b6ab35d0c66eaacd677 Mon Sep 17 00:00:00 2001
From: "TAKAHASHI,Toru"
Date: Sun, 1 Dec 2019 21:51:18 +0900
Subject: [PATCH] [phase-23]Add csv file importing
---
app/controllers/glossary_terms_controller.rb | 5 +++++
app/models/glossary_term.rb | 19 +++++++++++++++++++
app/views/glossary_terms/_sidebar.html.erb | 12 ++++++++++++
config/locales/en.yml | 2 ++
config/locales/ja.yml | 4 +++-
config/routes.rb | 1 +
init.rb | 2 +-
7 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/app/controllers/glossary_terms_controller.rb b/app/controllers/glossary_terms_controller.rb
index c05f33e..5787509 100644
--- a/app/controllers/glossary_terms_controller.rb
+++ b/app/controllers/glossary_terms_controller.rb
@@ -64,6 +64,11 @@ class GlossaryTermsController < ApplicationController
render_404
end
+ def import
+ GlossaryTerm.import(params[:file], @project)
+ redirect_to project_glossary_terms_path(@project)
+ end
+
private
def glossary_term_params
diff --git a/app/models/glossary_term.rb b/app/models/glossary_term.rb
index ba569b5..66608b4 100644
--- a/app/models/glossary_term.rb
+++ b/app/models/glossary_term.rb
@@ -1,3 +1,5 @@
+require 'csv'
+
class GlossaryTerm < ActiveRecord::Base
belongs_to :category, class_name: 'GlossaryCategory', foreign_key: 'category_id'
belongs_to :project
@@ -37,4 +39,21 @@ class GlossaryTerm < ActiveRecord::Base
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
diff --git a/app/views/glossary_terms/_sidebar.html.erb b/app/views/glossary_terms/_sidebar.html.erb
index efe4cd4..c9aa6d9 100644
--- a/app/views/glossary_terms/_sidebar.html.erb
+++ b/app/views/glossary_terms/_sidebar.html.erb
@@ -17,6 +17,18 @@
{ controller: :glossary_terms, action: :new, project_id: @project },
class: 'icon icon-add' %>
+
+
<%=l :label_glossary_category %>
<%= link_to_if_authorized l(:label_glossary_category_new),
{ controller: :glossary_categories, action: :new, project_id: @project},
diff --git a/config/locales/en.yml b/config/locales/en.yml
index ad593b5..e2e0a65 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -3,6 +3,7 @@ en:
label_glossary_terms: "Glossary terms"
label_glossary_term: "Glossary term"
label_glossary_term_new: "New glossary term"
+ label_glossary_term_import_csv: "Import from CSV"
label_glossary_categories: "Glossary categories"
label_glossary_category: "Glossary category"
label_glossary_category_new: "New glossary category"
@@ -11,6 +12,7 @@ en:
project_module_glossary: Glossary
label_view: View
+ label_import: Import
label_glossary_index: index
label_not_categorized: Not categorized
label_grouping: Grouping
diff --git a/config/locales/ja.yml b/config/locales/ja.yml
index 6bbac65..c3c8042 100644
--- a/config/locales/ja.yml
+++ b/config/locales/ja.yml
@@ -2,6 +2,7 @@ ja:
label_glossary_terms: "用語集"
label_glossary_term: "用語"
label_glossary_term_new: "用語の作成"
+ label_glossary_term_import_csv: "CSVからインポート"
label_glossary_categories: "用語のカテゴリ一覧"
label_glossary_category: "用語のカテゴリ"
label_glossary_category_new: "カテゴリの作成"
@@ -10,7 +11,8 @@ ja:
project_module_glossary: 用語集
label_view: 表示
- label_glossary_index: 索引
+ label_import: インポート
+ label_glossary_index: 索引
label_not_categorized: 未分類
label_grouping: グループ化
label_categorized: カテゴリで分類
diff --git a/config/routes.rb b/config/routes.rb
index 8c14ca6..bdf1873 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -9,6 +9,7 @@ Rails.application.routes.draw do
end
collection do
post 'preview'
+ post 'import'
end
end
resources :glossary_categories
diff --git a/init.rb b/init.rb
index de8889c..34f24c9 100644
--- a/init.rb
+++ b/init.rb
@@ -19,7 +19,7 @@ Redmine::Plugin.register :redmine_glossary do
glossary_categories: [:index, :show]
}
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],
},
require: :member