Sha256: 3b60f85b54988dff5a0319574ef596a19839b2de077eb0edc864fe8ad3522ce7

Contents?: true

Size: 1.9 KB

Versions: 21

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

21 entries across 21 versions & 1 rubygems

Version Path
infopark_rails_connector-6.8.0.210.ed204b0 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.110.6570b45 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.72.d18d096 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.23.da7f96b app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.16.def5e85 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.15.a24f5ff app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.891.647580e app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.889.d503e42 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.883.f5f063b app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.884.4bd86e6 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.869.9609b39 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.856.8c0fec5 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.809.bdfa8c3 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.785.05d4af9 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.744.99f67fc app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.720.44fbabd app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.713.e5c3150 app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.681.7c84f4a app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.663.ceecdee app/controllers/rails_connector/default_comments_controller.rb
infopark_rails_connector-6.8.0.beta.200.650.7c93155 app/controllers/rails_connector/default_comments_controller.rb