# encoding: utf-8 class Syllabus < ActiveRecord::Base include Redmine::SafeAttributes include ApplicationHelper #elasticsearch include Elasticsearch::Model #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 indexes :title, analyzer: 'smartcn',index_options: 'offsets' indexes :description, analyzer: 'smartcn',index_options: 'offsets' indexes :updated_at, index:"not_analyzed", type:'date' end end acts_as_taggable acts_as_attachable has_many_kindeditor_assets :assets, :dependent => :destroy belongs_to :user has_many :courses has_many :homework_banks has_many :reference_materials, :dependent => :destroy has_many :syllabus_update_records, :dependent => :destroy has_many :journals_for_messages, :as => :jour, :dependent => :destroy has_many :syllabus_members, :dependent => :destroy has_many :praise_tread, as: :praise_tread_object, dependent: :destroy has_one :praise_tread_cache, as: :object, dependent: :destroy belongs_to :discipline_category belongs_to :first_level_discipline belongs_to :major attr_accessible :description, :user_id, :title, :eng_name, :syllabus_type, :credit, :hours, :theory_hours, :practice_hours, :applicable_major, :pre_course, :major_level, :discipline_category_id, :first_level_discipline_id, :major_id,:support_shixuns safe_attributes 'title','user', 'description', 'eng_name', 'syllabus_type', 'credit', 'hours', 'theory_hours', 'practice_hours', 'credit', 'applicable_major', 'pre_course', 'major_level', 'discipline_category_id', "first_level_discipline_id" validates :title, :user_id, presence: true after_update :update_syllabus_ealasticsearch_index after_create :create_syllabus_ealasticsearch_index before_destroy :delete_syllabus_ealasticsearch_index scope :indexable,lambda { where(nil) }#用于elastic建索引的scope scope :like, lambda {|arg| if arg.blank? where(nil) else pattern = "%#{arg.to_s.strip.downcase}%" where(" LOWER(title) LIKE :p ", :p => pattern) end } def delete_kindeditor_assets delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::SYLLABUS end # 课程层级 def syllabus_major_level type = "" case self.major_level when 1 type = "研究生" when 2 type = "本科" when 3 type = "专科" end type end def syllabus_type_str type = "" case self.syllabus_type when 1 type = "公共必修课" when 2 type = "学科必修课" when 3 type = "专业选修课" when 4 type = "实践必修课" when 5 type = "实践选修课" end type end # 课程的学科门类 def syllabus_discipline_category self.discipline_category ? self.discipline_category.name : '' end # 课程的一级学科 def syllabus_first_level_discipline self.first_level_discipline ? self.first_level_discipline.name : '' end # 课程的专业 def syllabus_major self.major ? self.major.name : '' 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 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 def self.search(query) __elasticsearch__.search( { query: { multi_match: { query: query, type:"most_fields", operator: "or", fields: ['title'] } }, sort: { _score:{order: "desc" }, updated_at:{order:"desc"} }, highlight: { pre_tags: [''], post_tags: [''], fields: { title: {}, description: {} } } } ) end end