Created
January 28, 2015 16:35
-
-
Save larryzhao/2b32a83dff99d24af62f to your computer and use it in GitHub Desktop.
Newrelic Support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
... | |
def present(object, klass = nil) | |
klass ||= "#{object.class}Presenter".constantize | |
presenter = klass.new(object, self) | |
yield presenter if block_given? | |
presenter | |
end | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'new_relic/agent/method_tracer' | |
class BasePresenter | |
attr_reader :object, :template | |
def initialize(object, template) | |
@object = object | |
@template = template | |
end | |
def self.presents(name) | |
define_method(name) { object } | |
end | |
private | |
def method_missing(method_name, *args, &block) | |
template.send(method_name, *args, &block) | |
end | |
def respond_to_missing?(method_name, include_private = false) | |
template.respond_to?(method_name, include_private) || super | |
end | |
add_method_tracer :initialize, 'BasePresenter/initialize' | |
add_method_tracer :method_missing, 'BasePresenter/method_missing' | |
add_method_tracer :respond_to_missing?, 'BasePresenter/respond_to_missing?' | |
class << self | |
include ::NewRelic::Agent::MethodTracer | |
add_method_tracer :presents, 'BasePresenter/presents' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'new_relic/agent/method_tracer' | |
class NotePresenter < BasePresenter | |
presents :note | |
def recommend_button | |
if user_signed_in? && current_user.is_editor? | |
link_to "<i class='icon-plus'></i>", [:editor, note], class: "btn", target: "_blank" | |
end | |
end | |
def pick_daily_button | |
if user_signed_in? && [3, 16424, 24784].include?(current_user.id) | |
link_to "<i class='icon-cloud-upload'></i>", new_editor_pick_daily_path(note_id: note.id), class: "btn", target: "_blank" | |
end | |
end | |
def edit_button | |
if user_signed_in? && note.authored_by?(current_user) | |
link_to "<i class='icon-edit'></i>", note_edit_path(note), class: "btn" | |
end | |
end | |
def add_collection_button | |
if user_signed_in? && (note.authored_by?(current_user) || current_user.is_a_collection_editor?) | |
"<a href='javascript:void(null)' class='btn add-collection' data-toggle='modal' data-target='#contribute-modal'><i class='icon-plus'></i><span>#{t('.add_to_collection')}</span></a>".html_safe | |
end | |
end | |
def bookmark_button | |
render "notes/bookmark_button", note: note if user_signed_in? | |
end | |
add_method_tracer :recommend_button, 'NotePresenter/recommend_button' | |
add_method_tracer :pick_daily_button, 'NotePresenter/pick_daily_button' | |
add_method_tracer :edit_button, 'NotePresenter/edit_button' | |
add_method_tracer :add_collection_button, 'NotePresenter/add_collection_button' | |
add_method_tracer :bookmark_button, 'NotePresenter/bookmark_button' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% present @note do |presenter| %> | |
<div class="post-bg" id="flag"> | |
<div class="wrap-btn"> | |
<div class="btn-group editor-article"> | |
<span> | |
<%= presenter.recommend_button %> | |
<%= presenter.pick_daily_button %> | |
<%= presenter.edit_button %> | |
<%= presenter.add_collection_button %> | |
<%= presenter.bookmark_button %> | |
</span> | |
</div> | |
</div> | |
</div> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment