No commit activity in last 3 years
No release in over 3 years
Backport ActiveModel::Attributes -like method
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.13
~> 10.0
~> 3.0

Runtime

<= 5.2.0, >= 4.2.0
 Project Readme

ActiveModelAttributes

Make .attribute in ActiveModel for < Rails 5.2.

Deprecation Warning

This is no longer needed on Rails5.2+.

Installation

Add this line to your application's Gemfile:

gem 'active_model_attributes_backport'

Usage

Define .attribute to cast value.

class User
  include ActiveModelAttributes

  attribute :id, :integer
  attribute :no_cast
  attribute :user_id, :integer, default: -> { Current.user.id }
  attribute :locale, :string, default: 'ja'
end

user = User.new(id: '1')

user.id == 1
user.user_id == Current.user.id
user.locale == 'ja'
user.no_cast.nil?
# Lookup type from `ActiveModel::Type.lookup(type)` 
# ActiveModel::Type.registry.send(:registrations).map { |type| type.send(:name) }

:big_integer
:binary
:boolean
:date
:datetime
:decimal
:float
:integer
:string
:text
:time

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/alpaca-tc/active_model_attributes.

run spec

bundle exec appraisal install
bundle exec appraisal 4.2-stable rspec
bundle exec appraisal 5.0-stable rspec
bundle exec appraisal 5.1-stable rspec