class PostRevisionSerializer < ApplicationSerializer attributes :created_at, :post_id, # which revision is hidden :previous_hidden, :current_hidden, # dynamic & based on the current scope :first_revision, :previous_revision, :current_revision, :next_revision, :last_revision, # used for display :current_version, :version_count, # from the user :username, :display_username, :avatar_template, # all the changes :edit_reason, :body_changes, :title_changes, :user_changes # Creates a field called field_name_changes with previous and # current members if a field has changed in this revision def self.add_compared_field(field) changes_name = "#{field}_changes".to_sym self.attributes changes_name define_method(changes_name) do { previous: previous[field], current: current[field] } end define_method("include_#{changes_name}?") do previous[field] != current[field] end end add_compared_field :category_id add_compared_field :wiki add_compared_field :post_type def previous_hidden previous["hidden"] end def current_hidden current["hidden"] end def first_revision revisions.first["revision"] end def previous_revision @previous_revision ||= revisions.select { |r| r["revision"] >= first_revision } .select { |r| r["revision"] < current_revision } .last.try(:[], "revision") end def current_revision object.number end def next_revision @next_revision ||= revisions.select { |r| r["revision"] <= last_revision } .select { |r| r["revision"] > current_revision } .first.try(:[], "revision") end def last_revision @last_revision ||= revisions.select { |r| r["revision"] <= post.version }.last["revision"] end def current_version @current_version ||= revisions.select { |r| r["revision"] <= current_revision }.count + 1 end def version_count revisions.count end def username user.username_lower end def display_username user.username end def avatar_template user.avatar_template end def edit_reason # only show 'edit_reason' when revisions are consecutive current["edit_reason"] if scope.can_view_hidden_post_revisions? || current["revision"] == previous["revision"] + 1 end def body_changes cooked_diff = DiscourseDiff.new(previous["cooked"], current["cooked"]) raw_diff = DiscourseDiff.new(previous["raw"], current["raw"]) { inline: cooked_diff.inline_html, side_by_side: cooked_diff.side_by_side_html, side_by_side_markdown: raw_diff.side_by_side_markdown } end def title_changes prev = "