如何让 Paper_trail 使用 Action Text?

How to get Paper_trail to work with Action Text?

我的应用程序 Paper Trail Gem nicely set up with my basic model Article which had a text column called body. However, after I have implemented Action Text 并从 Article 模型中删除了列 body我无法让 Paper Trail 跟踪相关 body 专栏。我怎样才能让它工作?

免责声明:我是 Rails 的新手。

Article.rb

...
  has_rich_text :body
  has_paper_trail
...

文章架构(删除 :body 列后)

  create_table "articles", force: :cascade do |t|
    t.string "title"
    t.string "slug"
    t.datetime "archived_at"
    t.datetime "published_at"
    ...
  end

动作文本架构

create_table "action_text_rich_texts", force: :cascade do |t|
    t.string "name", null: false
    t.text "body"
    t.string "record_type", null: false
    t.bigint "record_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["record_type", "record_id", "name"], name: "index_action_text_rich_texts_uniqueness", unique: true
  end

我希望 return 应用程序具有与以前相同的功能,我能够看到文章正文中所做的更改。例如。有人加了一句,删了一个字等等

Ruby 允许您(无论好坏)即时修改内容。

将其放入初始化程序中:

ActionText::RichText.class_eval do 
  has_paper_trail
end 

尝试添加 config/initializers/rich_text_paper_trail.rb 文件:

ActiveSupport.on_load(:action_text_rich_text) do
  has_paper_trail
end

在尝试了此处提供的所有选项后,我想到了这个解决方法,最终效果非常好。

我所做的是 我只是用纯 Trix Editor 版本 替换了动作文本,因此我能够将 :body 保留在 [=18] =] 模型,保存整个对象的版本并显示差异。耶!

结合对我有用的答案:

# frozen_string_literal: true

ActiveSupport.on_load(:action_text_rich_text) do
  ActionText::RichText.class_eval do
    has_paper_trail
  end
end

in lib/rich_text_paper_trail.rb 并确保此文件已加载!例如通过明确要求它:require 'rich_text_paper_trail'

我们在 Rails 5.2 上使用活动文本的存档版本做了类似的事情。

宝石文件

gem 'actiontext', git: 'https://github.com/kobaltz/actiontext.git', branch: 'archive', require: 'action_text'

models/article.rb

class Article < ApplicationRecord
  has_paper_tail
  serialize :body, ActionText::Content
  ...
end

helpers/trix_editor_helper.rb

require 'action_view/helpers/tags/placeholderable'

module TrixEditorHelper
  mattr_accessor(:id, instance_accessor: false) { 0 }

# Returns a +trix-editor+ tag that instantiates the Trix JavaScript editor as well as a hidden field
# that Trix will write to on changes, so the content will be sent on form submissions.
#
# ==== Options
# * <tt>:class</tt> - Defaults to "trix-content" which ensures default styling is applied.
#
# ==== Example
#
#   rich_text_area_tag "content", message.content
#   # <input type="hidden" name="content" id="trix_input_post_1">
#   # <trix-editor id="content" input="trix_input_post_1" class="trix-content" ...></trix-editor>

  def trix_editor_tag(name, value = nil, options = {})
    options = options.symbolize_keys

    options[:input] ||= "trix_input_#{TrixEditorHelper.id += 1}"
    options[:class] ||= "trix-content"

    options[:data] ||= {}
    options[:data][:direct_upload_url] = main_app.rails_direct_uploads_url
    options[:data][:blob_url_template] = main_app.rails_service_blob_url(":signed_id", ":filename")

    editor_tag = content_tag("trix-editor", "", options)
    input_tag = hidden_field_tag(name, value, id: options[:input])

    input_tag + editor_tag
  end
end

module ActionView::Helpers
  class Tags::TrixEditor < Tags::Base
    include Tags::Placeholderable
    delegate :dom_id, to: ActionView::RecordIdentifier

    def render
      options = @options.stringify_keys
      add_default_name_and_id(options)
      options['input'] ||= dom_id(object, [options['id'], :trix_input].compact.join('_')) if object
      @template_object.trix_editor_tag(options.delete("name"), editable_value, options)
    end

    def editable_value
      value&.try(:to_trix_html)
    end
  end

  module FormHelper
    def trix_editor(object_name, method, options = {})
      Tags::TrixEditor.new(object_name, method, self, options).render
    end
  end

  class FormBuilder
    def trix_editor(method, options = {})
      @template.trix_editor(@object_name, method, objectify_options(options))
    end
  end
end

然后我们从 cdn 安装了 trix 1.1 版并使用了标准的 trix-attachments.js 和直接上传。

希望升级到 Rails 6 并尽可能保留此概念。