Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Move Streamable instance methods to module body

Alexander Brandon Coles5c10801ba5d9
diff --git a/app/components/concerns/op_turbo/streamable.rb b/app/components/concerns/op_turbo/streamable.rb
index 5da280d7894..0eebfbd1a75 100644
--- a/app/components/concerns/op_turbo/streamable.rb
+++ b/app/components/concerns/op_turbo/streamable.rb
@@ -44,116 +44,114 @@ module OpTurbo
end
end
- included do
- def render_as_turbo_stream(view_context:, action: :update, method: nil, **attributes)
- case action
- when :update, *INLINE_ACTIONS
- @inner_html_only = true
- template = render_in(view_context)
- when :replace
- template = render_in(view_context)
- when :remove
- @wrapper_only = true
- render_in(view_context)
- template = nil
- else
- raise ArgumentError, "Unsupported action #{action}"
- end
-
- if INLINE_ACTIONS.exclude?(action) && !wrapped?
- raise MissingComponentWrapper,
- "Wrap your component in a `component_wrapper` block in order to use turbo-stream methods"
- end
-
- if method && !action.in?(ACTIONS_WITH_METHOD)
- raise ArgumentError, "The #{action} action does not supports a method"
- end
-
- OpTurbo::StreamComponent.new(
- action:,
- method:,
- target: wrapper_key,
- template:,
- **attributes
- ).render_in(view_context)
+ def render_as_turbo_stream(view_context:, action: :update, method: nil, **attributes)
+ case action
+ when :update, *INLINE_ACTIONS
+ @inner_html_only = true
+ template = render_in(view_context)
+ when :replace
+ template = render_in(view_context)
+ when :remove
+ @wrapper_only = true
+ render_in(view_context)
+ template = nil
+ else
+ raise ArgumentError, "Unsupported action #{action}"
end
- def insert_as_turbo_stream(component:, view_context:, action: :append)
- template = component.render_in(view_context)
-
- # The component being inserted into the target component
- # needs wrapping, not the target since it isn't the one
- # that needs to be rendered to perform this turbo stream action.
- unless component.wrapped?
- raise MissingComponentWrapper,
- "Wrap your component in a `component_wrapper` block in order to use turbo-stream methods"
- end
-
- OpTurbo::StreamComponent.new(
- action:,
- target: insert_target_modified? ? insert_target_modifier_id : wrapper_key,
- template:
- ).render_in(view_context)
+ if INLINE_ACTIONS.exclude?(action) && !wrapped?
+ raise MissingComponentWrapper,
+ "Wrap your component in a `component_wrapper` block in order to use turbo-stream methods"
end
- def component_wrapper(method = nil, tag: "div", **kwargs, &block)
- @wrapped = true
+ if method && !action.in?(ACTIONS_WITH_METHOD)
+ raise ArgumentError, "The #{action} action does not supports a method"
+ end
- wrapper_arguments = { id: wrapper_key }.merge(kwargs)
+ OpTurbo::StreamComponent.new(
+ action:,
+ method:,
+ target: wrapper_key,
+ template:,
+ **attributes
+ ).render_in(view_context)
+ end
- if inner_html_only?
- capture(&block)
- elsif wrapper_only?
- method ? send(method, wrapper_arguments) : content_tag(tag, wrapper_arguments)
- else
- method ? send(method, wrapper_arguments, &block) : content_tag(tag, wrapper_arguments, &block)
- end
- end
+ def insert_as_turbo_stream(component:, view_context:, action: :append)
+ template = component.render_in(view_context)
- def wrapped?
- !!@wrapped
+ # The component being inserted into the target component
+ # needs wrapping, not the target since it isn't the one
+ # that needs to be rendered to perform this turbo stream action.
+ unless component.wrapped?
+ raise MissingComponentWrapper,
+ "Wrap your component in a `component_wrapper` block in order to use turbo-stream methods"
end
- def inner_html_only?
- !!@inner_html_only
- end
+ OpTurbo::StreamComponent.new(
+ action:,
+ target: insert_target_modified? ? insert_target_modifier_id : wrapper_key,
+ template:
+ ).render_in(view_context)
+ end
- def wrapper_only?
- !!@wrapper_only
- end
+ def component_wrapper(method = nil, tag: "div", **kwargs, &)
+ @wrapped = true
- def wrapper_key
- if wrapper_uniq_by.nil?
- self.class.wrapper_key
- else
- "#{self.class.wrapper_key}-#{wrapper_uniq_by}"
- end
- end
+ wrapper_arguments = { id: wrapper_key }.merge(kwargs)
- def wrapper_uniq_by
- # optionally implemented in subclass in order to make the wrapper key unique
+ if inner_html_only?
+ capture(&)
+ elsif wrapper_only?
+ method ? send(method, wrapper_arguments) : content_tag(tag, wrapper_arguments)
+ else
+ method ? send(method, wrapper_arguments, &) : content_tag(tag, wrapper_arguments, &)
end
+ end
- def insert_target_modified?
- # optionally overriden (returning true) in subclass in order to indicate thate the insert target
- # is modified and should not be the root inner html element
- # insert_target_container needs to be present on component's erb template then
- false
- end
+ def wrapped?
+ !!@wrapped
+ end
- def insert_target_container(tag: "div", class: nil, data: nil, style: nil, &block)
- unless insert_target_modified?
- raise NotImplementedError,
- "#insert_target_modified? needs to be implemented and return true if #insert_target_container is " \
- "used in this component"
- end
+ def inner_html_only?
+ !!@inner_html_only
+ end
+
+ def wrapper_only?
+ !!@wrapper_only
+ end
- content_tag(tag, id: insert_target_modifier_id, class:, data:, style:, &block)
+ def wrapper_key
+ if wrapper_uniq_by.nil?
+ self.class.wrapper_key
+ else
+ "#{self.class.wrapper_key}-#{wrapper_uniq_by}"
end
+ end
+
+ def wrapper_uniq_by
+ # optionally implemented in subclass in order to make the wrapper key unique
+ end
- def insert_target_modifier_id
- "#{wrapper_key}-insert-target-modifier"
+ def insert_target_modified?
+ # optionally overriden (returning true) in subclass in order to indicate thate the insert target
+ # is modified and should not be the root inner html element
+ # insert_target_container needs to be present on component's erb template then
+ false
+ end
+
+ def insert_target_container(tag: "div", class: nil, data: nil, style: nil, &)
+ unless insert_target_modified?
+ raise NotImplementedError,
+ "#insert_target_modified? needs to be implemented and return true if #insert_target_container is " \
+ "used in this component"
end
+
+ content_tag(tag, id: insert_target_modifier_id, class:, data:, style:, &)
+ end
+
+ def insert_target_modifier_id
+ "#{wrapper_key}-insert-target-modifier"
end
end
end