basecamp/marginalia

Strategy for using outside Rails controllers like grape?

rromanchuk opened this issue · 1 comments

I love using this gem with pghero, but i'm wondering the best way to make use of customizing my my own component to use outside of a rails controller, in my case it's grape.

I know i can add my own component, and in my case, grape offers env["api.endpoint"] https://github.com/ruby-grape/grape#current-route-and-endpoint but i'm not sure what the best way to pass this information in from what i believe requires a grape middlewear from Grape::Middleware::Base

You can go ahead and close this, just seeking advice, improving SEO for others. Or maybe there is a simpler solution as this has most likely aged.

sj26 commented

You could probably do something similar to the actioncontroller instrumentation:

module ActionControllerInstrumentation
def self.included(instrumented_class)
instrumented_class.class_eval do
if respond_to?(:around_action)
around_action :record_query_comment
else
around_filter :record_query_comment
end
end
end
def record_query_comment
Marginalia::Comment.update!(self)
yield
ensure
Marginalia::Comment.clear!
end
end

def self.update!(controller = nil)
self.marginalia_controller = controller
end

def self.controller
marginalia_controller.controller_name if marginalia_controller.respond_to? :controller_name
end
def self.controller_with_namespace
marginalia_controller.class.name if marginalia_controller
end
def self.action
marginalia_controller.action_name if marginalia_controller.respond_to? :action_name
end

Good luck!