npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rubyonjets/ujs-compat

v1.2.0

Published

UJS compat library with @rails/ujs library for Ruby on Jets and API Gateway

Downloads

1,704

Readme

@rubyonjets/ujs-compat

UJS compat library for Ruby on Jets and API Gateway.

Overview

The @rails/ujs package adds unobtrusive JavaScript scripting support for Rails. This is typically done with:

yarn add @rails/ujs

Call rails ujs code:

app/javascript/packs/application.js

import Rails from "@rails/ujs"
Rails.start()

How Rails UJS Works

  1. The @rails/ujs package registers click handlers to links with the data-delete attribute. Example: <a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/posts/123">Destroy</a>
  2. When the link is clicked, @rails/ujs creates a hidden form and submits the request as a POST request with a hidden field _method=delete.
  3. The Rack::MethodOverride middleware is used to then adjust POST to DELETE request appropriately.

Essentially, a DELETE http request is mimiced using a POST http request. It all happens transparently, so developers don't really notice the magic happening behind the scenes.

The Issue

Unfortunately API Gateway REST APIs require actual DELETE requests to the resource, not mimiced POST requests. Example:

DELETE /posts/123

Sending a POST /posts/123 with a _method=delete will not work with API Gateway because the POST /posts/123 route points to the posts#update method API Gateway.

The Fix

To fix the issue, this javascript library adds click handlers that intercept @rails/ujs handlers and sends an AJAX DELETE request correctly.

Usage

yarn add @rubyonjets/ujs-compat

And then add the Jets ujs-compat code below the Rails ujs code.

app/javascript/packs/application.js

import jquery from 'jquery'
window.$ = jquery
import Jets from "@rubyonjets/ujs-compat"
Jets.start()

Dependencies

This package is dependencies on jquery. You should configure your own jquery.

Thoughts and Considerations

  • Considered putting POST /posts/123 to the posts#delete action instead of posts#update. But that feels like unexpected behavior.
  • Considered adding logic at the lambda handlers processing logic to convert posts#update actions with _method=delete to call posts#delete. This will make the logic closer to what Rack::MethodOverride does but again feels a little bit confusing.
  • So went with javascript library to handle things earlier. An actual DELETE request is sent via AJAX to API Gateway.
  • Open to suggestions and improvements.