mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
commit
39d6dab218
@ -1,4 +1,4 @@
|
|||||||
require "set"
|
require 'set'
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class SerializableResource
|
class SerializableResource
|
||||||
|
|
||||||
@ -76,7 +76,9 @@ module ActiveModel
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
ActiveModelSerializers.silence_warnings do
|
||||||
attr_reader :resource, :adapter_opts, :serializer_opts
|
attr_reader :resource, :adapter_opts, :serializer_opts
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -9,6 +9,7 @@ module ActiveModel
|
|||||||
autoload :Adapter
|
autoload :Adapter
|
||||||
autoload :Lint
|
autoload :Lint
|
||||||
autoload :Associations
|
autoload :Associations
|
||||||
|
autoload :Fieldset
|
||||||
include Configuration
|
include Configuration
|
||||||
include Associations
|
include Associations
|
||||||
|
|
||||||
@ -66,10 +67,10 @@ module ActiveModel
|
|||||||
@_attributes_keys[attr] = { key: key } if key != attr
|
@_attributes_keys[attr] = { key: key } if key != attr
|
||||||
@_attributes << key unless @_attributes.include?(key)
|
@_attributes << key unless @_attributes.include?(key)
|
||||||
|
|
||||||
unless respond_to?(key, false) || _fragmented.respond_to?(attr)
|
ActiveModelSerializers.silence_warnings do
|
||||||
define_method key do
|
define_method key do
|
||||||
object.read_attribute_for_serialization(attr)
|
object.read_attribute_for_serialization(attr)
|
||||||
end
|
end unless respond_to?(key, false) || _fragmented.respond_to?(attr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,23 @@
|
|||||||
require 'active_model/serializer/adapter/fragment_cache'
|
|
||||||
|
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class Adapter
|
class Adapter
|
||||||
extend ActiveSupport::Autoload
|
extend ActiveSupport::Autoload
|
||||||
autoload :Json
|
require 'active_model/serializer/adapter/json'
|
||||||
|
require 'active_model/serializer/adapter/json_api'
|
||||||
autoload :FlattenJson
|
autoload :FlattenJson
|
||||||
autoload :Null
|
autoload :Null
|
||||||
autoload :JsonApi
|
autoload :FragmentCache
|
||||||
|
|
||||||
|
def self.create(resource, options = {})
|
||||||
|
override = options.delete(:adapter)
|
||||||
|
klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter
|
||||||
|
klass.new(resource, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.adapter_class(adapter)
|
||||||
|
adapter_name = adapter.to_s.classify.sub("API", "Api")
|
||||||
|
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize
|
||||||
|
end
|
||||||
|
|
||||||
attr_reader :serializer
|
attr_reader :serializer
|
||||||
|
|
||||||
@ -26,17 +36,6 @@ module ActiveModel
|
|||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.create(resource, options = {})
|
|
||||||
override = options.delete(:adapter)
|
|
||||||
klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter
|
|
||||||
klass.new(resource, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.adapter_class(adapter)
|
|
||||||
adapter_name = adapter.to_s.classify.sub("API", "Api")
|
|
||||||
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize
|
|
||||||
end
|
|
||||||
|
|
||||||
def fragment_cache(*args)
|
def fragment_cache(*args)
|
||||||
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
|
raise NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
require 'active_model/serializer/adapter/fragment_cache'
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class Adapter
|
class Adapter
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
require 'active_model/serializer/adapter/fragment_cache'
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class Adapter
|
class Adapter
|
||||||
|
|||||||
@ -73,11 +73,9 @@ module ActiveModel
|
|||||||
def associate(reflection)
|
def associate(reflection)
|
||||||
self._reflections = _reflections.dup
|
self._reflections = _reflections.dup
|
||||||
|
|
||||||
unless method_defined?(reflection.name)
|
define_method reflection.name do
|
||||||
define_method reflection.name do
|
object.send reflection.name
|
||||||
object.send reflection.name
|
end unless method_defined?(reflection.name)
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self._reflections << reflection
|
self._reflections << reflection
|
||||||
end
|
end
|
||||||
|
|||||||
@ -18,7 +18,9 @@ module ActiveModel
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
ActiveModelSerializers.silence_warnings do
|
||||||
attr_reader :raw_fields, :root
|
attr_reader :raw_fields, :root
|
||||||
|
end
|
||||||
|
|
||||||
def parsed_fields
|
def parsed_fields
|
||||||
if raw_fields.is_a?(Hash)
|
if raw_fields.is_a?(Hash)
|
||||||
|
|||||||
@ -1,7 +1,18 @@
|
|||||||
|
module ActiveModelSerializers
|
||||||
|
module_function
|
||||||
|
|
||||||
|
def silence_warnings
|
||||||
|
verbose = $VERBOSE
|
||||||
|
$VERBOSE = nil
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
$VERBOSE = verbose
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
require 'active_model'
|
require 'active_model'
|
||||||
require 'active_model/serializer/version'
|
require 'active_model/serializer/version'
|
||||||
require 'active_model/serializer'
|
require 'active_model/serializer'
|
||||||
require 'active_model/serializer/fieldset'
|
|
||||||
require 'active_model/serializable_resource'
|
require 'active_model/serializable_resource'
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
|||||||
@ -4,8 +4,10 @@ require 'pathname'
|
|||||||
class DefaultScopeNameTest < ActionController::TestCase
|
class DefaultScopeNameTest < ActionController::TestCase
|
||||||
class UserSerializer < ActiveModel::Serializer
|
class UserSerializer < ActiveModel::Serializer
|
||||||
attributes :admin?
|
attributes :admin?
|
||||||
def admin?
|
ActiveModelSerializers.silence_warnings do
|
||||||
current_user.admin
|
def admin?
|
||||||
|
current_user.admin
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -34,8 +36,10 @@ end
|
|||||||
class SerializationScopeNameTest < ActionController::TestCase
|
class SerializationScopeNameTest < ActionController::TestCase
|
||||||
class AdminUserSerializer < ActiveModel::Serializer
|
class AdminUserSerializer < ActiveModel::Serializer
|
||||||
attributes :admin?
|
attributes :admin?
|
||||||
def admin?
|
ActiveModelSerializers.silence_warnings do
|
||||||
current_admin.admin
|
def admin?
|
||||||
|
current_admin.admin
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -129,7 +129,6 @@ module ActionController
|
|||||||
def render_fragment_changed_object_with_relationship
|
def render_fragment_changed_object_with_relationship
|
||||||
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
||||||
comment2 = Comment.new({ id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT' })
|
comment2 = Comment.new({ id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT' })
|
||||||
author = Author.new(id: 1, name: 'Joao Moura.')
|
|
||||||
like = Like.new({ id: 1, likeable: comment, time: 3.days.ago })
|
like = Like.new({ id: 1, likeable: comment, time: 3.days.ago })
|
||||||
|
|
||||||
generate_cached_serializer(like)
|
generate_cached_serializer(like)
|
||||||
@ -215,14 +214,16 @@ module ActionController
|
|||||||
get :render_json_object_without_serializer
|
get :render_json_object_without_serializer
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal ({error: 'Result is Invalid'}).to_json, @response.body
|
expected_body = {error: 'Result is Invalid'}
|
||||||
|
assert_equal expected_body.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_json_array_object_without_serializer
|
def test_render_json_array_object_without_serializer
|
||||||
get :render_json_array_object_without_serializer
|
get :render_json_array_object_without_serializer
|
||||||
|
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal ([{error: 'Result is Invalid'}]).to_json, @response.body
|
expected_body = [{error: 'Result is Invalid'}]
|
||||||
|
assert_equal expected_body.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_array_using_implicit_serializer
|
def test_render_array_using_implicit_serializer
|
||||||
@ -405,9 +406,9 @@ module ActionController
|
|||||||
false
|
false
|
||||||
end
|
end
|
||||||
}.new
|
}.new
|
||||||
assert_match /adapter: false/, (capture(:stderr) {
|
assert_match(/adapter: false/, (capture(:stderr) {
|
||||||
controller.get_serializer(Profile.new)
|
controller.get_serializer(Profile.new)
|
||||||
})
|
}))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance
|
def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance
|
||||||
|
|||||||
@ -11,7 +11,6 @@ module ActiveModel
|
|||||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
@post.comments = [@comment]
|
@post.comments = [@comment]
|
||||||
@anonymous_post.comments = []
|
@anonymous_post.comments = []
|
||||||
@post.author = @author
|
|
||||||
@comment.post = @post
|
@comment.post = @post
|
||||||
@comment.author = nil
|
@comment.author = nil
|
||||||
@anonymous_post.author = nil
|
@anonymous_post.author = nil
|
||||||
|
|||||||
@ -20,7 +20,7 @@ class CaptureWarnings
|
|||||||
|
|
||||||
def after_tests
|
def after_tests
|
||||||
stderr_file.rewind
|
stderr_file.rewind
|
||||||
lines = stderr_file.read.split("\n").uniq
|
lines = stderr_file.read.split("\n")
|
||||||
stderr_file.close!
|
stderr_file.close!
|
||||||
|
|
||||||
$stderr.reopen(STDERR)
|
$stderr.reopen(STDERR)
|
||||||
|
|||||||
9
test/fixtures/poro.rb
vendored
9
test/fixtures/poro.rb
vendored
@ -1,3 +1,5 @@
|
|||||||
|
verbose = $VERBOSE
|
||||||
|
$VERBOSE = nil
|
||||||
class Model
|
class Model
|
||||||
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
|
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
|
||||||
|
|
||||||
@ -260,9 +262,4 @@ Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
|||||||
cache only: [:id]
|
cache only: [:id]
|
||||||
attributes :id
|
attributes :id
|
||||||
end
|
end
|
||||||
|
$VERBOSE = verbose
|
||||||
RaiseErrorSerializer = Class.new(ActiveModel::Serializer) do
|
|
||||||
def json_key
|
|
||||||
raise StandardError, 'Intentional error for rescue_from test'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|||||||
@ -47,9 +47,9 @@ class SerializerGeneratorTest < Rails::Generators::TestCase
|
|||||||
run_generator ["account"]
|
run_generator ["account"]
|
||||||
assert_file "app/serializers/account_serializer.rb" do |content|
|
assert_file "app/serializers/account_serializer.rb" do |content|
|
||||||
if RUBY_PLATFORM =~ /mingw/
|
if RUBY_PLATFORM =~ /mingw/
|
||||||
assert_no_match /\r\n\r\nend/, content
|
assert_no_match(/\r\n\r\nend/, content)
|
||||||
else
|
else
|
||||||
assert_no_match /\n\nend/, content
|
assert_no_match(/\n\nend/, content)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -81,7 +81,7 @@ module ActiveModel
|
|||||||
def test_serializer_options_are_passed_into_associations_serializers
|
def test_serializer_options_are_passed_into_associations_serializers
|
||||||
association = @post_serializer
|
association = @post_serializer
|
||||||
.associations
|
.associations
|
||||||
.detect { |association| association.key == :comments }
|
.detect { |assoc| assoc.key == :comments }
|
||||||
|
|
||||||
assert association.serializer.first.custom_options[:custom_options]
|
assert association.serializer.first.custom_options[:custom_options]
|
||||||
end
|
end
|
||||||
|
|||||||
@ -47,13 +47,13 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_cache_key_interpolation_with_updated_at
|
def test_cache_key_interpolation_with_updated_at
|
||||||
author = render_object_with_cache(@author)
|
render_object_with_cache(@author)
|
||||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
|
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
|
||||||
assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json)
|
assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_default_cache_key_fallback
|
def test_default_cache_key_fallback
|
||||||
comment = render_object_with_cache(@comment)
|
render_object_with_cache(@comment)
|
||||||
assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
|
assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ module ActiveModel
|
|||||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
||||||
|
|
||||||
Timecop.freeze(Time.now) do
|
Timecop.freeze(Time.now) do
|
||||||
post = render_object_with_cache(@post)
|
render_object_with_cache(@post)
|
||||||
|
|
||||||
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
|
||||||
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
|
||||||
@ -122,7 +122,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_uses_file_digest_in_cache_key
|
def test_uses_file_digest_in_cache_key
|
||||||
blog = render_object_with_cache(@blog)
|
render_object_with_cache(@blog)
|
||||||
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
|
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
21
test/support/rails_app.rb
Normal file
21
test/support/rails_app.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
class Foo < Rails::Application
|
||||||
|
if Rails::VERSION::MAJOR >= 4
|
||||||
|
config.eager_load = false
|
||||||
|
config.secret_key_base = 'abc123'
|
||||||
|
config.action_controller.perform_caching = true
|
||||||
|
config.active_support.test_order = :random
|
||||||
|
config.logger = Logger.new(nil)
|
||||||
|
ActionController::Base.cache_store = :memory_store
|
||||||
|
end
|
||||||
|
end
|
||||||
|
Foo.initialize!
|
||||||
|
|
||||||
|
module TestHelper
|
||||||
|
Routes = ActionDispatch::Routing::RouteSet.new
|
||||||
|
Routes.draw do
|
||||||
|
get ':controller(/:action(/:id))'
|
||||||
|
get ':controller(/:action)'
|
||||||
|
end
|
||||||
|
|
||||||
|
ActionController::Base.send :include, Routes.url_helpers
|
||||||
|
end
|
||||||
49
test/support/stream_capture.rb
Normal file
49
test/support/stream_capture.rb
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# Use cleaner stream testing interface from Rails 5 if available
|
||||||
|
# see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb
|
||||||
|
begin
|
||||||
|
require "active_support/testing/stream"
|
||||||
|
rescue LoadError
|
||||||
|
module ActiveSupport
|
||||||
|
module Testing
|
||||||
|
module Stream #:nodoc:
|
||||||
|
private
|
||||||
|
|
||||||
|
def silence_stream(stream)
|
||||||
|
old_stream = stream.dup
|
||||||
|
stream.reopen(IO::NULL)
|
||||||
|
stream.sync = true
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
stream.reopen(old_stream)
|
||||||
|
old_stream.close
|
||||||
|
end
|
||||||
|
|
||||||
|
def quietly
|
||||||
|
silence_stream(STDOUT) do
|
||||||
|
silence_stream(STDERR) do
|
||||||
|
yield
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def capture(stream)
|
||||||
|
stream = stream.to_s
|
||||||
|
captured_stream = Tempfile.new(stream)
|
||||||
|
stream_io = eval("$#{stream}")
|
||||||
|
origin_stream = stream_io.dup
|
||||||
|
stream_io.reopen(captured_stream)
|
||||||
|
|
||||||
|
yield
|
||||||
|
|
||||||
|
stream_io.rewind
|
||||||
|
return captured_stream.read
|
||||||
|
ensure
|
||||||
|
captured_stream.close
|
||||||
|
captured_stream.unlink
|
||||||
|
stream_io.reopen(origin_stream)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
5
test/support/test_case.rb
Normal file
5
test/support/test_case.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
ActionController::TestCase.class_eval do
|
||||||
|
def setup
|
||||||
|
@routes = TestHelper::Routes
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -6,95 +6,34 @@ require 'action_controller'
|
|||||||
require 'action_controller/test_case'
|
require 'action_controller/test_case'
|
||||||
require 'action_controller/railtie'
|
require 'action_controller/railtie'
|
||||||
require 'active_support/json'
|
require 'active_support/json'
|
||||||
require 'minitest/autorun'
|
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
|
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
||||||
|
|
||||||
|
require 'minitest/autorun'
|
||||||
# Ensure backward compatibility with Minitest 4
|
# Ensure backward compatibility with Minitest 4
|
||||||
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
||||||
|
|
||||||
require "capture_warnings"
|
|
||||||
@capture_warnings = CaptureWarnings.new(fail_build = false)
|
require 'capture_warnings'
|
||||||
|
@capture_warnings = CaptureWarnings.new(fail_build = true)
|
||||||
@capture_warnings.before_tests
|
@capture_warnings.before_tests
|
||||||
at_exit do
|
if Minitest.respond_to?(:after_run)
|
||||||
@capture_warnings.after_tests
|
Minitest.after_run do
|
||||||
|
@capture_warnings.after_tests
|
||||||
|
end
|
||||||
|
else
|
||||||
|
at_exit do
|
||||||
|
STDOUT.puts "Minitest.after_run not available."
|
||||||
|
@capture_warnings.after_tests
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'active_model_serializers'
|
require 'active_model_serializers'
|
||||||
|
|
||||||
# Use cleaner stream testing interface from Rails 5 if available
|
require 'support/stream_capture'
|
||||||
# see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb
|
|
||||||
begin
|
|
||||||
require "active_support/testing/stream"
|
|
||||||
rescue LoadError
|
|
||||||
module ActiveSupport
|
|
||||||
module Testing
|
|
||||||
module Stream #:nodoc:
|
|
||||||
private
|
|
||||||
|
|
||||||
def silence_stream(stream)
|
require 'support/rails_app'
|
||||||
old_stream = stream.dup
|
|
||||||
stream.reopen(IO::NULL)
|
|
||||||
stream.sync = true
|
|
||||||
yield
|
|
||||||
ensure
|
|
||||||
stream.reopen(old_stream)
|
|
||||||
old_stream.close
|
|
||||||
end
|
|
||||||
|
|
||||||
def quietly
|
|
||||||
silence_stream(STDOUT) do
|
|
||||||
silence_stream(STDERR) do
|
|
||||||
yield
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def capture(stream)
|
|
||||||
stream = stream.to_s
|
|
||||||
captured_stream = Tempfile.new(stream)
|
|
||||||
stream_io = eval("$#{stream}")
|
|
||||||
origin_stream = stream_io.dup
|
|
||||||
stream_io.reopen(captured_stream)
|
|
||||||
|
|
||||||
yield
|
|
||||||
|
|
||||||
stream_io.rewind
|
|
||||||
return captured_stream.read
|
|
||||||
ensure
|
|
||||||
captured_stream.close
|
|
||||||
captured_stream.unlink
|
|
||||||
stream_io.reopen(origin_stream)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Foo < Rails::Application
|
|
||||||
if Rails::VERSION::MAJOR >= 4
|
|
||||||
config.eager_load = false
|
|
||||||
config.secret_key_base = 'abc123'
|
|
||||||
config.action_controller.perform_caching = true
|
|
||||||
config.active_support.test_order = :random
|
|
||||||
config.logger = Logger.new(nil)
|
|
||||||
ActionController::Base.cache_store = :memory_store
|
|
||||||
end
|
|
||||||
end
|
|
||||||
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
|
||||||
Foo.initialize!
|
|
||||||
|
|
||||||
require 'fixtures/poro'
|
require 'fixtures/poro'
|
||||||
|
|
||||||
module TestHelper
|
require 'support/test_case'
|
||||||
Routes = ActionDispatch::Routing::RouteSet.new
|
|
||||||
Routes.draw do
|
|
||||||
get ':controller(/:action(/:id))'
|
|
||||||
get ':controller(/:action)'
|
|
||||||
end
|
|
||||||
|
|
||||||
ActionController::Base.send :include, Routes.url_helpers
|
|
||||||
end
|
|
||||||
|
|
||||||
ActionController::TestCase.class_eval do
|
|
||||||
def setup
|
|
||||||
@routes = TestHelper::Routes
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user