Sha256: aa60f12cb7b3c992738043e86e964bffbdf68a849041691e32e0553cdad2e18f

Contents?: true

Size: 1.81 KB

Versions: 18

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

class CommentsController < BaseController
  before_action :set_article, only: [:create, :preview]

  def create
    options = new_comment_defaults.merge comment_params.to_h
    @comment = @article.add_comment(options)

    unless current_user.nil? || session[:user_id].nil?
      # maybe useless, but who knows ?
      @comment.user_id = current_user.id if current_user.id == session[:user_id]
    end

    remember_author_info_for @comment

    partial = "/articles/comment_failed"
    partial = "/articles/comment" if recaptcha_ok_for?(@comment) && @comment.save
    respond_to do |format|
      format.js { render partial }
      format.html { redirect_to URI.parse(@article.permalink_url).path }
    end
  end

  def preview
    return render plain: "Comments are closed" if @article.comments_closed?

    if comment_params[:body].blank?
      head :ok
      return
    end

    @comment = @article.comments.build(comment_params)
  end

  protected

  def recaptcha_ok_for?(comment)
    use_recaptcha = comment.blog.use_recaptcha
    ((use_recaptcha && verify_recaptcha(model: comment)) || !use_recaptcha)
  end

  def new_comment_defaults
    { ip: request.remote_ip,
      author: "Anonymous",
      user: @current_user,
      user_agent: request.env["HTTP_USER_AGENT"],
      referrer: request.env["HTTP_REFERER"],
      permalink: @article.permalink_url }.stringify_keys
  end

  def remember_author_info_for(comment)
    add_to_cookies(:author, comment.author)
    add_to_cookies(:url, comment.url)
    if comment.email.present?
      add_to_cookies(:gravatar_id, Digest::MD5.hexdigest(comment.email.strip))
    end
  end

  def set_article
    @article = Article.find(params[:article_id])
  end

  def comment_params
    @comment_params ||= params.require(:comment).permit(:body, :author, :email, :url)
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
HornsAndHooves-publify_core-10.5.0 app/controllers/comments_controller.rb
HornsAndHooves-publify_core-10.4.0 app/controllers/comments_controller.rb
HornsAndHooves-publify_core-10.3.0 app/controllers/comments_controller.rb
HornsAndHooves-publify_core-10.2.0 app/controllers/comments_controller.rb
HornsAndHooves-publify_core-10.1.1 app/controllers/comments_controller.rb
HornsAndHooves-publify_core-10.1.0 app/controllers/comments_controller.rb
HornsAndHooves-publify_core-10.0.3 app/controllers/comments_controller.rb
HornsAndHooves-publify_core-10.0.2 app/controllers/comments_controller.rb
HornsAndHooves-publify_core-10.0.1 app/controllers/comments_controller.rb
HornsAndHooves-publify_core-10.0.0 app/controllers/comments_controller.rb
publify_core-9.2.7 app/controllers/comments_controller.rb
publify_core-9.2.6 app/controllers/comments_controller.rb
publify_core-9.2.5 app/controllers/comments_controller.rb
publify_core-9.2.4 app/controllers/comments_controller.rb
publify_core-9.2.3 app/controllers/comments_controller.rb
publify_core-9.2.2 app/controllers/comments_controller.rb
publify_core-9.2.1 app/controllers/comments_controller.rb
publify_core-9.2.0 app/controllers/comments_controller.rb