All Projects → freeletics → array_enum

freeletics / array_enum

Licence: MIT License
String to integer mapping for PostgreSQL array columns

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to array enum

models stats
Charts for your rails models with MetricsGraphics.js and NVD3
Stars: ✭ 43 (+53.57%)
Mutual labels:  activerecord
datetime-scopes
A plugin for ActiveRecord which adds very helpful methods for querying records over time ranges (weeks/days/months/years), and it also respects time-zones!
Stars: ✭ 14 (-50%)
Mutual labels:  activerecord
rails-microservices-book
A guide to building distributed Ruby on Rails applications using Protocol Buffers, NATS and RabbitMQ
Stars: ✭ 23 (-17.86%)
Mutual labels:  activerecord
computed model
Batch loader with dependency resolution and computed fields
Stars: ✭ 22 (-21.43%)
Mutual labels:  activerecord
SimpleCurd
2个类,实现类ActiveRecord,无需写Mapper, mybatis增强
Stars: ✭ 14 (-50%)
Mutual labels:  activerecord
ar-check
Enable PostgreSQL's CHECK constraints on ActiveRecord migrations
Stars: ✭ 17 (-39.29%)
Mutual labels:  activerecord
mv-core
Migration Validators project core classes
Stars: ✭ 23 (-17.86%)
Mutual labels:  activerecord
timeliness-i18n
Translations for timeliness and validates_timeliness gem.
Stars: ✭ 16 (-42.86%)
Mutual labels:  activerecord
active-persistence
Active Persistence is a implementation of Active Record Query Interface for JPA that makes it easy and fun.
Stars: ✭ 14 (-50%)
Mutual labels:  activerecord
active model serializers validator
🃏 An extension to ActiveModel::Serializer that validates serializers output against a JSON schema
Stars: ✭ 18 (-35.71%)
Mutual labels:  activerecord
rails-countries
Integration between Rails and countries gem.
Stars: ✭ 17 (-39.29%)
Mutual labels:  activerecord
filedb
ActiveRecord for static data definitions based on files
Stars: ✭ 72 (+157.14%)
Mutual labels:  activerecord
idy
👓 An ID obfuscator for ActiveRecord
Stars: ✭ 15 (-46.43%)
Mutual labels:  activerecord
wp-activerecord
An ActiveRecord implementation for WordPress
Stars: ✭ 19 (-32.14%)
Mutual labels:  activerecord
normalize attributes
Sometimes you want to normalize data before saving it to the database like down casing e-mails, removing spaces and so on. This Rails plugin allows you to do so in a simple way.
Stars: ✭ 41 (+46.43%)
Mutual labels:  activerecord
ar-dynattribute
Provide ActiveRecord dynamic attributes stored into the single field in serialized state
Stars: ✭ 43 (+53.57%)
Mutual labels:  activerecord
filtered
Filters ActiveRecord queries in a nice way
Stars: ✭ 28 (+0%)
Mutual labels:  activerecord
ar-ondemand
Fast access to database results without the memory overhead of ActiveRecord objects
Stars: ✭ 37 (+32.14%)
Mutual labels:  activerecord
wharel
Arel made simple
Stars: ✭ 95 (+239.29%)
Mutual labels:  activerecord
LocalSupport
A directory of local support services and volunteer opportunities
Stars: ✭ 60 (+114.29%)
Mutual labels:  activerecord

Gem Version BUILD

ArrayEnum

Extension for ActiveRecord that adds support for PostgreSQL array columns, mapping string values to integers.

Installation

gem install array_enum or use Gemfile with bundler

Usage

ActiveRecord extension

Database will store integers that after reading will map to string values.

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.integer :favourite_colors, array: true, null: false, default: []
  end
end

class User < ActiveRecord::Base
  extend ArrayEnum

  array_enum favourite_colors: {"red" => 1, "blue" => 2, "green" => 3}
end

user = User.create!(favourite_colors: ["red", "green"])
user.favourite_colors # => ["red", "green"]
User.favourite_colors # => {"red" => 1, "blue" => 2, "green" => 3}

Several scopes are made available on your model to find records based on a value or an array of values:

user1 = User.create!(favourite_colors: ["red", "green"])
user2 = User.create!(favourite_colors: ["red"])

# Find a record that has _all_ the provided values in the array enum attribute
User.with_favourite_colors("red") # => [user1, user2]
User.with_favourite_colors(%w[red green]) # => [user1]
User.with_favourite_colors(%w[red blue]) # => []
User.with_favourite_colors(%w[green blue]) # => []

# Find a record that has the provided values, and _only those values_, in the array enum attribute
User.only_with_favourite_colors("red") # => [user2]
User.only_with_favourite_colors(%w[red green]) # => [user1]
User.only_with_favourite_colors(%w[red blue]) # => []
User.only_with_favourite_colors(%w[green blue]) # => []

# Find a record that has _at least one_ of the provided values in the array enum attribute
User.with_any_of_favourite_colors("red") # => [user1, user2]
User.with_any_of_favourite_colors(%w[red green]) # => [user1, user2]
User.with_any_of_favourite_colors(%w[red blue]) # => [user1, user2]
User.with_any_of_favourite_colors(%w[green blue]) # => [user1]

Attempting to find a record with a value that is not in the enum will fail:

User.with_favourite_colors("yellow") # => ArgumentError["yellow is not a valid value for favourite_colors"]

Subset Validator

Additionally subset validator is provided that can help to ensure correct values are passed during validation.

class CreateUser
  include ActiveModel::Model

  attr_accessor :favourite_colors

  validates :favourite_colors, subset: ["green", "blue"]
  # or:
  # validates :favourite_colors, subset: { in: ->(record) { Color.pluck(:name) } }
end

CreateUser.new(favourite_colors: ["black"]).valid? # => false

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/freeletics/array_enum.

License

The gem is available as open source under the terms of the MIT License.

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].