Sha256: 63d12c54ca01181cb2068da831c68c22726546c1a291c51241221c64cc7428bc

Contents?: true

Size: 1.9 KB

Versions: 24

Compression:

Stored size: 1.9 KB

Contents

module RailsConnector
  #
  # This class provides a default controller implementation for comment creation.
  # It should only be used indirectly via a subclass named {CommentsController}.
  #
  # The controller actions only respond to XMLHttpRequests (Ajax).
  class DefaultCommentsController < ApplicationController
    layout nil

    before_filter :ensure_object_is_commentable, :only => :create
    before_filter :ensure_admin, :only => :delete

    #
    # Creates a comment and calls the +after_create+ callback.
    #
    def create
      respond_to do |format|
        format.js do
          @comment = @obj.comments.create(
            if logged_in?
              params[:comment].merge({
                :name => "#{current_user.first_name} #{current_user.last_name}",
                :email => current_user.email
              })
            else
              params[:comment]
            end
          )
          if @comment.errors.empty?
            after_create
            html = render_to_string(:partial => "comments/comment", :locals => {:comment => @comment})
            render :json => {:comment => html}.to_json
          else
            render :json => {:errors => @comment.errors.keys}.to_json
          end
        end
      end
    end

    #
    # Deletes a comment.
    # Allowed only to an admin.
    #
    def delete
      respond_to do |format|
        format.html do
          @comment = Comment.find(params[:id])
          @comment.destroy
          redirect_to :back
        end
      end
    end

    private

    def ensure_object_is_commentable
      @obj = Obj.find(params[:obj_id])
      render :nothing => true unless @obj.allow_comments?
      unless @obj.allow_anonymous_comments? or logged_in?
        render '/errors/403_forbidden', :status => 403
      end
    end

    def ensure_admin
      render("errors/403_forbidden", :status => 403) unless admin?
    end

    def after_create; end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
infopark_rails_connector-6.9.1.3.22208381 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.9.0.3.197272233 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.3.174.51542603 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.3.115.227021242 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.3.73.75172665 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.3.2.153416031 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.3.67.110847540 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.3.1.23895778 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.2.36.82613853 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.2.7.128454143 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.1.26.236500544 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.1 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.539.93842523 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.529.114081979 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.518.30999728 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.515.34928522 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.498.46559598 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.480.261594408 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.444.171626367 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.406.131718077 app/controllers/rails_connector/default_comments_controller.rb