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

persisted_attributes

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

persisted_attributes

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

PersistedAttributes

PersistedAttributes allows to add persisted attributes to ActiveRecord models without the need for a migration.

You might want to add a temporary attribute, for monitoring or reporting purposes, or avoid to pollute your domain tables with marketing-related fields.

With PersistedAttributes, this is as easy as the following:

  class User
    include PersistedAttributes::ActiveRecord

    has_persisted_attribute :subscribe_to_newsletter, field_type: :boolean
  end

  user = User.create(subscribe_to_newsletter: true)
  user.subscribe_to_newsletter # => true

The declared attribute works seamlessly like a traditionnal ActiveRecord attribute. This means you can use features such as validations without any futher work:

class Post
  include PersistedAttributes::ActiveRecord

  has_persisted_attribute :title

  validates :title, presence: true
end

post = Post.new
post.valid? # => false
post.errors.full_messages # => ["Title can't be blank"]

Installation

Add this line to your application's Gemfile:

gem "persisted_attributes"

And then execute:

$ bundle

Or install it yourself as:

$ gem install persisted_attributes

Usage

Persisted attributes are backed by the persisted_attributes_attributes table. After installing the gem run migrations to add the required tables:

rails persisted_attributes:install:migrations

Once the migrations are done, start adding attributes to model by including the PersistedAttributes::ActiveRecord module any active record model:

class Post
  include PersistedAttributes::ActiveRecord
end

And voilà! Your model is ready to declare new persisted attributes. The API is as simple as:

class Post
  include PersistedAttributes::ActiveRecord

  # Use `has_persisted_attribute` with the field name to declare a new attribute.
  has_persisted_attribute :title

  # Default type for a field is :string. Use the `field_type` option to declare another field type
  has_persisted_attribute :reading_time, field_type: :integer
  has_persisted_attribute :published, field_type: :boolean
end

For now, PersistedAttributes allow for three field types;

  • String (default)
  • Integer
  • Boolean

The list will likely grow with time, usage and needs.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 01 Aug 2024

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