All Projects → fnando → defaults

fnando / defaults

Licence: MIT license
Add default value for ActiveRecord attributes

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to defaults

activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (+31.25%)
Mutual labels:  activerecord
activeentity
Active Record without Database
Stars: ✭ 46 (+187.5%)
Mutual labels:  activerecord
u-observers
Simple and powerful implementation of the observer pattern.
Stars: ✭ 31 (+93.75%)
Mutual labels:  activerecord
index shotgun
duplicate index checker 🔥 🔫 👮
Stars: ✭ 35 (+118.75%)
Mutual labels:  activerecord
scopy
Common ActiveRecord scopes as model concerns
Stars: ✭ 13 (-18.75%)
Mutual labels:  activerecord
active record distinct on
Support for `DISTINCT ON` statements when querying with ActiveRecord
Stars: ✭ 23 (+43.75%)
Mutual labels:  activerecord
sinatras-skeleton
Basic Sinatra Skeleton MVC CRUD App with Sprockets, Warden, ActiveRecord and PostgresQL
Stars: ✭ 13 (-18.75%)
Mutual labels:  activerecord
ActiveRecord
New Active Record for KumbiaPHP
Stars: ✭ 23 (+43.75%)
Mutual labels:  activerecord
squint
Search PostgreSQL jsonb and hstore columns
Stars: ✭ 26 (+62.5%)
Mutual labels:  activerecord
ar-role
ActiveRecord behavior, which provides relation roles (table inheritance)
Stars: ✭ 34 (+112.5%)
Mutual labels:  activerecord
microscope
🔬 Microscope adds useful scopes targeting ActiveRecord boolean, date and datetime fields.
Stars: ✭ 52 (+225%)
Mutual labels:  activerecord
activerecord-cockroachdb-adapter
CockroachDB adapter for ActiveRecord.
Stars: ✭ 90 (+462.5%)
Mutual labels:  activerecord
yii2-rest-client
REST client (AR-like model) for Yii Framework 2.0 (via yii2-http-client)
Stars: ✭ 19 (+18.75%)
Mutual labels:  activerecord
nxt state machine
A simple but powerful state machine implementation.
Stars: ✭ 14 (-12.5%)
Mutual labels:  activerecord
workflow-activerecord
ActiveRecord/Rails Integration for the Workflow library
Stars: ✭ 24 (+50%)
Mutual labels:  activerecord
paper trail-association tracking
Plugin for the PaperTrail gem to track and reify associations
Stars: ✭ 103 (+543.75%)
Mutual labels:  activerecord
siphon
Siphon enables you to easily combine your scopes with params
Stars: ✭ 23 (+43.75%)
Mutual labels:  activerecord
AlamoRecord
An elegant yet powerful iOS networking layer inspired by ActiveRecord.
Stars: ✭ 18 (+12.5%)
Mutual labels:  activerecord
ar-variation
Variation behavior for ActiveRecord
Stars: ✭ 46 (+187.5%)
Mutual labels:  activerecord
activerecord-shard for
Database Sharding Library for ActiveRecord
Stars: ✭ 16 (+0%)
Mutual labels:  activerecord

Defaults

Tests Gem Gem

Instalation

Add this line to your application's Gemfile:

gem "defaults"

And then execute:

$ bundle

Or install it yourself as:

$ gem install defaults

Usage

Here are the rules:

  1. Assigned attributes have precedence over default values
  2. Default values have precedence over database defaults.

Add the method call defaults to your model.

class Page < ActiveRecord::Base
  defaults title: "New page",
           body: "Put your text here"
end

Attributes will be set only if it's a new record and the attribute is blank.

Retrieve the default attribute with the default_for instance method:

@page.default_for(:title)

You can pass callables (any objects that respond to .call() or .call(record)) as attribute:

class Expiration
  def self.call
    Time.now
  end
end

class Checksum
  def self.call(user)
    Digest::SHA1.hexdigest(user.name.to_s, user.email.to_s)
  end
end

class User < ApplicationRecord
  defaults checksum: Checksum,
           expires_at: -> { Time.now }
end

You can override the default attributes as follow:

Page.default_values = {
  title: "Here's your new page",
  body: "Write your page text"
}

Maintainer

Contributors

Contributing

For more details about how to contribute, please read https://github.com/fnando/defaults/blob/main/CONTRIBUTING.md.

License

The gem is available as open source under the terms of the MIT License. A copy of the license can be found at https://github.com/fnando/defaults/blob/main/LICENSE.md.

Code of Conduct

Everyone interacting in the defaults project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].