view_component/test/test_helper.rb

199 строки
5.7 KiB
Ruby
Исходник Постоянная ссылка Обычный вид История

2019-08-16 20:09:33 +03:00
# frozen_string_literal: true
require "allocation_stats"
require "simplecov"
2020-07-27 23:46:40 +03:00
require "simplecov-console"
require "rails/version"
2020-09-22 01:56:14 +03:00
if ENV["MEASURE_COVERAGE"]
2020-09-22 01:54:36 +03:00
SimpleCov.start do
command_name "minitest-rails#{Rails::VERSION::STRING}-ruby#{RUBY_VERSION}"
2020-08-22 01:23:38 +03:00
2020-09-22 01:54:36 +03:00
formatter SimpleCov::Formatter::Console
end
end
2019-08-16 20:09:33 +03:00
require "bundler/setup"
require "pathname"
require "minitest/autorun"
if ENV["RAISE_ON_WARNING"]
module Warning
PROJECT_ROOT = File.expand_path("..", __dir__).freeze
def self.warn(message)
called_by = caller_locations(1, 1).first.path
return super unless called_by&.start_with?(PROJECT_ROOT) && !called_by.start_with?("#{PROJECT_ROOT}/vendor")
raise "Warning: #{message}"
end
end
end
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"
require "view_component/deprecation"
ViewComponent::Deprecation.behavior = :silence
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
require File.expand_path("sandbox/config/environment.rb", __dir__)
2019-08-16 20:09:33 +03:00
require "rails/test_help"
[904] Enable testing component JS interactions using new with_rendered_component_path method (#1061) * Add render_in_browser test helper method to test JS interactions * Update test to test simple JS interactions * Fix rubocop violations * Add test coverage for with or without layout * Allow tmp file to be included to handle temp file generations * Fix more rubocop complaints * Exclude JS interaction test with layout specified * Change process_timeout on cuprite to address brittleness * Add new BrowserTestCase specified for in-browser tests of componnets * Attempt to utilize system test approach * Add selenium to appease failing specs (not sure why tbh!) * Bump to redo CI * Enable changing layout with a parameter in visit_rendered_in_browser * Update docs/CHANGELOG.md * Update docs/CHANGELOG.md * Update docs/CHANGELOG.md * Fix broken specs * Remove unneccesary .gitkeep on tmp * Create tmp folder if it doesn't exist * Fix linting error * Increase wait time on capybara & remove unneccesary comments * Add test coverage for when you use or not use a layout in system tests * Add guide on how to test interactions * Fix lint errors * Secure the system test endpoint and use specific directory for tmp components * Address rubocop violations * Fix problem with the mkdir * Update condition to check for being in production * Strictly only set routes used for system_tests under RAILS_ENV=test * Place the gems used for system_test to only be included in test environment * Update test to use new block syntax * Address linting issues reported by standardrb * Update Gemfile Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update Gemfile Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update app/controllers/view_components_controller.rb Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Revert removal of tmp from .gitignore * Update Gemfile.lock * Revert gitignore change that is not needed * Update testing documentation * Apply renaming suggestions for entry point * Remove extra comment that is not needed * Further expand max_wait_time for capybara test to avoid test failures * Remove need to create dedicated folder in tmp for system test * Remove extra option for system test route drawing * Remove definition of template in with_rendered_component_in_browser * Update Gemfile.lock * Test * Add back in tmp directory to address test failures * Update Gemfile Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update test Gems to have coarse dependency pointers * Remove redundant status: 200 on system_test_entrypoint * Change test helper method to be clear why are not providing the browser, only the path! * Move the system_test_entrypoint to the PreviewsAction module * Conditionally use selenium 3 when using ruby 2.5 or below * Add option to incrase process_timeout on cuprite * Fix formatting issue on CHANGELOG.md * Update timeouts on cuprite * Update docs/CHANGELOG.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update lib/view_component/engine.rb Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Undo a change that was not intended * Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Decouple capybara from ViewComponents * Apply suggestions from code review * Add basic test for passing in parameters into component * Update API to use fragements to support slots * Update the method to be a bit more clear * Update for linting issues * Fix the method name * Fix linting issue * Place the html_safe in the class * Update API to match aligned naming * Update documentation for testing using new approach * Update yardoc * Remove empty line * Apply suggestions from code review * re-add errant removal * remove errant whitespace * render fragment directly instead of via preview template * reuse component instead of duplicating html * remove change to preview template * simplify example * simplify test helper * simplify test helper * prefix internal route with underscore * increase process_timeout Co-authored-by: Joel Hawksley <joel@hawksley.org> Co-authored-by: Joel Hawksley <joelhawksley@github.com>
2022-12-12 20:50:53 +03:00
require "capybara/cuprite"
# Rails registers its own driver named "cuprite" which will overwrite the one we
# register here. Avoid the problem by registering the driver with a distinct name.
Capybara.register_driver(:vc_cuprite) do |app|
[904] Enable testing component JS interactions using new with_rendered_component_path method (#1061) * Add render_in_browser test helper method to test JS interactions * Update test to test simple JS interactions * Fix rubocop violations * Add test coverage for with or without layout * Allow tmp file to be included to handle temp file generations * Fix more rubocop complaints * Exclude JS interaction test with layout specified * Change process_timeout on cuprite to address brittleness * Add new BrowserTestCase specified for in-browser tests of componnets * Attempt to utilize system test approach * Add selenium to appease failing specs (not sure why tbh!) * Bump to redo CI * Enable changing layout with a parameter in visit_rendered_in_browser * Update docs/CHANGELOG.md * Update docs/CHANGELOG.md * Update docs/CHANGELOG.md * Fix broken specs * Remove unneccesary .gitkeep on tmp * Create tmp folder if it doesn't exist * Fix linting error * Increase wait time on capybara & remove unneccesary comments * Add test coverage for when you use or not use a layout in system tests * Add guide on how to test interactions * Fix lint errors * Secure the system test endpoint and use specific directory for tmp components * Address rubocop violations * Fix problem with the mkdir * Update condition to check for being in production * Strictly only set routes used for system_tests under RAILS_ENV=test * Place the gems used for system_test to only be included in test environment * Update test to use new block syntax * Address linting issues reported by standardrb * Update Gemfile Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update Gemfile Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update app/controllers/view_components_controller.rb Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Revert removal of tmp from .gitignore * Update Gemfile.lock * Revert gitignore change that is not needed * Update testing documentation * Apply renaming suggestions for entry point * Remove extra comment that is not needed * Further expand max_wait_time for capybara test to avoid test failures * Remove need to create dedicated folder in tmp for system test * Remove extra option for system test route drawing * Remove definition of template in with_rendered_component_in_browser * Update Gemfile.lock * Test * Add back in tmp directory to address test failures * Update Gemfile Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update test Gems to have coarse dependency pointers * Remove redundant status: 200 on system_test_entrypoint * Change test helper method to be clear why are not providing the browser, only the path! * Move the system_test_entrypoint to the PreviewsAction module * Conditionally use selenium 3 when using ruby 2.5 or below * Add option to incrase process_timeout on cuprite * Fix formatting issue on CHANGELOG.md * Update timeouts on cuprite * Update docs/CHANGELOG.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update lib/view_component/engine.rb Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Undo a change that was not intended * Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Decouple capybara from ViewComponents * Apply suggestions from code review * Add basic test for passing in parameters into component * Update API to use fragements to support slots * Update the method to be a bit more clear * Update for linting issues * Fix the method name * Fix linting issue * Place the html_safe in the class * Update API to match aligned naming * Update documentation for testing using new approach * Update yardoc * Remove empty line * Apply suggestions from code review * re-add errant removal * remove errant whitespace * render fragment directly instead of via preview template * reuse component instead of duplicating html * remove change to preview template * simplify example * simplify test helper * simplify test helper * prefix internal route with underscore * increase process_timeout Co-authored-by: Joel Hawksley <joel@hawksley.org> Co-authored-by: Joel Hawksley <joelhawksley@github.com>
2022-12-12 20:50:53 +03:00
# Add the process_timeout option to prevent failures due to the browser
# taking too long to start up.
Capybara::Cuprite::Driver.new(app, {process_timeout: 60, timeout: 30})
end
# Reduce extra logs produced by puma booting up
Capybara.server = :puma, {Silent: true}
# Increase the max wait time to appease test failures due to timeouts.
Capybara.default_max_wait_time = 30
[904] Enable testing component JS interactions using new with_rendered_component_path method (#1061) * Add render_in_browser test helper method to test JS interactions * Update test to test simple JS interactions * Fix rubocop violations * Add test coverage for with or without layout * Allow tmp file to be included to handle temp file generations * Fix more rubocop complaints * Exclude JS interaction test with layout specified * Change process_timeout on cuprite to address brittleness * Add new BrowserTestCase specified for in-browser tests of componnets * Attempt to utilize system test approach * Add selenium to appease failing specs (not sure why tbh!) * Bump to redo CI * Enable changing layout with a parameter in visit_rendered_in_browser * Update docs/CHANGELOG.md * Update docs/CHANGELOG.md * Update docs/CHANGELOG.md * Fix broken specs * Remove unneccesary .gitkeep on tmp * Create tmp folder if it doesn't exist * Fix linting error * Increase wait time on capybara & remove unneccesary comments * Add test coverage for when you use or not use a layout in system tests * Add guide on how to test interactions * Fix lint errors * Secure the system test endpoint and use specific directory for tmp components * Address rubocop violations * Fix problem with the mkdir * Update condition to check for being in production * Strictly only set routes used for system_tests under RAILS_ENV=test * Place the gems used for system_test to only be included in test environment * Update test to use new block syntax * Address linting issues reported by standardrb * Update Gemfile Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update Gemfile Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update app/controllers/view_components_controller.rb Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Revert removal of tmp from .gitignore * Update Gemfile.lock * Revert gitignore change that is not needed * Update testing documentation * Apply renaming suggestions for entry point * Remove extra comment that is not needed * Further expand max_wait_time for capybara test to avoid test failures * Remove need to create dedicated folder in tmp for system test * Remove extra option for system test route drawing * Remove definition of template in with_rendered_component_in_browser * Update Gemfile.lock * Test * Add back in tmp directory to address test failures * Update Gemfile Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update test Gems to have coarse dependency pointers * Remove redundant status: 200 on system_test_entrypoint * Change test helper method to be clear why are not providing the browser, only the path! * Move the system_test_entrypoint to the PreviewsAction module * Conditionally use selenium 3 when using ruby 2.5 or below * Add option to incrase process_timeout on cuprite * Fix formatting issue on CHANGELOG.md * Update timeouts on cuprite * Update docs/CHANGELOG.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> Update lib/view_component/engine.rb Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Undo a change that was not intended * Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update docs/guide/testing.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Decouple capybara from ViewComponents * Apply suggestions from code review * Add basic test for passing in parameters into component * Update API to use fragements to support slots * Update the method to be a bit more clear * Update for linting issues * Fix the method name * Fix linting issue * Place the html_safe in the class * Update API to match aligned naming * Update documentation for testing using new approach * Update yardoc * Remove empty line * Apply suggestions from code review * re-add errant removal * remove errant whitespace * render fragment directly instead of via preview template * reuse component instead of duplicating html * remove change to preview template * simplify example * simplify test helper * simplify test helper * prefix internal route with underscore * increase process_timeout Co-authored-by: Joel Hawksley <joel@hawksley.org> Co-authored-by: Joel Hawksley <joelhawksley@github.com>
2022-12-12 20:50:53 +03:00
def with_config_option(option_name, new_value, config_entrypoint: Rails.application.config.view_component)
old_value = config_entrypoint.public_send(option_name)
config_entrypoint.public_send(:"#{option_name}=", new_value)
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
yield
ensure
config_entrypoint.public_send(:"#{option_name}=", old_value)
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
end
2021-02-17 04:36:40 +03:00
# Sets custom preview paths in tests.
#
# @param new_value [Array<String>] List of preview paths
# @yield Test code to run
# @return [void]
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
def with_preview_paths(new_value, &block)
with_config_option(:preview_paths, new_value, &block)
2021-02-17 04:36:40 +03:00
end
def with_preview_route(new_value)
old_value = Rails.application.config.view_component.preview_route
Rails.application.config.view_component.preview_route = new_value
app.reloader.reload!
yield
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
ensure
Rails.application.config.view_component.preview_route = old_value
app.reloader.reload!
end
2020-10-04 04:33:05 +03:00
def with_preview_controller(new_value)
old_value = Rails.application.config.view_component.preview_controller
Rails.application.config.view_component.preview_controller = new_value
app.reloader.reload!
yield
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
ensure
2020-10-04 04:33:05 +03:00
Rails.application.config.view_component.preview_controller = old_value
app.reloader.reload!
end
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
def with_custom_component_path(new_value, &block)
with_config_option(:view_component_path, new_value, &block)
end
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
def with_custom_component_parent_class(new_value, &block)
with_config_option(:component_parent_class, new_value, &block)
2021-09-17 20:30:48 +03:00
end
def with_application_component_class
Object.const_set(:ApplicationComponent, Class.new(Object))
2021-09-17 20:30:48 +03:00
yield
ensure
Object.send(:remove_const, :ApplicationComponent)
end
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
def with_generate_option(config_option, value)
old_value = Rails.application.config.view_component.generate[config_option]
Rails.application.config.view_component.generate[config_option] = value
yield
ensure
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
Rails.application.config.view_component.generate[config_option] = old_value
end
def with_generate_sidecar(enabled, &block)
with_generate_option(:sidecar, enabled, &block)
end
def with_template_caching
old_cache_template_loading = ActionView::Base.cache_template_loading
ActionView::Base.cache_template_loading = true
yield
ensure
ActionView::Base.cache_template_loading = old_cache_template_loading
end
def with_new_cache
old_cache = ViewComponent::CompileCache.cache
ViewComponent::CompileCache.cache = Set.new
old_cache_template_loading = ActionView::Base.cache_template_loading
ActionView::Base.cache_template_loading = false
yield
ensure
ActionView::Base.cache_template_loading = old_cache_template_loading
ViewComponent::CompileCache.cache = old_cache
end
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
def without_template_annotations(&block)
2021-05-10 17:58:56 +03:00
if ActionView::Base.respond_to?(:annotate_rendered_view_with_filenames)
old_value = ActionView::Base.annotate_rendered_view_with_filenames
ActionView::Base.annotate_rendered_view_with_filenames = false
app.reloader.reload! if defined?(app)
2021-05-10 17:58:56 +03:00
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
with_new_cache(&block)
2021-05-10 17:58:56 +03:00
ActionView::Base.annotate_rendered_view_with_filenames = old_value
app.reloader.reload! if defined?(app)
2021-05-10 17:58:56 +03:00
else
yield
end
end
def modify_file(file, content)
filename = Rails.root.join(file)
old_content = File.read(filename)
begin
File.open(filename, "wb+") { |f| f.write(content) }
yield
ensure
File.open(filename, "wb+") { |f| f.write(old_content) }
end
end
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
def with_default_preview_layout(layout, &block)
with_config_option(:default_preview_layout, layout, &block)
end
Centralize ViewComponent::Base config (#1291) * Update changelog * Clean up mirrored config options from ViewComponent::Base * Refactor generate test helper * Test that app config and ViewComponent::Base config are equivalent * Update documentation on defaults * Update lib/view_component/base.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * Create dedicated config object * Move default config to constant * Test to ensure preview_path alias is maintained * Use OrderedOptions for generate config option to maintain behavior * Update some defaults * Remove duplicate config definitions These are specified in ViewComponent::Config now. * Clarify necessity of specifying preview paths * Delegate the majority of config options back to application config * Move remainder of Previewable accessors to app config * Extract out default preview paths * Make all config accessors accessible on ViewComponent::Base by default * Remove the Previewable module ALl it did was make preview-related config accessible on ViewComponent::Base * Add default_preview_layout to global config * Run `bundle update` * Make ViewComponent::Base the source of truth for config * Add backwards compatibility for config_accessor defaults * Restore documentation for config options * Improve test coverage * Directly require ViewComponent::Base to fix Primer CI * Directly require ViewComponent::Config to fix Primer CI * Move config require to ViewComponent::Base * Require ViewComponent::Deprecation in Config * Add missing defaults * Reset default_preview_layout to `nil` * Remove all changes to docs/api.md * Add code ownership * s/with_generate/with_generate_option/ * Explain alias between .default and .new * Draft test to ensure all config options are documented The pieces are in place, but I think the final aim is that everything for which there's a default and/or a reader is documented * Ensure accessors defined in public methods are considered too * Coerce to string for backwards compatibility * Lint * Use Ruby 3.0.3 * Add more detailed changelog message Co-authored-by: Joel Hawksley <joel@hawksley.org> * Manually restore the Previewable changes after rebase * Lint * Remove global output buffer option * Remove previewable module Config options for preview are now under ViewComponent::Config. * Make Rails.application.config.view_component the source of truth ViewComponent::Base should continue to work but will delegate back to that. * Update missed stub * Update Gemfile.lock * Compose around ActiveSupport::OrderedOptions Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> Co-authored-by: Joel Hawksley <joel@hawksley.org>
2022-08-09 16:37:16 +03:00
def with_render_monkey_patch_config(enabled, &block)
with_config_option(:render_monkey_patch_enabled, enabled, &block)
end
Improve thread safety with development compiler mode (#1202) * Improved threadsafety * add blocking and non blocking compiler mode (blocking in Rails dev & test mode) * add monitor check in render method to ensure thread safety * fixes https://github.com/github/view_component/issues/1177 * Add test for both compiler modes * refactor tests * Refactorings: * rename blocking & non-blocking to development & production * use constants * some more renamings * Refactorings: * remove do_compile since extraction is no longer needed for DRY * Refactorings: * when compiler runs in production mode, don't add monitor check in the `.call` method * Refactorings: * refactor tests * Make compiler mode a class attribute on the compiler * add test * make test more explicit * Update docs/CHANGELOG.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update lib/view_component/compiler.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * Update lib/view_component/compiler.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * DRY method implementation * fix order * Update docs/CHANGELOG.md Co-authored-by: Simon Fish <si@mon.fish> * Update lib/view_component/compiler.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * enhance comment * revert test * fix error * remove test which causes failures with multithread test * Revert "remove test which causes failures with multithread test" This reverts commit 87d5c1a9b12ff6c727889d9159a26c75e462d472. * remove test which causes failures with multithread test * Re-run the initializer after playing around with Rails.env * fix linter * Update test/view_component/integration_test.rb Co-authored-by: Simon Fish <si@mon.fish> Co-authored-by: Joel Hawksley <joelhawksley@github.com> Co-authored-by: Blake Williams <blakewilliams@github.com> Co-authored-by: Simon Fish <si@mon.fish> Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com>
2022-01-06 00:41:17 +03:00
Refactor compiler for clarity (#2091) * add test case * two tests remaining * first time all green * Fix final line endings * add docs, changelog, test helpers * Fix final line endings * streamline compiler method generation * simplification * refactor to remove index usage * clearer control flow * lint * md lint * remove remaining hardcoded formats * Update lib/view_component/base.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * remove unnecessary `inspect` * remove unused `identifier` * inline single-use method * add safe navigation * consolidate template collision error messages to include format * add backticks around variant name in error * use one line syntax * no need to return false * inline single-use method * remove offhand comment that would be better in the compiler, if at all * inline barely used variable * consolidate safe_class_name into default_method_name * use shorter `component` instead of `component_class` * remove unnecessary conditional * remove unnecessary nesting * inline single use method * remove intermediate compiler methods * InlineTemplate is now included by default * move comment to be in correct location * start to make inline templates like other templates * continue to consolidate inline template logic * inline templates are just templates * move lock to outside conditional * use same method_name definition * all templates have line numbers * move towards single method definition path with source * template handler is already present, no need to re-calculate * just pass the template around instead of four params * use single class_eval to define template methods * remove unnecessary skipping of iterator * make template hashes look the same * move default slot registration to Slotable * define render_template_for in one place * remove duplicated method name construction * sort nils last * consolidate construction of safe_name * inline single-use method that at minimum should have been private * method name should not depend on templates state * set method name on template hash * use method name from template hash * shorten to one-line * move conditional into existing file template conditional * simplify `templates * remove unused formats method * always set @templates at the same point vs. lazily * move compiled_template to Template * clean up method name duplication * more clearly identify method name * move compile_to_component to Template * construct template objects * use template object in more places * move html format check to template * use format accessor on template * move variant access to template * remove uses for method name from hash * use template object in more locations * use template object in more places * use existing template object * add comment for reload functionality * no more template hash! * do not define branches if it's not used * use single code path for safe method definition for templates * consolidate render_template_for duplication * consolidate to one-liner * consolidate to one-liner * pass redefinition lock to template * use template object for inline templates in render_template_for * remove unused method definition * simplify error check * use default method name in fewer cases * remove unreachable code * remove method name logic from compiler * simplify lookup of variants * inline variants accessor * remove duplicative normalized_variant_name * add note about allowing inline_call / template collision * remove usage of inline_calls_defined_on_self * remove inline_calls_defined_on_self * inline_calls is only used once * add todo around tracking template rendering * simplify conditional to use templates * simplify conditional * make one-liner * gather_template_errors explicitly * use attr_reader to match templates * move logic into template error construction * couple template compilation with render method definition * define safe method as part of compilation * extract Template#inline_call? * inline inline_calls helper * use select instead of conditonal map+compact * simplify logic and naming in duplicate detection * use more specific naming for variant pairs * inline * passing around redef lock was messy * initializer is longer than 120 chars * let Ruby construct symbol string * simplify branch construction * assign body as result of conditionals * use each_with_object to simplify * use syntactic sugar * naming, method privacy * use default values * inline inline_calls * standardrb * remove out-of-date comment * shorten lines * use .first * remove attr_reader * simplify compiler mode to be boolean * remove attr_reader * extract constant for default format * split conditional into two lines for readibilty * rename compiler mode to development mode * use any? with method extracted to template * avoid use of _1 * avoid use of _1 * move Template to its own file * extract simplest case to conditional * Fix final line endings * avoid block variable * avoid more block variables * standardrb * add comment * memoize instance methods lookup * memoize instance method check * use single line for each argument * add TODO for HTML output safety check * extract and comment format and variant derivation from path * simpler extension extraction * add explainer comments for format and variant extraction * use Hash with Set value to find conflicting variants * use single loop instead of two * standardrb * indicate constant is in internal * move redefinition lock back to compiler * remove loop --------- Co-authored-by: GitHub Actions Bot <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Blake Williams <blakewilliams@github.com>
2024-09-11 23:43:58 +03:00
def with_compiler_development_mode(mode)
previous_mode = ViewComponent::Compiler.development_mode
ViewComponent::Compiler.development_mode = mode
Improve thread safety with development compiler mode (#1202) * Improved threadsafety * add blocking and non blocking compiler mode (blocking in Rails dev & test mode) * add monitor check in render method to ensure thread safety * fixes https://github.com/github/view_component/issues/1177 * Add test for both compiler modes * refactor tests * Refactorings: * rename blocking & non-blocking to development & production * use constants * some more renamings * Refactorings: * remove do_compile since extraction is no longer needed for DRY * Refactorings: * when compiler runs in production mode, don't add monitor check in the `.call` method * Refactorings: * refactor tests * Make compiler mode a class attribute on the compiler * add test * make test more explicit * Update docs/CHANGELOG.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update lib/view_component/compiler.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * Update lib/view_component/compiler.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * DRY method implementation * fix order * Update docs/CHANGELOG.md Co-authored-by: Simon Fish <si@mon.fish> * Update lib/view_component/compiler.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * enhance comment * revert test * fix error * remove test which causes failures with multithread test * Revert "remove test which causes failures with multithread test" This reverts commit 87d5c1a9b12ff6c727889d9159a26c75e462d472. * remove test which causes failures with multithread test * Re-run the initializer after playing around with Rails.env * fix linter * Update test/view_component/integration_test.rb Co-authored-by: Simon Fish <si@mon.fish> Co-authored-by: Joel Hawksley <joelhawksley@github.com> Co-authored-by: Blake Williams <blakewilliams@github.com> Co-authored-by: Simon Fish <si@mon.fish> Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com>
2022-01-06 00:41:17 +03:00
yield
ensure
Refactor compiler for clarity (#2091) * add test case * two tests remaining * first time all green * Fix final line endings * add docs, changelog, test helpers * Fix final line endings * streamline compiler method generation * simplification * refactor to remove index usage * clearer control flow * lint * md lint * remove remaining hardcoded formats * Update lib/view_component/base.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * remove unnecessary `inspect` * remove unused `identifier` * inline single-use method * add safe navigation * consolidate template collision error messages to include format * add backticks around variant name in error * use one line syntax * no need to return false * inline single-use method * remove offhand comment that would be better in the compiler, if at all * inline barely used variable * consolidate safe_class_name into default_method_name * use shorter `component` instead of `component_class` * remove unnecessary conditional * remove unnecessary nesting * inline single use method * remove intermediate compiler methods * InlineTemplate is now included by default * move comment to be in correct location * start to make inline templates like other templates * continue to consolidate inline template logic * inline templates are just templates * move lock to outside conditional * use same method_name definition * all templates have line numbers * move towards single method definition path with source * template handler is already present, no need to re-calculate * just pass the template around instead of four params * use single class_eval to define template methods * remove unnecessary skipping of iterator * make template hashes look the same * move default slot registration to Slotable * define render_template_for in one place * remove duplicated method name construction * sort nils last * consolidate construction of safe_name * inline single-use method that at minimum should have been private * method name should not depend on templates state * set method name on template hash * use method name from template hash * shorten to one-line * move conditional into existing file template conditional * simplify `templates * remove unused formats method * always set @templates at the same point vs. lazily * move compiled_template to Template * clean up method name duplication * more clearly identify method name * move compile_to_component to Template * construct template objects * use template object in more places * move html format check to template * use format accessor on template * move variant access to template * remove uses for method name from hash * use template object in more locations * use template object in more places * use existing template object * add comment for reload functionality * no more template hash! * do not define branches if it's not used * use single code path for safe method definition for templates * consolidate render_template_for duplication * consolidate to one-liner * consolidate to one-liner * pass redefinition lock to template * use template object for inline templates in render_template_for * remove unused method definition * simplify error check * use default method name in fewer cases * remove unreachable code * remove method name logic from compiler * simplify lookup of variants * inline variants accessor * remove duplicative normalized_variant_name * add note about allowing inline_call / template collision * remove usage of inline_calls_defined_on_self * remove inline_calls_defined_on_self * inline_calls is only used once * add todo around tracking template rendering * simplify conditional to use templates * simplify conditional * make one-liner * gather_template_errors explicitly * use attr_reader to match templates * move logic into template error construction * couple template compilation with render method definition * define safe method as part of compilation * extract Template#inline_call? * inline inline_calls helper * use select instead of conditonal map+compact * simplify logic and naming in duplicate detection * use more specific naming for variant pairs * inline * passing around redef lock was messy * initializer is longer than 120 chars * let Ruby construct symbol string * simplify branch construction * assign body as result of conditionals * use each_with_object to simplify * use syntactic sugar * naming, method privacy * use default values * inline inline_calls * standardrb * remove out-of-date comment * shorten lines * use .first * remove attr_reader * simplify compiler mode to be boolean * remove attr_reader * extract constant for default format * split conditional into two lines for readibilty * rename compiler mode to development mode * use any? with method extracted to template * avoid use of _1 * avoid use of _1 * move Template to its own file * extract simplest case to conditional * Fix final line endings * avoid block variable * avoid more block variables * standardrb * add comment * memoize instance methods lookup * memoize instance method check * use single line for each argument * add TODO for HTML output safety check * extract and comment format and variant derivation from path * simpler extension extraction * add explainer comments for format and variant extraction * use Hash with Set value to find conflicting variants * use single loop instead of two * standardrb * indicate constant is in internal * move redefinition lock back to compiler * remove loop --------- Co-authored-by: GitHub Actions Bot <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Blake Williams <blakewilliams@github.com>
2024-09-11 23:43:58 +03:00
ViewComponent::Compiler.development_mode = previous_mode
Improve thread safety with development compiler mode (#1202) * Improved threadsafety * add blocking and non blocking compiler mode (blocking in Rails dev & test mode) * add monitor check in render method to ensure thread safety * fixes https://github.com/github/view_component/issues/1177 * Add test for both compiler modes * refactor tests * Refactorings: * rename blocking & non-blocking to development & production * use constants * some more renamings * Refactorings: * remove do_compile since extraction is no longer needed for DRY * Refactorings: * when compiler runs in production mode, don't add monitor check in the `.call` method * Refactorings: * refactor tests * Make compiler mode a class attribute on the compiler * add test * make test more explicit * Update docs/CHANGELOG.md Co-authored-by: Joel Hawksley <joelhawksley@github.com> * Update lib/view_component/compiler.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * Update lib/view_component/compiler.rb Co-authored-by: Blake Williams <blakewilliams@github.com> * DRY method implementation * fix order * Update docs/CHANGELOG.md Co-authored-by: Simon Fish <si@mon.fish> * Update lib/view_component/compiler.rb Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com> * enhance comment * revert test * fix error * remove test which causes failures with multithread test * Revert "remove test which causes failures with multithread test" This reverts commit 87d5c1a9b12ff6c727889d9159a26c75e462d472. * remove test which causes failures with multithread test * Re-run the initializer after playing around with Rails.env * fix linter * Update test/view_component/integration_test.rb Co-authored-by: Simon Fish <si@mon.fish> Co-authored-by: Joel Hawksley <joelhawksley@github.com> Co-authored-by: Blake Williams <blakewilliams@github.com> Co-authored-by: Simon Fish <si@mon.fish> Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com>
2022-01-06 00:41:17 +03:00
end
2024-01-04 22:25:54 +03:00
def capture_warnings(&block)
[].tap do |warnings|
Kernel.stub(:warn, ->(msg) { warnings << msg }) do
block.call
end
end
end
def assert_allocations(count_map, &block)
trace = AllocationStats.trace(&block)
total = trace.allocations.all.size
count = count_map[RUBY_VERSION]
assert_equal count, total, "Expected #{count} allocations, got #{total} allocations for Ruby #{RUBY_VERSION}"
end