Skip to content

Commit

Permalink
Merge a783bd8 into 2e31a14
Browse files Browse the repository at this point in the history
  • Loading branch information
zlx committed Apr 16, 2014
2 parents 2e31a14 + a783bd8 commit 6b4e320
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions active_model_serializers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ Gem::Specification.new do |gem|
gem.required_ruby_version = ">= 1.9.3"

gem.add_dependency "activemodel", ">= 3.2"
gem.add_dependency "activesupport", ">= 3.2"
gem.add_development_dependency "rails", ">= 3.2"
end
46 changes: 44 additions & 2 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ module ActiveModel
class Serializer
include Serializable

class_attribute :cache
class_attribute :perform_caching

@mutex = Mutex.new

class << self
def inherited(base)
base._root = _root
base.perform_caching = false
base._attributes = (_attributes || []).dup
base._associations = (_associations || {}).dup
end

def cached(value = true)
self.perform_caching = value
end

def setup
@mutex.synchronize do
Expand Down Expand Up @@ -122,11 +130,45 @@ def json_key
end

def attributes
filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
hash[name] = send(name)
if perform_cached?
cache_key = expand_cache_key([self.class.to_s.underscore, cache_key, 'attributes-json', digest])
p "Fetch Cache: #{cache_key}" if cache.read(cache_key)
cache.fetch cache_key do
p "Write Cache: #{cache_key}"
filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
hash[name] = send(name)
end
end
else
filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
hash[name] = send(name)
end
end
end

def perform_cached?
perform_caching && cache && respond_to?(:cache_key)
end

def expand_cache_key(*args)
ActiveSupport::Cache.expand_cache_key(args)
end

def digest
class_source = File.read(default_file_path) rescue File.read(find_file_path)
Digest::MD5.hexdigest(class_source)
end

def default_file_path
Rails.root.join("app/serializers", "#{self.class.to_s.tableize.singularize}.rb").to_s
end

def find_file_path
self.class.instance_methods(false).map { |m|
self.class.instance_method(m).source_location.first
}.select{|f| f.match(Rails.root.to_s) }.last
end

def associations
associations = self.class._associations
included_associations = filter(associations.keys)
Expand Down
4 changes: 4 additions & 0 deletions lib/active_model/serializer/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ class Railtie < Rails::Railtie
require 'active_model/serializer/generators/serializer/scaffold_controller_generator'
require 'active_model/serializer/generators/resource_override'
end

initializer "caching.active_model_serializer" do |app|
ActiveModel::Serializer.cache = Rails.cache
end
end
end
4 changes: 3 additions & 1 deletion lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'active_model'
require 'active_support'
require 'active_support/core_ext'
require 'active_model/serializer'
require 'active_model/serializer_support'
require 'active_model/serializer/version'
Expand All @@ -7,7 +9,7 @@
begin
require 'action_controller'
require 'action_controller/serialization'

ActiveSupport.on_load(:action_controller) do
include ::ActionController::Serialization
end
Expand Down

0 comments on commit 6b4e320

Please sign in to comment.