No commit activity in last 3 years
No release in over 3 years
This 'acts_as' extension provides the capability for having a default object. The class that has this specified needs to have a default column defined as an integer, string or boolean on the mapped database table
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

ActsAsDefaultable

Build Status Code Climate Gem Version Dependency Status

Description

This acts_as extension provides the capability for having a default object. The class that has this specified needs to have a default column defined as an integer, string or boolean on the mapped database table. There is also optional support for each record to have a default object of another model.

Install

In your Gemfile

  gem 'acts_as_defaultable'

Currently is tested on rails3.2, ruby 1.9.3 and mysql

Example

Default object

  class Country < ActiveRecord::Base
    acts_as_defaultable :default
  end

  gr = Country.new
  gr.default = true
  gr.save
  Country.default == gr # => true
  uk = Country.new
  uk.default = true
  uk.save
  Country.default == gr # => false

If no options provided default column is assumed.

Default object for each record

  class User < ActiveRecord::Base
    acts_as_defaultable :default, relation: :post
  end

  class Post < ActiveRecord::Base
    after_destroy User.new
  end

  class Comment < ActiveRecord::Base
  end

  user = User.new
  post = Post.create
  user.default_post = post
  user.save
  user.default_post == post # => true

  post.destroy
  user.reload
  user.default_post == nil # => true

  user.default_post = Comment.create
  user.default_post == nil # => true

Only an object of the relation class will be accepted. The after_destroy callback is optional but will ensure that results stay consistent in the event that a newly created post gets the same id as the one destroyed

Notes

The option to acts_as_defaultable is the column of the Model that defines default behaviour. This column can be a string, a boolean or an integer with default positive values 'on', true, 1 and default negative values 'off', false, 0 respectively.

The column name for a default relation is default_column _ relation_column and can be either a string or an integer.

Examples:

  class User < ActiveRecord::Base
    acts_as_defaultable :default, relation: :post
  end

Column name in users table must be default_post

  class User < ActiveRecord::Base
    acts_as_defaultable :unique, relation: :post
  end

Column name must be unique_post

License

acts_as_defaultable is released under the MIT License