[Rails]报告批改网站


简单来说,就是一个模仿lang8的网站。自己想要了解自己实现的话要花多久。实际上确实花了很久,特别是批改网站核心的批改页面。

代码在这里 https://github.com/xnnyygn/report8

page-report

模型不难

models

围绕Report为核心。上传报告后分解为句子。然后针对句子批改。每次批改会有一个批改号。并在单独表中记录批改操作用于排序等。

句子分解

DELIMITERS = Set.new(%w{。 ! . ?})

def split_text(text)
  sentences = []
  sentence = ''
  text.chars do |c|
    if c == ?\n or c == ?\r
      sentence << ' '
    elsif DELIMITERS.include? c
      sentence << c
      sentences << sentence.strip
      sentence = ''
    else
      sentence << c
    end
  end
  sentence.strip!
  sentences << sentence unless sentence.empty?
  sentences
end

批改页面的编辑器使用tinymce,个人感觉不tiny。编辑器设置(coffeescript)

tinymce.init
    selector: ".sentence-editor",
    theme: 'advanced',
    theme_advanced_resizing : true,
    theme_advancedabuttons1: 'undo,redo,|,strikethrough,forecolor,|,removeformat',
    theme_advanced_text_colors : 'FF0000,0000FF,000000',
    theme_advanced_default_foreground_color : '#FF0000',
    theme_advanced_more_colors : false,
    width: '100%',
    content_css: '/assets/sentence-editor.css'

最后的content_css可能是错误的,实际不能像我这么写。

批改界面上按钮比较多,有修改,撤销,保存,移除,重置等。这几个按钮之间的交互js花了挺长时间的。还有提交的表单是复杂表单,看一下保存处理就明白了。

def save_corrections
  # generate sentence map
  sentence_map = {}
  @report.sentences.each do |s|
    sentence_map[s.id] = s
  end

  # filter corrections
  corrections = []
  params[:corrections].each do |id, c|
    sentence_key = id.to_i
    correction_data = {}
    if c[:modified] == 'true' and sentence_map.include? sentence_key
      correction_data[:sentence] = sentence_map[sentence_key]
      correction_data[:advice] = c[:advice]
      correction_data[:comment] = c[:comment]
      correction_data[:advisor] = @current_user
      corrections << correction_data
    end
  end

  if corrections.any?
    Report.transaction do
      # save correction log
      log = CorrectionLog.new
      log.report = @report
      log.advisor = @current_user
      log.save

      # save corrections
      corrections.each do |c|
        c[:correction_log] = log
        correction = Correction.new(c)
        correction.save
      end

      # update correction count in report
      @report.correction_count = CorrectionLog.where(report: @report).count
      @report.save

      # send mail
      if @report.author != @current_user
        CorrectionNotifier.received(@report.author, @current_user, @report).deliver
      end
    end
  end
  redirect_to @report
end

不过把这个大页面做完之后,剩下的相对就简单多了。比如新增页面,界面语言设置页面(国际化)。

page-write

page-settings

实际上网站还可以发邮件,不过用的是Rails默认的测试邮箱,没发出来。

另外还有个PV功能,使用Redis。相对简单。不过实际网站中被我禁用了。Redis貌似在Rails中不能降级使用?
最后是一张首页图。为啥最后放,因为自己的界面设计实属一般……

page-index