Sha256: 0afb1ebf7ff9e115a4e30f1d77a9f58f1e7e3a2aedbb9943b41a9403fc62daa9

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

class StimulusReflex::Reflex
  attr_reader :channel, :url, :element, :selectors

  delegate :connection, to: :channel
  delegate :session, to: :request

  def initialize(channel, url: nil, element: nil, selectors: [])
    @channel = channel
    @url = url
    @element = element
    @selectors = selectors
  end

  def request
    @request ||= begin
      uri = URI.parse(url)
      path = ActionDispatch::Journey::Router::Utils.normalize_path(uri.path)
      path_params = Rails.application.routes.recognize_path(path)
      query_hash = Rack::Utils.parse_nested_query(uri.query)
      ActionDispatch::Request.new(
        connection.env.merge(
          Rack::MockRequest.env_for(uri.to_s).merge(
            "rack.request.query_hash" => query_hash,
            "rack.request.query_string" => uri.query,
            "ORIGINAL_SCRIPT_NAME" => "",
            "ORIGINAL_FULLPATH" => path,
            Rack::SCRIPT_NAME => "",
            Rack::PATH_INFO => path,
            Rack::REQUEST_PATH => path,
            Rack::QUERY_STRING => uri.query,
            ActionDispatch::Http::Parameters::PARAMETERS_KEY => path_params
          )
        )
      ).tap { |req| req.session.send :load! }
    end
  end

  def url_params
    @url_params ||= Rails.application.routes.recognize_path_with_request(request, request.path, request.env[:extras] || {})
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stimulus_reflex-3.1.3 lib/stimulus_reflex/reflex.rb
stimulus_reflex-3.1.2 lib/stimulus_reflex/reflex.rb
stimulus_reflex-3.0.0 lib/stimulus_reflex/reflex.rb
stimulus_reflex-2.2.3 lib/stimulus_reflex/reflex.rb