socialforge/app/models/syllabus.rb

130 lines
3.7 KiB
Ruby
Raw Normal View History

2016-07-01 11:11:53 +08:00
# encoding: utf-8
2016-06-14 10:38:11 +08:00
class Syllabus < ActiveRecord::Base
2016-06-29 10:45:56 +08:00
include Redmine::SafeAttributes
include ApplicationHelper
2016-10-09 13:16:03 +08:00
#elasticsearch
2016-10-09 12:10:34 +08:00
include Elasticsearch::Model
2016-10-09 13:16:03 +08:00
#elasticsearch kaminari init
Kaminari::Hooks.init
Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari
settings index: { number_of_shards: 5 } do
mappings dynamic: 'false' do
2016-10-09 13:29:13 +08:00
indexes :title, analyzer: 'smartcn',index_options: 'offsets'
2016-10-09 13:16:03 +08:00
indexes :description, analyzer: 'smartcn',index_options: 'offsets'
2016-10-09 13:29:13 +08:00
indexes :updated_at, index:"not_analyzed", type:'date'
2016-10-09 13:16:03 +08:00
end
end
acts_as_taggable
2016-06-29 10:45:56 +08:00
acts_as_attachable
has_many_kindeditor_assets :assets, :dependent => :destroy
2016-06-14 10:38:11 +08:00
belongs_to :user
has_many :courses
has_many :journals_for_messages, :as => :jour, :dependent => :destroy
2016-09-23 09:22:47 +08:00
has_many :syllabus_members, :dependent => :destroy
2016-09-23 16:44:49 +08:00
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
has_one :praise_tread_cache, as: :object, dependent: :destroy
2016-07-04 16:19:05 +08:00
attr_accessible :description, :user_id, :title, :eng_name, :syllabus_type, :credit, :hours, :theory_hours, :practice_hours, :applicable_major, :pre_course
safe_attributes 'title','user', 'description', 'eng_name', 'syllabus_type', 'credit', 'hours', 'theory_hours', 'practice_hours', 'credit', 'applicable_major', 'pre_course'
validates :title, :user_id, presence: true
2016-06-29 10:45:56 +08:00
2016-10-09 11:27:35 +08:00
after_update :update_syllabus_ealasticsearch_index
after_create :create_syllabus_ealasticsearch_index
before_destroy :delete_syllabus_ealasticsearch_index
2016-10-09 15:05:50 +08:00
scope :indexable,lambda { where(nil) }#用于elastic建索引的scope
2016-07-07 09:02:41 +08:00
scope :like, lambda {|arg|
if arg.blank?
where(nil)
else
pattern = "%#{arg.to_s.strip.downcase}%"
where(" LOWER(title) LIKE :p ", :p => pattern)
end
}
2016-06-29 10:45:56 +08:00
def delete_kindeditor_assets
delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::SYLLABUS
end
2016-07-01 11:11:53 +08:00
def syllabus_type_str
case self.syllabus_type
when 1
type = "公共必修课"
when 2
type = "学科必修课"
when 3
type = "专业选修课"
when 4
type = "实践必修课"
when 5
type = "实践选修课"
end
type
end
###添加回复
def self.add_syllabus_jour(user, notes, id, root_id, options = {})
syllabus = Syllabus.find(id)
if options.count == 0
jfm = syllabus.journals_for_messages.build(:user_id => user.id, :notes => notes, :reply_id => 0, :root_id => root_id)
else
jfm = syllabus.journals_for_messages.build(options)
end
jfm.save
jfm
end
2016-10-09 10:35:43 +08:00
2016-10-09 11:27:35 +08:00
def create_syllabus_ealasticsearch_index
return if Rails.env.development?
self.__elasticsearch__.index_document
end
def update_syllabus_ealasticsearch_index
return if Rails.env.development?
begin
self.__elasticsearch__.update_document
rescue => e
self.__elasticsearch__.index_document
end
end
def delete_syllabus_ealasticsearch_index
return if Rails.env.development?
begin
self.__elasticsearch__.delete_document
rescue => e
end
end
2016-10-09 10:35:43 +08:00
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
type:"most_fields",
operator: "or",
2016-10-10 14:00:10 +08:00
fields: ['title']
2016-10-09 10:35:43 +08:00
}
},
sort: {
_score:{order: "desc" },
updated_at:{order:"desc"}
},
highlight: {
pre_tags: ['<span class="c_red">'],
post_tags: ['</span>'],
fields: {
title: {},
description: {}
}
}
}
)
end
2016-06-14 10:38:11 +08:00
end