Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

polutan

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

polutan

  • 1.0.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Polutan

The polutan gem offers Rails developers a way to gather attributes on an unsaved model across multiple requests. It's perfect for StimulusReflex users that are building faceted search interfaces, as well as Optimism users looking to implement real-time, per-attribute validation schemes.

Try a demo, here: 👉 Under Line StimulusReflex 👈

GitHub stars GitHub forks

Why use Polutan?

Many reactive UI concepts are a pain in the ass to implement using the classic Rails request/response pattern, which was created at a time before developers started using Ajax to update portions of a page. ActionController is designed to mutate state in response to form submissions, leading to abuse of the session object and awkward hacks to validate and persist models across multiple requests.

Polutan presents a flexible and lightweight mechanism to refine a model that persists its attributes across multiple updates, and even multiple servers.

Is Polutan for you?

Do you ever find yourself:

  • building complex search interfaces
  • creating multi-stage data entry processes
  • frustrated by the limitations of classic form submission
  • wanting to save data even if the model is currently invalid
  • reinventing the wheel every time you need field validation

If you answered yes to any of the above... you are every Rails developer, and you're not crazy. This functionality has been a blind-spot in the framework for a long time.

Yes, Polutan is for you.

Key features and advantages

  • A natural fit with StimulusReflex and Stimulus
  • No reliance on sessions, so it works across servers
  • Easy to learn, quick to implement
  • Supports model attributes, validations and errors
  • No need to mess around with temporary records

How does Polutan work?

First, set up an Polutan class that defines some attributes. Your class will inherit from Possibility, which is aptly-named.

class ExampleModel < Possibility
  attribute :name, :string
  attribute :age, :integer, default: 21
end

Then create an instance and assign it to an instance variable in the controller responsible for your initial page load:

class ExampleController < ApplicationController
  def index
    @af = ExampleModel.new
  end
end

Emit the instance id as a data attribute on every element which can update your model:

Name: <input type="text" data-af="<%= @af.id %>" data-reflex="input->Example#name" /><br/>
Age: <input type="text" data-af="<%= @af.id %>" data-reflex="input->Example#age" placeholder="<%= @id.age %>" />

Since all attributes are gathered and sent to the server during a Reflex operation, it's easy to retrieve the instance id from the Reflex element accessor and use it to call up the correct Polutan object and make changes to it:

class ExampleReflex < ApplicationReflex
  def name
    model = ExampleModel.find(element.dataset.af)
    model[:name] = element.value
  end
  
  def age
    model = ExampleModel.find(element.dataset.af)
    model[:age] = element.value
  end
end

The current state of the attributes is persisted every time you set the value of an attribute using bracket notation. You can use standard setter assignments, but the model state will not be persisted until you manually call save:

model[:name] = "Helen" # saved
model.name = "Helen" # not saved
model.save # saved

{% hint style="warning" %} Polutan class attributes are persisted in Redis via the excellent Kredis gem, which must be set up and running in your project before you can use Polutan. {% endhint %}

Polutan is based on Active Entity. It is similar to using ActiveModel::Model, except that it has full support for Attributes, including arrays and nested attributes. Polutan classes behave like ActiveModel classes, so you can inspect valid? and the errors accessor.

class ExampleModel < Possibility
  attribute :name, :string
  validates :name, presence: true
end

model = ExampleModel.new
model.valid? # false
model.errors # @errors=[#<ActiveModel::Error attribute=name, type=blank, options={}>]

{% hint style="info" %} Unlike an ActiveRecord model, Polutan instances can persist their attributes even if the attributes are currently invalid. This design allows you to resolve any errors present, even if it takes several distinct operations to do so. {% endhint %}

{% hint style="success" %} Once the state of your attributes is valid, you can pass the attributes from your Polutan model right into the constructor of a real ActiveRecord model. It should work perfectly. {% endhint %}

FAQs

Package last updated on 19 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc