draper 4.0.2 → 4.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +41 -25
- data/.rspec +1 -2
- data/CHANGELOG.md +18 -0
- data/Gemfile +27 -5
- data/README.md +23 -1
- data/draper.gemspec +3 -3
- data/lib/draper/automatic_delegation.rb +14 -4
- data/lib/draper/decoratable/collection_proxy.rb +15 -0
- data/lib/draper/decoratable.rb +2 -2
- data/lib/draper/query_methods.rb +1 -1
- data/lib/draper/railtie.rb +8 -4
- data/lib/draper/version.rb +1 -1
- data/lib/draper/view_context/build_strategy.rb +1 -9
- data/lib/draper.rb +0 -6
- data/spec/draper/decoratable_spec.rb +1 -1
- data/spec/draper/decorator_spec.rb +2 -2
- data/spec/draper/factory_spec.rb +8 -8
- data/spec/draper/query_methods_spec.rb +10 -0
- data/spec/draper/view_context/build_strategy_spec.rb +1 -18
- data/spec/dummy/.rspec +0 -1
- data/spec/dummy/app/decorators/comment_decorator.rb +13 -0
- data/spec/dummy/app/decorators/mongoid_post_decorator.rb +1 -3
- data/spec/dummy/app/models/admin.rb +2 -4
- data/spec/dummy/app/models/comment.rb +3 -0
- data/spec/dummy/app/models/mongoid_post.rb +2 -4
- data/spec/dummy/app/models/post.rb +4 -0
- data/spec/dummy/app/models/user.rb +2 -4
- data/spec/dummy/config/application.rb +22 -51
- data/spec/dummy/config/environments/development.rb +65 -22
- data/spec/dummy/config/environments/production.rb +76 -33
- data/spec/dummy/config/environments/test.rb +55 -21
- data/spec/dummy/config/initializers/draper.rb +4 -2
- data/spec/dummy/db/migrate/20240907041839_create_comments.rb +9 -0
- data/spec/dummy/db/schema.rb +16 -9
- data/spec/dummy/spec/decorators/active_model_serializers_spec.rb +1 -1
- data/spec/dummy/spec/decorators/post_decorator_spec.rb +1 -1
- data/spec/dummy/spec/jobs/publish_post_job_spec.rb +2 -0
- data/spec/dummy/spec/models/post_spec.rb +41 -5
- data/spec/dummy/spec/rails_helper.rb +69 -0
- data/spec/dummy/spec/spec_helper.rb +90 -5
- data/spec/performance/benchmark.rb +1 -1
- data/spec/support/dummy_app.rb +1 -1
- metadata +22 -13
@@ -1,63 +1,34 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
def attempt_require(file)
|
4
|
-
require file
|
5
|
-
rescue LoadError
|
6
|
-
end
|
1
|
+
require_relative 'boot'
|
7
2
|
|
8
3
|
require 'rails/all'
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
|
5
|
+
# Require the gems listed in Gemfile, including any gems
|
6
|
+
# you've limited to :test, :development, or :production.
|
7
|
+
Kernel.silence_warnings do
|
8
|
+
Bundler.require(*Rails.groups)
|
9
|
+
end
|
13
10
|
|
14
11
|
module Dummy
|
15
12
|
class Application < Rails::Application
|
16
|
-
|
17
|
-
# Application configuration should go into files in config/initializers
|
18
|
-
# -- all .rb files in that directory are automatically loaded.
|
19
|
-
|
20
|
-
# Custom directories with classes and modules you want to be autoloadable.
|
21
|
-
# config.autoload_paths += %W(#{config.root}/extras)
|
22
|
-
|
23
|
-
# Only load the plugins named here, in the order given (default is alphabetical).
|
24
|
-
# :all can be used as a placeholder for all plugins not explicitly named.
|
25
|
-
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
26
|
-
|
27
|
-
# Activate observers that should always be running.
|
28
|
-
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
29
|
-
|
30
|
-
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
31
|
-
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
32
|
-
# config.time_zone = 'Central Time (US & Canada)'
|
33
|
-
|
34
|
-
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
35
|
-
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
36
|
-
# config.i18n.default_locale = :de
|
37
|
-
|
38
|
-
# Configure the default encoding used in templates for Ruby 1.9.
|
39
|
-
config.encoding = "utf-8"
|
40
|
-
|
41
|
-
# Enable escaping HTML in JSON.
|
42
|
-
config.active_support.escape_html_entities_in_json = true
|
43
|
-
|
44
|
-
# Use SQL instead of Active Record's schema dumper when creating the database.
|
45
|
-
# This is necessary if your schema can't be completely dumped by the schema dumper,
|
46
|
-
# like if you have constraints or database-specific column types
|
47
|
-
# config.active_record.schema_format = :sql
|
13
|
+
config.load_defaults Rails::VERSION::STRING.to_f
|
48
14
|
|
49
|
-
#
|
50
|
-
#
|
51
|
-
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
|
52
|
-
# parameters by using an attr_accessible or attr_protected declaration.
|
53
|
-
# config.active_record.whitelist_attributes = true
|
15
|
+
# For compatibility with applications that use this config
|
16
|
+
# config.action_controller.include_all_helpers = false # FIXME
|
54
17
|
|
55
|
-
#
|
56
|
-
#
|
18
|
+
# Please, add to the `ignore` list any other `lib` subdirectories that do
|
19
|
+
# not contain `.rb` files, or that should not be reloaded or eager loaded.
|
20
|
+
# Common ones are `templates`, `generators`, or `middleware`, for example.
|
21
|
+
config.try :autoload_lib, ignore: %w[assets tasks] # HACK for Rails below 7
|
57
22
|
|
58
|
-
#
|
59
|
-
#
|
23
|
+
# Configuration for the application, engines, and railties goes here.
|
24
|
+
#
|
25
|
+
# These settings can be overridden in specific environments using the files
|
26
|
+
# in config/environments, which are processed later.
|
27
|
+
#
|
28
|
+
# config.time_zone = "Central Time (US & Canada)"
|
29
|
+
# config.eager_load_paths << Rails.root.join("extras")
|
60
30
|
|
31
|
+
# HACK: allows testing in production & development environments
|
61
32
|
# Tell Action Mailer not to deliver emails to the real world.
|
62
33
|
# The :test delivery method accumulates sent emails in the
|
63
34
|
# ActionMailer::Base.deliveries array.
|
@@ -1,35 +1,78 @@
|
|
1
|
-
|
2
|
-
# Settings specified here will take precedence over those in config/application.rb
|
1
|
+
require "active_support/core_ext/integer/time"
|
3
2
|
|
4
|
-
|
5
|
-
#
|
3
|
+
Rails.application.configure do
|
4
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
5
|
+
|
6
|
+
# In the development environment your application's code is reloaded any time
|
7
|
+
# it changes. This slows down response time but is perfect for development
|
6
8
|
# since you don't have to restart the web server when you make code changes.
|
7
|
-
config.
|
9
|
+
config.enable_reloading = true
|
10
|
+
|
11
|
+
# Do not eager load code on boot.
|
12
|
+
config.eager_load = false
|
13
|
+
|
14
|
+
# Show full error reports.
|
15
|
+
config.consider_all_requests_local = true
|
16
|
+
|
17
|
+
# Enable server timing.
|
18
|
+
config.server_timing = true
|
8
19
|
|
9
|
-
#
|
10
|
-
|
11
|
-
|
20
|
+
# Enable/disable caching. By default caching is disabled.
|
21
|
+
# Run rails dev:cache to toggle caching.
|
22
|
+
if Rails.root.join("tmp/caching-dev.txt").exist?
|
23
|
+
config.action_controller.perform_caching = true
|
24
|
+
config.action_controller.enable_fragment_cache_logging = true
|
25
|
+
|
26
|
+
config.cache_store = :memory_store
|
27
|
+
config.public_file_server.headers = { "Cache-Control" => "public, max-age=#{2.days.to_i}" }
|
28
|
+
else
|
29
|
+
config.action_controller.perform_caching = false
|
30
|
+
|
31
|
+
config.cache_store = :null_store
|
32
|
+
end
|
33
|
+
|
34
|
+
# Store uploaded files on the local file system (see config/storage.yml for options).
|
35
|
+
config.active_storage.service = :local
|
12
36
|
|
13
|
-
#
|
37
|
+
# Don't care if the mailer can't send.
|
38
|
+
config.action_mailer.raise_delivery_errors = false
|
39
|
+
|
40
|
+
# Disable caching for Action Mailer templates even if Action Controller
|
41
|
+
# caching is enabled.
|
42
|
+
config.action_mailer.perform_caching = false
|
43
|
+
|
44
|
+
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
|
45
|
+
|
46
|
+
# Print deprecation notices to the Rails logger.
|
14
47
|
config.active_support.deprecation = :log
|
15
48
|
|
16
|
-
#
|
17
|
-
config.
|
49
|
+
# Raise exceptions for disallowed deprecations.
|
50
|
+
config.active_support.disallowed_deprecation = :raise
|
18
51
|
|
19
|
-
|
52
|
+
# Tell Active Support which deprecation messages to disallow.
|
53
|
+
config.active_support.disallowed_deprecation_warnings = []
|
20
54
|
|
21
|
-
# Raise
|
22
|
-
|
55
|
+
# Raise an error on page load if there are pending migrations.
|
56
|
+
config.active_record.migration_error = :page_load
|
23
57
|
|
24
|
-
#
|
25
|
-
|
26
|
-
# config.active_record.auto_explain_threshold_in_seconds = 0.5
|
58
|
+
# Highlight code that triggered database queries in logs.
|
59
|
+
config.active_record.verbose_query_logs = true
|
27
60
|
|
28
|
-
#
|
29
|
-
# config.
|
61
|
+
# Highlight code that enqueued background job in logs.
|
62
|
+
# config.active_job.verbose_enqueue_logs = true
|
30
63
|
|
31
|
-
#
|
32
|
-
# config.assets.
|
64
|
+
# Suppress logger output for asset requests.
|
65
|
+
# config.assets.quiet = true
|
33
66
|
|
34
|
-
|
67
|
+
# Raises error for missing translations.
|
68
|
+
# config.i18n.raise_on_missing_translations = true
|
69
|
+
|
70
|
+
# Annotate rendered view with file names.
|
71
|
+
# config.action_view.annotate_rendered_view_with_filenames = true
|
72
|
+
|
73
|
+
# Uncomment if you wish to allow Action Cable access from any origin.
|
74
|
+
# config.action_cable.disable_request_forgery_protection = true
|
75
|
+
|
76
|
+
# Raise error when a before_action's only/except options reference missing actions.
|
77
|
+
# config.action_controller.raise_on_missing_callback_actions = true
|
35
78
|
end
|
@@ -1,59 +1,102 @@
|
|
1
|
-
|
2
|
-
# Settings specified here will take precedence over those in config/application.rb
|
1
|
+
require "active_support/core_ext/integer/time"
|
3
2
|
|
4
|
-
|
5
|
-
config.
|
3
|
+
Rails.application.configure do
|
4
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
6
5
|
|
7
|
-
#
|
6
|
+
# Code is not reloaded between requests.
|
7
|
+
config.enable_reloading = false
|
8
|
+
|
9
|
+
# Eager load code on boot. This eager loads most of Rails and
|
10
|
+
# your application in memory, allowing both threaded web servers
|
11
|
+
# and those relying on copy on write to perform better.
|
12
|
+
# Rake tasks automatically ignore this option for performance.
|
13
|
+
config.eager_load = true
|
14
|
+
|
15
|
+
# Full error reports are disabled and caching is turned on.
|
8
16
|
config.consider_all_requests_local = false
|
9
17
|
config.action_controller.perform_caching = true
|
10
18
|
|
11
|
-
#
|
12
|
-
config.
|
19
|
+
# Ensures that a master key has been made available in ENV["RAILS_MASTER_KEY"], config/master.key, or an environment
|
20
|
+
# key such as config/credentials/production.key. This key is used to decrypt credentials (and other encrypted files).
|
21
|
+
# config.require_master_key = true
|
13
22
|
|
14
|
-
|
23
|
+
# Disable serving static files from `public/`, relying on NGINX/Apache to do so instead.
|
24
|
+
# config.public_file_server.enabled = false
|
25
|
+
|
26
|
+
# Compress CSS using a preprocessor.
|
27
|
+
# config.assets.css_compressor = :sass
|
28
|
+
|
29
|
+
# Do not fall back to assets pipeline if a precompiled asset is missed.
|
30
|
+
# config.assets.compile = false
|
15
31
|
|
16
|
-
#
|
17
|
-
# config.
|
32
|
+
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
|
33
|
+
# config.asset_host = "http://assets.example.com"
|
18
34
|
|
19
|
-
# Specifies the header that your server uses for sending files
|
20
|
-
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for
|
21
|
-
# config.action_dispatch.x_sendfile_header =
|
35
|
+
# Specifies the header that your server uses for sending files.
|
36
|
+
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache
|
37
|
+
# config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX
|
38
|
+
|
39
|
+
# Store uploaded files on the local file system (see config/storage.yml for options).
|
40
|
+
config.active_storage.service = :local
|
41
|
+
|
42
|
+
# Mount Action Cable outside main process or domain.
|
43
|
+
# config.action_cable.mount_path = nil
|
44
|
+
# config.action_cable.url = "wss://example.com/cable"
|
45
|
+
# config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ]
|
46
|
+
|
47
|
+
# Assume all access to the app is happening through a SSL-terminating reverse proxy.
|
48
|
+
# Can be used together with config.force_ssl for Strict-Transport-Security and secure cookies.
|
49
|
+
# config.assume_ssl = true
|
22
50
|
|
23
51
|
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
|
24
52
|
# config.force_ssl = true
|
25
53
|
|
26
|
-
#
|
27
|
-
# config.
|
54
|
+
# Skip http-to-https redirect for the default health check endpoint.
|
55
|
+
# config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } }
|
56
|
+
|
57
|
+
# Log to STDOUT by default
|
58
|
+
config.logger = ActiveSupport::Logger.new(STDOUT)
|
59
|
+
.tap { |logger| logger.formatter = ::Logger::Formatter.new }
|
60
|
+
.tap { |logger| break ActiveSupport::TaggedLogging.new logger }
|
28
61
|
|
29
|
-
# Prepend all log lines with the following tags
|
30
|
-
|
62
|
+
# Prepend all log lines with the following tags.
|
63
|
+
config.log_tags = [:request_id]
|
31
64
|
|
32
|
-
#
|
33
|
-
#
|
65
|
+
# "info" includes generic and useful information about system operation, but avoids logging too much
|
66
|
+
# information to avoid inadvertent exposure of personally identifiable information (PII). If you
|
67
|
+
# want to log everything, set the level to "debug".
|
68
|
+
config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")
|
34
69
|
|
35
|
-
# Use a different cache store in production
|
70
|
+
# Use a different cache store in production.
|
36
71
|
# config.cache_store = :mem_cache_store
|
37
72
|
|
38
|
-
#
|
39
|
-
# config.
|
73
|
+
# Use a real queuing backend for Active Job (and separate queues per environment).
|
74
|
+
# config.active_job.queue_adapter = :resque
|
75
|
+
# config.active_job.queue_name_prefix = "dummy_production"
|
40
76
|
|
41
|
-
#
|
42
|
-
#
|
77
|
+
# Disable caching for Action Mailer templates even if Action Controller
|
78
|
+
# caching is enabled.
|
79
|
+
config.action_mailer.perform_caching = false
|
43
80
|
|
44
|
-
#
|
45
|
-
#
|
81
|
+
# Ignore bad email addresses and do not raise email delivery errors.
|
82
|
+
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
|
83
|
+
# config.action_mailer.raise_delivery_errors = false
|
46
84
|
|
47
85
|
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
48
|
-
# the I18n.default_locale when a translation
|
86
|
+
# the I18n.default_locale when a translation cannot be found).
|
49
87
|
config.i18n.fallbacks = true
|
50
88
|
|
51
|
-
#
|
52
|
-
config.active_support.
|
89
|
+
# Don't log any deprecations.
|
90
|
+
config.active_support.report_deprecations = false
|
53
91
|
|
54
|
-
#
|
55
|
-
|
56
|
-
# config.active_record.auto_explain_threshold_in_seconds = 0.5
|
92
|
+
# Do not dump schema after migrations.
|
93
|
+
config.active_record.dump_schema_after_migration = false
|
57
94
|
|
58
|
-
|
95
|
+
# Enable DNS rebinding protection and other `Host` header attacks.
|
96
|
+
# config.hosts = [
|
97
|
+
# "example.com", # Allow requests from example.com
|
98
|
+
# /.*\.example\.com/ # Allow requests from subdomains like `www.example.com`
|
99
|
+
# ]
|
100
|
+
# Skip DNS rebinding protection for the default health check endpoint.
|
101
|
+
# config.host_authorization = { exclude: ->(request) { request.path == "/up" } }
|
59
102
|
end
|
@@ -1,35 +1,69 @@
|
|
1
|
-
|
2
|
-
# Settings specified here will take precedence over those in config/application.rb
|
1
|
+
require "active_support/core_ext/integer/time"
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
config.cache_classes = true
|
3
|
+
# The test environment is used exclusively to run your application's
|
4
|
+
# test suite. You never need to work with it otherwise. Remember that
|
5
|
+
# your test database is "scratch space" for the test suite and is wiped
|
6
|
+
# and recreated between test runs. Don't rely on the data there!
|
9
7
|
|
10
|
-
|
11
|
-
# config.
|
12
|
-
# config.static_cache_control = "public, max-age=3600"
|
8
|
+
Rails.application.configure do
|
9
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
13
10
|
|
14
|
-
#
|
11
|
+
# While tests run files are not watched, reloading is not necessary.
|
12
|
+
config.enable_reloading = false
|
13
|
+
|
14
|
+
# Eager loading loads your entire application. When running a single test locally,
|
15
|
+
# this is usually not necessary, and can slow down your test suite. However, it's
|
16
|
+
# recommended that you enable it in continuous integration systems to ensure eager
|
17
|
+
# loading is working properly before deploying your code.
|
18
|
+
config.eager_load = ENV["CI"].present?
|
19
|
+
|
20
|
+
# Configure public file server for tests with Cache-Control for performance.
|
21
|
+
config.public_file_server.headers = { "Cache-Control" => "public, max-age=#{1.hour.to_i}" }
|
22
|
+
|
23
|
+
# Show full error reports and disable caching.
|
15
24
|
config.consider_all_requests_local = true
|
16
25
|
config.action_controller.perform_caching = false
|
26
|
+
config.cache_store = :null_store
|
17
27
|
|
18
|
-
#
|
19
|
-
config.action_dispatch.show_exceptions =
|
28
|
+
# Render exception templates for rescuable exceptions and raise for other exceptions.
|
29
|
+
config.action_dispatch.show_exceptions = :rescuable
|
30
|
+
|
31
|
+
# Disable request forgery protection in test environment.
|
32
|
+
config.action_controller.allow_forgery_protection = false
|
33
|
+
|
34
|
+
# Store uploaded files on the local file system in a temporary directory.
|
35
|
+
config.active_storage.service = :test
|
20
36
|
|
21
|
-
#
|
22
|
-
config.action_controller.allow_forgery_protection = false
|
37
|
+
config.active_job.queue_adapter = :test # TODO: remove alongside support for Rails below 7.2
|
23
38
|
|
24
|
-
#
|
25
|
-
#
|
39
|
+
# Disable caching for Action Mailer templates even if Action Controller
|
40
|
+
# caching is enabled.
|
41
|
+
config.action_mailer.perform_caching = false
|
26
42
|
|
27
|
-
#
|
43
|
+
# Tell Action Mailer not to deliver emails to the real world.
|
44
|
+
# The :test delivery method accumulates sent emails in the
|
45
|
+
# ActionMailer::Base.deliveries array.
|
46
|
+
config.action_mailer.delivery_method = :test
|
47
|
+
|
48
|
+
# Unlike controllers, the mailer instance doesn't have any context about the
|
49
|
+
# incoming request so you'll need to provide the :host parameter yourself.
|
50
|
+
config.action_mailer.default_url_options = { host: "www.example.com" }
|
51
|
+
|
52
|
+
# Print deprecation notices to the stderr.
|
28
53
|
config.active_support.deprecation = :stderr
|
29
54
|
|
30
|
-
|
55
|
+
# Raise exceptions for disallowed deprecations.
|
56
|
+
config.active_support.disallowed_deprecation = :raise
|
31
57
|
|
32
|
-
|
58
|
+
# Tell Active Support which deprecation messages to disallow.
|
59
|
+
config.active_support.disallowed_deprecation_warnings = []
|
33
60
|
|
34
|
-
|
61
|
+
# Raises error for missing translations.
|
62
|
+
# config.i18n.raise_on_missing_translations = true
|
63
|
+
|
64
|
+
# Annotate rendered view with file names.
|
65
|
+
# config.action_view.annotate_rendered_view_with_filenames = true
|
66
|
+
|
67
|
+
# Raise error when a before_action's only/except options reference missing actions.
|
68
|
+
# config.action_controller.raise_on_missing_callback_actions = true
|
35
69
|
end
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -1,21 +1,28 @@
|
|
1
|
-
# encoding: UTF-8
|
2
1
|
# This file is auto-generated from the current state of the database. Instead
|
3
2
|
# of editing this file, please use the migrations feature of Active Record to
|
4
3
|
# incrementally modify your database, and then regenerate this schema definition.
|
5
4
|
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# from scratch.
|
10
|
-
#
|
5
|
+
# This file is the source Rails uses to define your schema when running `bin/rails
|
6
|
+
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
|
7
|
+
# be faster and is potentially less error prone than running all of your
|
8
|
+
# migrations from scratch. Old migrations may fail to apply correctly if those
|
9
|
+
# migrations use external dependencies or application code.
|
11
10
|
#
|
12
|
-
# It's strongly recommended
|
11
|
+
# It's strongly recommended that you check this file into your version control system.
|
13
12
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
13
|
+
ActiveRecord::Schema.define(version: 2024_09_07_041839) do
|
15
14
|
|
16
|
-
create_table "
|
15
|
+
create_table "comments", force: :cascade do |t|
|
16
|
+
t.integer "post_id"
|
17
|
+
t.datetime "created_at", precision: 6, null: false
|
18
|
+
t.datetime "updated_at", precision: 6, null: false
|
19
|
+
t.index ["post_id"], name: "index_comments_on_post_id"
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table "posts", force: :cascade do |t|
|
17
23
|
t.datetime "created_at", null: false
|
18
24
|
t.datetime "updated_at", null: false
|
19
25
|
end
|
20
26
|
|
27
|
+
add_foreign_key "comments", "posts"
|
21
28
|
end
|
@@ -1,15 +1,51 @@
|
|
1
|
-
require '
|
1
|
+
require 'rails_helper'
|
2
2
|
require 'shared_examples/decoratable'
|
3
3
|
|
4
4
|
RSpec.describe Post do
|
5
|
+
let(:record) { described_class.create! }
|
6
|
+
|
5
7
|
it_behaves_like 'a decoratable model'
|
6
8
|
|
7
9
|
it { should be_a ApplicationRecord }
|
8
10
|
|
9
|
-
describe '
|
10
|
-
|
11
|
-
|
11
|
+
describe 'associations' do
|
12
|
+
context 'when decorated' do
|
13
|
+
subject { associated.decorate }
|
14
|
+
|
15
|
+
let(:associated) { record.comments }
|
16
|
+
let(:persisted) { associated.create! [{}] * rand(0..2) }
|
17
|
+
let(:unsaved) { associated.build [{}] * rand(1..2) }
|
18
|
+
|
19
|
+
before { persisted } # should exist
|
20
|
+
|
21
|
+
it 'returns a decorated collection' do
|
22
|
+
is_expected.to match_array persisted
|
23
|
+
is_expected.to be_all &:decorated?
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'uses cached records' do
|
27
|
+
expect(associated).not_to be_loaded
|
28
|
+
|
29
|
+
associated.load
|
30
|
+
|
31
|
+
expect { subject.to_a }.to execute.exactly(0).queries
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'caches records' do
|
35
|
+
expect(associated).not_to be_loaded
|
36
|
+
|
37
|
+
associated.decorate
|
38
|
+
|
39
|
+
expect { subject.to_a; associated.load }.to execute.exactly(0).queries
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'with unsaved records' do
|
43
|
+
before { unsaved } # should exist
|
12
44
|
|
13
|
-
|
45
|
+
it 'respects unsaved records' do
|
46
|
+
is_expected.to match_array persisted + unsaved
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
14
50
|
end
|
15
51
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
2
|
+
require 'spec_helper'
|
3
|
+
ENV['RAILS_ENV'] ||= 'test'
|
4
|
+
require_relative '../config/environment'
|
5
|
+
# Prevent database truncation if the environment is production
|
6
|
+
abort("The Rails environment is running in production mode!") if Rails.env.production?
|
7
|
+
require 'rspec/rails'
|
8
|
+
require 'rspec/activerecord/expectations'
|
9
|
+
# Add additional requires below this line. Rails is not loaded until this point!
|
10
|
+
|
11
|
+
# Requires supporting ruby files with custom matchers and macros, etc, in
|
12
|
+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
|
13
|
+
# run as spec files by default. This means that files in spec/support that end
|
14
|
+
# in _spec.rb will both be required and run as specs, causing the specs to be
|
15
|
+
# run twice. It is recommended that you do not name files matching this glob to
|
16
|
+
# end with _spec.rb. You can configure this pattern with the --pattern
|
17
|
+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
|
18
|
+
#
|
19
|
+
# The following line is provided for convenience purposes. It has the downside
|
20
|
+
# of increasing the boot-up time by auto-requiring all files in the support
|
21
|
+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
|
22
|
+
# require only the support files necessary.
|
23
|
+
#
|
24
|
+
# Rails.root.glob('spec/support/**/*.rb').sort.each { |f| require f }
|
25
|
+
|
26
|
+
# Checks for pending migrations and applies them before tests are run.
|
27
|
+
# If you are not using ActiveRecord, you can remove these lines.
|
28
|
+
begin
|
29
|
+
ActiveRecord::Migration.maintain_test_schema!
|
30
|
+
rescue ActiveRecord::PendingMigrationError => e
|
31
|
+
abort e.to_s.strip
|
32
|
+
end
|
33
|
+
RSpec.configure do |config|
|
34
|
+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
35
|
+
# config.fixture_paths = [
|
36
|
+
# Rails.root.join('spec/fixtures')
|
37
|
+
# ]
|
38
|
+
|
39
|
+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
40
|
+
# examples within a transaction, remove the following line or assign false
|
41
|
+
# instead of true.
|
42
|
+
# config.use_transactional_fixtures = true
|
43
|
+
|
44
|
+
# You can uncomment this line to turn off ActiveRecord support entirely.
|
45
|
+
# config.use_active_record = false
|
46
|
+
|
47
|
+
# RSpec Rails can automatically mix in different behaviours to your tests
|
48
|
+
# based on their file location, for example enabling you to call `get` and
|
49
|
+
# `post` in specs under `spec/controllers`.
|
50
|
+
#
|
51
|
+
# You can disable this behaviour by removing the line below, and instead
|
52
|
+
# explicitly tag your specs with their type, e.g.:
|
53
|
+
#
|
54
|
+
# RSpec.describe UsersController, type: :controller do
|
55
|
+
# # ...
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# The different available types are documented in the features, such as in
|
59
|
+
# https://rspec.info/features/6-0/rspec-rails
|
60
|
+
config.infer_spec_type_from_file_location!
|
61
|
+
|
62
|
+
# Filter lines from Rails gems in backtraces.
|
63
|
+
config.filter_rails_from_backtrace!
|
64
|
+
# arbitrary gems may also be filtered via:
|
65
|
+
# config.filter_gems_from_backtrace("gem name")
|
66
|
+
|
67
|
+
# Extra matchers
|
68
|
+
config.include RSpec::ActiveRecord::Expectations
|
69
|
+
end
|