lib/stimulus_reflex/reflex.rb in stimulus_reflex-3.5.0.rc3 vs lib/stimulus_reflex/reflex.rb in stimulus_reflex-3.5.0.rc4

- old
+ new

@@ -1,68 +1,53 @@ # frozen_string_literal: true require "stimulus_reflex/cable_readiness" require "stimulus_reflex/version_checker" -# TODO remove xpath_controller and xpath_element for v4 -ClientAttributes = Struct.new( - :id, - :tab_id, - :reflex_controller, - :xpath_controller, - :xpath_element, - :permanent_attribute_name, - :version, - :npm_version, - :suppress_logging, - keyword_init: true -) - class StimulusReflex::Reflex prepend StimulusReflex::CableReadiness include StimulusReflex::VersionChecker include StimulusReflex::Callbacks include ActiveSupport::Rescuable include ActionView::Helpers::TagHelper include CableReady::Identifiable attr_accessor :payload, :headers - attr_reader :channel, :url, :element, :selectors, :method_name, :broadcaster, :client_attributes, :logger + attr_reader :channel, :reflex_data, :broadcaster - alias_method :action_name, :method_name # for compatibility with controller libraries like Pundit that expect an action name - delegate :connection, :stream_name, to: :channel delegate :controller_class, :flash, :session, to: :request delegate :broadcast, :broadcast_halt, :broadcast_forbid, :broadcast_error, to: :broadcaster + # TODO remove xpath_controller and xpath_element for v4 - delegate :id, :tab_id, :reflex_controller, :xpath_controller, :xpath_element, :permanent_attribute_name, :version, :npm_version, :suppress_logging, to: :client_attributes + delegate :url, :element, :selectors, :method_name, :id, :tab_id, :reflex_controller, :xpath_controller, :xpath_element, :permanent_attribute_name, :version, :npm_version, :suppress_logging, to: :reflex_data + # END TODO: remove - def initialize(channel, url: nil, element: nil, selectors: [], method_name: nil, params: {}, client_attributes: {}) + alias_method :action_name, :method_name # for compatibility with controller libraries like Pundit that expect an action name + alias_method :data, :reflex_data + + def initialize(channel, reflex_data:) @channel = channel - @url = url - @element = element - @selectors = selectors - @method_name = method_name - @params = params - @client_attributes = ClientAttributes.new(client_attributes) + @reflex_data = reflex_data @broadcaster = StimulusReflex::PageBroadcaster.new(self) - @logger = suppress_logging ? nil : StimulusReflex::Logger.new(self) @payload = {} @headers = {} check_version! - - self.params end # TODO: remove this for v4 def reflex_id warn "Deprecation warning: reflex_id will be removed in v4. Use id instead!" if Rails.env.development? id end # END TODO: remove + def logger + @logger ||= StimulusReflex::Logger.new(self) unless suppress_logging + end + def request @request ||= begin uri = URI.parse(url) path = ActionDispatch::Journey::Router::Utils.normalize_path(uri.path) query_hash = Rack::Utils.parse_nested_query(uri.query) @@ -89,10 +74,10 @@ end req = ActionDispatch::Request.new(env) # fetch path params (controller, action, ...) and apply them - request_params = StimulusReflex::RequestParameters.new(params: @params, req: req, url: url) + request_params = StimulusReflex::RequestParameters.new(params: reflex_data.params, req: req, url: url) req = request_params.apply! req end end