No release in over a year
ArrayAttributes - Rails Concern to encapsulate common array attributes methods
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.2, < 7.1
 Project Readme

ArrayAttributes

A Rails plugin to encapsulate common array attributes methods.

This gem can be used to avoid repeating the same methods in models when working with arrays.

Support

This branch targets Ruby 2.4+ only

Usage

Install gem:

Add this line to your Gemfile:

gem 'array_attributes'

then run:

bundle install

Enable ArrayAttributes

class Book < ActiveRecord::Base
  string_array_attributes [:authors, :countries]
end

It assumes you are passing string attributes.

It also supports the following options:

  • :reject_blank: whether the attribute should skip blank values
  • :reject_if: condition to reject values
class Device < ActiveRecord::Base
  string_array_attributes [:ips], reject_blank: true, reject_if: ->(value) { value == '127.0.0.1' }
end

attribute_raw

You can invoke attribute_raw to get the array values joined by commas.

book = Book.create(authors: ['John Doe', 'Jane Doe'])
book.authors_raw #=> "John Doe, Jane Doe"

attribute_raw=

You can invoke attribute_raw= to set the array value by entering the values split by commas.

book = Book.new
book.authors_raw="John Doe, Jane Doe"
book.save!
book.authors #=> ["John Doe", "Jane Doe"]

See LICENSE.