Last active
December 25, 2015 18:29
-
-
Save ursm/7020968 to your computer and use it in GitHub Desktop.
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 ActiveModel | |
class MultiCacheSerializer < ArraySerializer | |
# XXX ArraySerializer の実装に依存しまくっている | |
def serializable_array | |
return super unless perform_caching && cache | |
entries = object.each_with_object({}) {|item, memo| | |
if options.has_key?(:each_serializer) | |
serializer = options[:each_serializer] | |
elsif item.respond_to?(:active_model_serializer) | |
serializer = item.active_model_serializer | |
end | |
serializer ||= ActiveModel::DefaultSerializer | |
serializable = serializer.new(item, options.merge(root: nil)) | |
if serializable.respond_to?(:serializable_hash) | |
key = expand_cache_key([serializable.class.to_s.underscore, serializable.cache_key, 'serialize']) | |
val = -> { serializable.serializable_hash } | |
else | |
key = expand_cache_key([serializable.class.to_s.underscore, serializable.cache_key, 'to-json']) | |
val = -> { serializable.as_json } | |
end | |
memo[key] = val | |
} | |
cached = cache.read_multi(*entries.keys) | |
entries.map {|key, val| | |
if cached.has_key?(key) | |
cached[key] | |
else | |
val.call.tap {|data| | |
cache.write key, data | |
} | |
end | |
} | |
end | |
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
begin | |
require 'new_relic/agent/method_tracer' | |
rescue LoadError | |
# do nothing | |
else | |
DependencyDetection.defer do | |
@name = :active_model_serializers | |
depends_on do | |
defined?(ActiveModel::Serializer) && !NewRelic::Control.instance['disable_active_model_serializers'] | |
end | |
executes do | |
NewRelic::Agent.logger.debug 'Installing ActiveModel::Serializers instrumentation' | |
end | |
executes do | |
ActiveModel::Serializer.class_eval do | |
include NewRelic::Agent::MethodTracer | |
add_method_tracer :serializable_hash, 'Serializer/#{self.class.name}/serialize' | |
end | |
end | |
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
+----------------------+-------+-------+---------+---------+-----+-------+ | |
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M | | |
+----------------------+-------+-------+---------+---------+-----+-------+ | |
| Controllers | 676 | 495 | 27 | 71 | 2 | 4 | | |
| Helpers | 2 | 2 | 0 | 0 | 0 | 0 | | |
| Models | 426 | 321 | 23 | 35 | 1 | 7 | | |
| Mailers | 21 | 18 | 1 | 2 | 2 | 7 | | |
| Javascripts | 2362 | 1663 | 0 | 323 | 0 | 3 | | |
| Libraries | 88 | 74 | 4 | 6 | 1 | 10 | | |
| Feature specs | 1322 | 948 | 0 | 2 | 0 | 472 | | |
| Lib specs | 66 | 50 | 0 | 2 | 0 | 23 | | |
| Controller specs | 264 | 191 | 0 | 0 | 0 | 0 | | |
| Cell specs | 439 | 355 | 0 | 0 | 0 | 0 | | |
| Mailer specs | 4 | 3 | 0 | 0 | 0 | 0 | | |
| Model specs | 204 | 151 | 0 | 0 | 0 | 0 | | |
| Serializer specs | 151 | 111 | 0 | 0 | 0 | 0 | | |
+----------------------+-------+-------+---------+---------+-----+-------+ | |
| Total | 6025 | 4382 | 55 | 441 | 8 | 7 | | |
+----------------------+-------+-------+---------+---------+-----+-------+ | |
Code LOC: 2573 Test LOC: 1809 Code to Test Ratio: 1:0.7 |
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
class UserSerializer < ApplicationSerializer | |
attributes :id, :name, :gravatar_id, :status | |
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
class Api::UsersController < Api::ApplicationController | |
def index(ids = nil) | |
@users = User.accessible_by(current_user) | |
@users = @users.where(id: ids) if ids | |
render json: @users | |
end | |
def show(id) | |
render json: User.accessible_by(current_user).find(id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment