draper 4.0.2 → 4.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +41 -25
  3. data/.rspec +1 -2
  4. data/CHANGELOG.md +18 -0
  5. data/Gemfile +27 -5
  6. data/README.md +23 -1
  7. data/draper.gemspec +3 -3
  8. data/lib/draper/automatic_delegation.rb +14 -4
  9. data/lib/draper/decoratable/collection_proxy.rb +15 -0
  10. data/lib/draper/decoratable.rb +2 -2
  11. data/lib/draper/query_methods.rb +1 -1
  12. data/lib/draper/railtie.rb +8 -4
  13. data/lib/draper/version.rb +1 -1
  14. data/lib/draper/view_context/build_strategy.rb +1 -9
  15. data/lib/draper.rb +0 -6
  16. data/spec/draper/decoratable_spec.rb +1 -1
  17. data/spec/draper/decorator_spec.rb +2 -2
  18. data/spec/draper/factory_spec.rb +8 -8
  19. data/spec/draper/query_methods_spec.rb +10 -0
  20. data/spec/draper/view_context/build_strategy_spec.rb +1 -18
  21. data/spec/dummy/.rspec +0 -1
  22. data/spec/dummy/app/decorators/comment_decorator.rb +13 -0
  23. data/spec/dummy/app/decorators/mongoid_post_decorator.rb +1 -3
  24. data/spec/dummy/app/models/admin.rb +2 -4
  25. data/spec/dummy/app/models/comment.rb +3 -0
  26. data/spec/dummy/app/models/mongoid_post.rb +2 -4
  27. data/spec/dummy/app/models/post.rb +4 -0
  28. data/spec/dummy/app/models/user.rb +2 -4
  29. data/spec/dummy/config/application.rb +22 -51
  30. data/spec/dummy/config/environments/development.rb +65 -22
  31. data/spec/dummy/config/environments/production.rb +76 -33
  32. data/spec/dummy/config/environments/test.rb +55 -21
  33. data/spec/dummy/config/initializers/draper.rb +4 -2
  34. data/spec/dummy/db/migrate/20240907041839_create_comments.rb +9 -0
  35. data/spec/dummy/db/schema.rb +16 -9
  36. data/spec/dummy/spec/decorators/active_model_serializers_spec.rb +1 -1
  37. data/spec/dummy/spec/decorators/post_decorator_spec.rb +1 -1
  38. data/spec/dummy/spec/jobs/publish_post_job_spec.rb +2 -0
  39. data/spec/dummy/spec/models/post_spec.rb +41 -5
  40. data/spec/dummy/spec/rails_helper.rb +69 -0
  41. data/spec/dummy/spec/spec_helper.rb +90 -5
  42. data/spec/performance/benchmark.rb +1 -1
  43. data/spec/support/dummy_app.rb +1 -1
  44. metadata +22 -13
@@ -1,63 +1,34 @@
1
- require File.expand_path('../boot', __FILE__)
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
- require 'draper'
10
- attempt_require 'mongoid'
11
- attempt_require 'devise'
12
- require 'active_model_serializers'
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
- # Settings in config/environments/* take precedence over those specified here.
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
- # Enforce whitelist mode for mass assignment.
50
- # This will create an empty whitelist of attributes available for mass-assignment for all models
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
- # Enable the asset pipeline
56
- # config.assets.enabled = true
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
- # Version of your assets, change this if you want to expire all your assets
59
- # config.assets.version = '1.0'
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
- Dummy::Application.configure do
2
- # Settings specified here will take precedence over those in config/application.rb
1
+ require "active_support/core_ext/integer/time"
3
2
 
4
- # In the development environment your application's code is reloaded on
5
- # every request. This slows down response time but is perfect for development
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.cache_classes = false
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
- # Show full error reports and disable caching
10
- config.consider_all_requests_local = true
11
- config.action_controller.perform_caching = false
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
- # Print deprecation notices to the Rails logger
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
- # Only use best-standards-support built into browsers
17
- config.action_dispatch.best_standards_support = :builtin
49
+ # Raise exceptions for disallowed deprecations.
50
+ config.active_support.disallowed_deprecation = :raise
18
51
 
19
- config.eager_load = false
52
+ # Tell Active Support which deprecation messages to disallow.
53
+ config.active_support.disallowed_deprecation_warnings = []
20
54
 
21
- # Raise exception on mass assignment protection for Active Record models
22
- # config.active_record.mass_assignment_sanitizer = :strict
55
+ # Raise an error on page load if there are pending migrations.
56
+ config.active_record.migration_error = :page_load
23
57
 
24
- # Log the query plan for queries taking more than this (works
25
- # with SQLite, MySQL, and PostgreSQL)
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
- # Do not compress assets
29
- # config.assets.compress = false
61
+ # Highlight code that enqueued background job in logs.
62
+ # config.active_job.verbose_enqueue_logs = true
30
63
 
31
- # Expands the lines which load the assets
32
- # config.assets.debug = true
64
+ # Suppress logger output for asset requests.
65
+ # config.assets.quiet = true
33
66
 
34
- config.active_storage.service = :local
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
- Dummy::Application.configure do
2
- # Settings specified here will take precedence over those in config/application.rb
1
+ require "active_support/core_ext/integer/time"
3
2
 
4
- # Code is not reloaded between requests
5
- config.cache_classes = true
3
+ Rails.application.configure do
4
+ # Settings specified here will take precedence over those in config/application.rb.
6
5
 
7
- # Full error reports are disabled and caching is turned on
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
- # Disable Rails's static asset server (Apache or nginx will already do this)
12
- config.serve_static_assets = false
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
- config.eager_load = true
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
- # Defaults to nil and saved in location specified by config.assets.prefix
17
- # config.assets.manifest = YOUR_PATH
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 apache
21
- # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
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
- # See everything in the log (default is :info)
27
- # config.log_level = :debug
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
- # config.log_tags = [ :subdomain, :uuid ]
62
+ # Prepend all log lines with the following tags.
63
+ config.log_tags = [:request_id]
31
64
 
32
- # Use a different logger for distributed setups
33
- # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
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
- # Enable serving of images, stylesheets, and JavaScripts from an asset server
39
- # config.action_controller.asset_host = "http://assets.example.com"
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
- # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
42
- # config.assets.precompile += %w( search.js )
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
- # Enable threaded mode
45
- # config.threadsafe!
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 can not be found)
86
+ # the I18n.default_locale when a translation cannot be found).
49
87
  config.i18n.fallbacks = true
50
88
 
51
- # Send deprecation notices to registered listeners
52
- config.active_support.deprecation = :notify
89
+ # Don't log any deprecations.
90
+ config.active_support.report_deprecations = false
53
91
 
54
- # Log the query plan for queries taking more than this (works
55
- # with SQLite, MySQL, and PostgreSQL)
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
- config.active_storage.service = :local
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
- Dummy::Application.configure do
2
- # Settings specified here will take precedence over those in config/application.rb
1
+ require "active_support/core_ext/integer/time"
3
2
 
4
- # The test environment is used exclusively to run your application's
5
- # test suite. You never need to work with it otherwise. Remember that
6
- # your test database is "scratch space" for the test suite and is wiped
7
- # and recreated between test runs. Don't rely on the data there!
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
- # Configure static asset server for tests with Cache-Control for performance
11
- # config.serve_static_assets = true
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
- # Show full error reports and disable caching
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
- # Raise exceptions instead of rendering exception templates
19
- config.action_dispatch.show_exceptions = false
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
- # Disable request forgery protection in test environment
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
- # Raise exception on mass assignment protection for Active Record models
25
- # config.active_record.mass_assignment_sanitizer = :strict
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
- # Print deprecation notices to the stderr
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
- config.eager_load = false
55
+ # Raise exceptions for disallowed deprecations.
56
+ config.active_support.disallowed_deprecation = :raise
31
57
 
32
- config.active_job.queue_adapter = :test
58
+ # Tell Active Support which deprecation messages to disallow.
59
+ config.active_support.disallowed_deprecation_warnings = []
33
60
 
34
- config.active_storage.service = :test
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
@@ -1,3 +1,5 @@
1
- Draper.configure do |config|
2
- config.default_controller = BaseController
1
+ Rails.application.config.to_prepare do
2
+ Draper.configure do |config|
3
+ config.default_controller = BaseController
4
+ end
3
5
  end
@@ -0,0 +1,9 @@
1
+ class CreateComments < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :comments do |t|
4
+ t.references :post, foreign_key: true
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -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
- # Note that this schema.rb definition is the authoritative source for your
7
- # database schema. If you need to create the application database on another
8
- # system, you should be using db:schema:load, not running all the migrations
9
- # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
- # you'll amass, the slower it'll run and the greater likelihood for issues).
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 to check this file into your version control system.
11
+ # It's strongly recommended that you check this file into your version control system.
13
12
 
14
- ActiveRecord::Schema.define(version: 20121019115657) do
13
+ ActiveRecord::Schema.define(version: 2024_09_07_041839) do
15
14
 
16
- create_table "posts", force: true do |t|
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,4 +1,4 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  describe Draper::CollectionDecorator do
4
4
  describe "#active_model_serializer" do
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  describe PostDecorator do
4
4
  let(:decorator) { PostDecorator.new(object) }
@@ -1,3 +1,5 @@
1
+ require 'rails_helper'
2
+
1
3
  RSpec.describe PublishPostJob, type: :job do
2
4
  let(:post) { Post.create.decorate }
3
5
 
@@ -1,15 +1,51 @@
1
- require 'spec_helper'
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 '#to_global_id' do
10
- let(:post) { Post.create }
11
- subject { post.to_global_id }
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
- it { is_expected.to eq post.decorate.to_global_id }
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