Class: RailsPageComment::ControllerResource

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_page_comment/controller_resource.rb

Overview

Handle the load and authorization controller logic so we don’t clutter up all controllers with non-interface methods. This class is used internally, so you do not need to call methods directly on it.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, *args) ⇒ ControllerResource

Returns a new instance of ControllerResource.



22
23
24
25
26
27
# File 'lib/rails_page_comment/controller_resource.rb', line 22

def initialize(controller, *args)
  @controller = controller
  @params = controller.params
  @options = args.extract_options!
  @name = args.first
end

Class Method Details

.add_after_filter(controller_class, method, *args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/rails_page_comment/controller_resource.rb', line 11

def self.add_after_filter(controller_class, method, *args)
  if  RailsPageComment.show_on_page
    options = args.extract_options!
    resource_name = args.first
    before_filter_method = options.delete(:prepend) ? :prepend_after_filter : :after_filter
    controller_class.send(before_filter_method, options.slice(:only, :except, :if, :unless)) do |controller|
      controller.class.page_comment_resource_class.new(controller, resource_name, options.except(:only, :except, :if, :unless)).send(method)
    end
  end
end

Instance Method Details

#page_comment_resourceObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_page_comment/controller_resource.rb', line 29

def page_comment_resource
  if !@controller.request.xhr?
    page_controller = RailsPageComment::PageCommentsController.new
    page_controller.request = @controller.request
    av = ActionView::Base.new(File.join(Gem.loaded_specs['rails_page_comment'].full_gem_path, 'app', 'views', 'rails_page_comment', 'page_comments'), {}, )
    page_uri = [@controller.params[:controller], @controller.params[:action]].join("/")
    page_comment = RailsPageComment::PageComment.find_by(:page_uri => page_uri) || RailsPageComment::PageComment.new(:page_uri => page_uri)
    av.assign({
                  :page_comment => page_comment,
                  :_csrf_token => @controller.session[:_csrf_token]
              })
    if page_comment.new_record?
      html = av.render(:template => "new")
    else
      html = av.render(:template => "edit")
    end

    @controller.response.body = @controller.response.body.gsub('</body>',"#{html}</body>")
  end
end