Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ nav_order: 6

## main

* Introduce component-local config and migrate `strip_trailing_whitespace` to use it under the hood.

*Simon Fish*

* Add docs about Slack channel in Ruby Central workspace. (Join us! #oss-view-component). Email [email protected] for an invite.

*Joel Hawksley
Expand Down
10 changes: 3 additions & 7 deletions docs/guide/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,14 @@ end

Code editors commonly add a trailing newline character to source files in keeping with the Unix standard. Including trailing whitespace in component templates can result in unwanted whitespace in the HTML, eg. if the component is rendered before the period at the end of a sentence.

To strip trailing whitespace from component templates, use the `strip_trailing_whitespace` component-local config option.
To strip trailing whitespace from component templates, use the `strip_trailing_whitespace` class method.

```ruby
class MyComponent < ViewComponent::Base
# do strip whitespace
configure_view_component do |config|
config.strip_trailing_whitespace = true
end
strip_trailing_whitespace

# don't strip whitespace
configure_view_component do |config|
config.strip_trailing_whitespace = false
end
strip_trailing_whitespace(false)
end
```
32 changes: 6 additions & 26 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require "view_component/collection"
require "view_component/compile_cache"
require "view_component/compiler"
require "view_component/component_local_config"
require "view_component/config"
require "view_component/errors"
require "view_component/inline_template"
Expand Down Expand Up @@ -40,7 +39,6 @@ def config
include ViewComponent::Slotable
include ViewComponent::Translatable
include ViewComponent::WithContentHelper
include ViewComponent::ComponentLocalConfig

RESERVED_PARAMETER = :content
VC_INTERNAL_DEFAULT_FORMAT = :html
Expand All @@ -51,6 +49,10 @@ def config
# For Content Security Policy nonces
delegate :content_security_policy_nonce, to: :helpers

# Config option that strips trailing whitespace in templates before compiling them.
class_attribute :__vc_strip_trailing_whitespace, instance_accessor: false, instance_predicate: false
self.__vc_strip_trailing_whitespace = false # class_attribute:default doesn't work until Rails 5.2

attr_accessor :__vc_original_view_context

# Components render in their own view context. Helpers and other functionality
Expand Down Expand Up @@ -612,38 +614,16 @@ def with_collection_parameter(parameter)
# end
# ```
#
# @deprecated Use the new component-local configuration option instead.
#
# ```ruby
# class MyComponent < ViewComponent::Base
# configure_view_component do |config|
# config.strip_trailing_whitespace = true
# end
# end
# ```
#
# @param value [Boolean] Whether to strip newlines.
def strip_trailing_whitespace(value = true)
ViewComponent::Deprecation.deprecation_warning(
"strip_trailing_whitespace",
<<~DOC
Use the new component-local configuration option instead:

class #{self.class.name} < ViewComponent::Base
configure_view_component do |config|
config.strip_trailing_whitespace = #{value}
end
end
DOC
)
view_component_config.strip_trailing_whitespace = value
self.__vc_strip_trailing_whitespace = value
end

# Whether trailing whitespace will be stripped before compilation.
#
# @return [Boolean]
def strip_trailing_whitespace?
view_component_config.strip_trailing_whitespace
__vc_strip_trailing_whitespace
end

# Ensure the component initializer accepts the
Expand Down
60 changes: 0 additions & 60 deletions lib/view_component/component_local_config.rb

This file was deleted.

7 changes: 0 additions & 7 deletions test/sandbox/app/components/configurable_component.rb

This file was deleted.

8 changes: 0 additions & 8 deletions test/sandbox/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,4 @@ def test_uses_module_configuration
assert_equal "AnotherController", TestAlreadyConfigurableModule::SomeComponent.test_controller
assert_equal "AnotherController", TestAlreadyConfiguredModule::SomeComponent.test_controller
end

def test_component_local_config_is_inheritable
assert_equal false, ViewComponent::Base.view_component_config.strip_trailing_whitespace
# This component doesn't call configure, so it should inherit the defaults.
assert_equal false, AnotherComponent.view_component_config.strip_trailing_whitespace
# This component overrides the defaults.
assert_equal true, ConfigurableComponent.view_component_config.strip_trailing_whitespace
end
end
Loading