<commit_before>module Storytime
  module Concerns
    module HasVersions
      extend ActiveSupport::Concern
      
      # TO USE IN MODEL:
      # include Storytime::Concerns::HasVersions
      # self.draft_content_column = :html (defaults to :content)
      # in controller, add @post.draft_user_id = current_user.id to #create and #update

      def draft_content
        @draft_content || (self.latest_version.present? ? self.latest_version.content : "")
      end

      def latest_version
        @latest_version ||= self.versions.order(updated_at: :desc).first
      end

      def create_version
        if self.latest_version.nil? || self.draft_content != self.latest_version.content
          version = self.versions.new
          version.content = self.draft_content
          version.user_id = self.draft_user_id
          version.save
        end
        self.publish! if self.published?
      end

      def activate_version
        if @draft_version_id
          version = Storytime::Version.find(@draft_version_id)
          if self.update_columns(content: version.content)
            version.touch
          end
        end
      end

      def publish!
        self.update_columns(self.class.draft_content_column => self.latest_version.content, :published => true)
      end

      included do
        has_many :versions, as: :versionable, dependent: :destroy
        cattr_accessor :draft_content_column
        attr_accessor :draft_user_id
        attr_writer :draft_content, :draft_version_id
        after_save :create_version, :activate_version

        self.draft_content_column = :content

        scope :published, -> { where(published: true) }
      end

      module ClassMethods
        
      end
    end
  end
end<commit_msg>use draft_content_column in activate_version method<commit_after>module Storytime
  module Concerns
    module HasVersions
      extend ActiveSupport::Concern
      
      # TO USE IN MODEL:
      # include Storytime::Concerns::HasVersions
      # self.draft_content_column = :html (defaults to :content)
      # in controller, add @post.draft_user_id = current_user.id to #create and #update

      def draft_content
        @draft_content || (self.latest_version.present? ? self.latest_version.content : "")
      end

      def latest_version
        @latest_version ||= self.versions.order(updated_at: :desc).first
      end

      def create_version
        if self.latest_version.nil? || self.draft_content != self.latest_version.content
          version = self.versions.new
          version.content = self.draft_content
          version.user_id = self.draft_user_id
          version.save
        end
        self.publish! if self.published?
      end

      def activate_version
        if @draft_version_id
          version = Storytime::Version.find(@draft_version_id)
          if self.update_columns(self.class.draft_content_column => version.content)
            version.touch
          end
        end
      end

      def publish!
        self.update_columns(self.class.draft_content_column => self.latest_version.content, :published => true)
      end

      included do
        has_many :versions, as: :versionable, dependent: :destroy
        cattr_accessor :draft_content_column
        attr_accessor :draft_user_id
        attr_writer :draft_content, :draft_version_id
        after_save :create_version, :activate_version

        self.draft_content_column = :content

        scope :published, -> { where(published: true) }
      end

      module ClassMethods
        
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07402234636871509, 'fraction_numerical': 0.0, 'mean_word_length': 2.4235181644359463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4317594', 'n_tokens_mistral': 1007, 'n_tokens_neox': 980, 'n_words': 221}