Coder Social home page Coder Social logo

rails-sti-enum's Introduction

Rails STI using Rails Enums

Having used Rails STI many times, I came to wonder if it was possible (not necessarily feasible or in any way sane) to use Rails Enum instead of a simple String field for STI type.

It seems it is, although with a tiny tweak to set the type on creation :)

Drawbacks: Manually edit Enum values to add new class.

Note: Should explicitly map enum values' integer mapping to avoid disaster when adding/removing types!

Initial Ruby and Rails versions

mkdir rails-sti-enum
cd rails-sti-enum
git init

echo "2.3.0" > .ruby-version

cat > Gemfile <<-EOF
source 'https://rubygems.org'
ruby '2.3.0'

gem 'rails', '>= 5.0.0.rc1', '< 5.2'
EOF
git add .ruby-version Gemfile
git commit -m "initial commit"

bundle install
git add Gemfile.lock
git commit -m "initial gems"

Create the Rails application

bundle exec rails new .
git add .
git commit -m "rails new"

Create a model to play with

bin/rails g model Animal name type:integer
bin/rails db:migrate

Create a few other models;

mkdir app/models/animal
cat > app/models/animal/cat.rb <<-EOF
class Animal::Cat < Animal
end
EOF

cat > app/models/animal/dog.rb <<-EOF
class Animal::Dog < Animal
end
EOF

Enable Enums as STI type in app/models/animal.rb

class Animal < ApplicationRecord
  before_create :set_type
  enum type: { 'Animal::Cat' => 0, 'Animal::Dog' => 1 }

  private

  # Unfortunately type is not set by rails...
  def set_type
    self.type = self.class.name
  end
end

Check it works

bin/rails c

Animal::Cat.create name: 'Meow'
# => #<Animal::Cat id: 1, name: "Meow", type: "Animal::Cat", created_at: "2016-06-09 17:51:51", updated_at: "2016-06-09 17:51:51">

Animal::Dog.create name: 'Woof'
# => #<Animal::Dog id: 2, name: "Woof", type: "Animal::Dog", created_at: "2016-06-09 17:51:54", updated_at: "2016-06-09 17:51:54">

Animal.create
# => ArgumentError: 'Animal' is not a valid type

Animal.first
# => #<Animal::Cat id: 1, name: "Meow", type: "Animal::Cat", created_at: "2016-06-09 17:51:51", updated_at: "2016-06-09 17:51:51">

rails-sti-enum's People

Contributors

lhoeg avatar

Stargazers

Takeo Yoshida avatar Dovi Winberger avatar

Watchers

 avatar

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.