Coder Social home page Coder Social logo

has_defaults's Introduction

has_defaults Build Status

Default values for ActiveRecord models.

Installation

In your Gemfile

gem 'has_defaults'

Now run

bundle install

Usage

Add the method call has_defaults to your model.

class Page < ActiveRecord::Base
  has_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 Proc as attribute:

has_defaults :expires_at => proc { Time.now }

You can override the default attributes as follow:

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

Development

There are tests in spec. We only accept PRs with tests. To run tests:

  • Install Ruby 2.7.4
  • Create a local test database has_defaults_test in both MySQL and PostgreSQL
  • Copy spec/support/database.sample.yml to spec/support/database.yml and enter your local credentials for the test databases
  • Install development dependencies using bundle install
  • Run tests using bundle exec rspec

We recommend to test large changes against multiple versions of Ruby and multiple dependency sets. We provide some rake tasks to help with this:

  • Install development dependencies using rake matrix:install
  • Run tests using rake matrix:spec

Note that we have configured Github Actions to automatically run tests in all supported Ruby versions and dependency sets after each push. We will only merge pull requests with successful test runs.

Credits

has_defaults's People

Contributors

brunosedler avatar codener avatar dastra-mak avatar denzelem avatar fnando avatar kraatob avatar kratob avatar nhasselmeyer avatar stemps avatar triskweline avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

has_defaults's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.