2019-12-01 13:51:18 +01:00
|
|
|
require 'csv'
|
|
|
|
|
2018-04-30 15:28:18 +02:00
|
|
|
class GlossaryTerm < ActiveRecord::Base
|
2018-05-06 09:45:27 +02:00
|
|
|
belongs_to :category, class_name: 'GlossaryCategory', foreign_key: 'category_id'
|
2018-05-12 07:26:43 +02:00
|
|
|
belongs_to :project
|
2019-10-20 11:33:22 +02:00
|
|
|
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
|
2019-10-27 13:48:51 +01:00
|
|
|
belongs_to :updater, class_name: 'User', foreign_key: 'updater_id'
|
2018-05-19 21:43:22 +02:00
|
|
|
|
2018-06-08 13:57:51 +02:00
|
|
|
# class method from Redmine::Acts::Attachable::ClassMethods
|
2019-10-27 13:48:51 +01:00
|
|
|
acts_as_attachable view_permission: :view_glossary_terms, edit_permission: :manage_glossary_terms,
|
|
|
|
delete_permission: :manage_glossary_terms
|
2018-06-11 14:31:38 +02:00
|
|
|
|
|
|
|
acts_as_event datetime: :updated_at,
|
|
|
|
description: :description,
|
|
|
|
author: nil,
|
|
|
|
title: Proc.new {|o| "#{l(:glossary_title)} ##{o.id} - #{o.name}" },
|
|
|
|
url: Proc.new {|o| { controller: 'glossary_terms',
|
|
|
|
action: 'show',
|
|
|
|
id: o.id,
|
|
|
|
project_id: o.project }
|
|
|
|
}
|
|
|
|
|
2019-10-27 13:48:51 +01:00
|
|
|
acts_as_searchable columns: [ "#{table_name}.name", "#{table_name}.description", "#{table_name}.rubi"],
|
|
|
|
preload: [:project ],
|
|
|
|
date_column: "#{table_name}.created_at",
|
|
|
|
scope: joins(:project),
|
|
|
|
permission: :view_glossary_terms
|
|
|
|
|
2018-06-11 14:31:38 +02:00
|
|
|
acts_as_activity_provider scope: joins(:project),
|
|
|
|
type: 'glossary_terms',
|
2019-10-27 13:48:51 +01:00
|
|
|
permission: :view_glossary_terms,
|
2018-06-11 14:31:38 +02:00
|
|
|
timestamp: :updated_at
|
2019-10-27 13:48:51 +01:00
|
|
|
|
2018-05-19 21:43:22 +02:00
|
|
|
scope :search_by_name, -> (keyword) {
|
|
|
|
where 'name like ?', "#{sanitize_sql_like(keyword)}%"
|
|
|
|
}
|
|
|
|
|
2018-06-05 18:21:17 +02:00
|
|
|
scope :search_by_rubi, -> (keyword) {
|
|
|
|
where 'rubi like ?', "#{sanitize_sql_like(keyword)}%"
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:51:18 +01:00
|
|
|
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
|
|
|
|
|
2018-04-30 15:28:18 +02:00
|
|
|
end
|