<commit_before>class PostsController < ProductController
  respond_to :html

  def index
    find_product!
    @post = @product.posts.order(created_at: :desc).first

    if @post
      redirect_to product_post_path(@post.product, @post)
    end
  end

  def new
    find_product!
    authenticate_user!
    @post = @product.posts.new(author: current_user)
    authorize! :create, @post
  end

  def create
    find_product!
    authenticate_user!

    @post = @product.posts.new(post_params)
    @post.author = current_user

    authorize! :create, @post

    @post.save

    Subscriber.where(product_id: @product.id).each do |email|
      PostMailer.delay(queue: 'mailer').mailing_list(@post.id, email)
    end

    @product.watchers.each do |watcher|
      PostMailer.delay(queue: 'mailer').created(@post.id, watcher.id)
    end

    Activities::Post.publish!(
      actor: @post.author,
      subject: @post,
      target: @product,
      socket_id: params[:socket_id]
    )

    respond_with @post, location: product_post_path(@post.product, @post)
  end

  def preview
    find_product!
    authenticate_user!
    authorize! :create, Post<commit_msg>check for post save before doing stuff<commit_after>class PostsController < ProductController
  respond_to :html

  def index
    find_product!
    @post = @product.posts.order(created_at: :desc).first

    if @post
      redirect_to product_post_path(@post.product, @post)
    end
  end

  def new
    find_product!
    authenticate_user!
    @post = @product.posts.new(author: current_user)
    authorize! :create, @post
  end

  def create
    find_product!
    authenticate_user!

    @post = @product.posts.new(post_params)
    @post.author = current_user

    authorize! :create, @post

    if @post.save

      Subscriber.where(product_id: @product.id).each do |email|
        PostMailer.delay(queue: 'mailer').mailing_list(@post.id, email)
      end

      @product.watchers.each do |watcher|
        PostMailer.delay(queue: 'mailer').created(@post.id, watcher.id)
      end

      Activities::Post.publish!(
        actor: @post.author,
        subject: @post,
        target: @product,
        socket_id: params[:socket_id]
      )
    end

    respond_with @post, location: product_post_path(@post.product, @post)
  end

  def preview
    find_product!
    authenticate_user!
    authorize! :create, Post
{'fraction_non_alphanumeric': 0.1080168776371308, 'fraction_numerical': 0.0, 'mean_word_length': 3.052991452991453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2745484', 'n_tokens_mistral': 797, 'n_tokens_neox': 764, 'n_words': 163}