0.01
The project is in a healthy, maintained state
ActiveRecord extension which allows us to safely use polymorphic associations, by validatingwhich classes are allowed to be related to, while also adding scopes and helper methods.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 5.2, < 8.0
 Project Readme

Gem Version Ruby Maintainability Test Coverage

SafePolymorphic

The best way to keep your polymorphic relationships safe.

An ActiveRecord extension for polymorphic associations.

  • Simple. It is as easy to use as it is to update.
  • Safe. It helps you avoid unintended usage of polymorphic classes.
  • Powerful. It packages some helpful scopes and helper methods.
Built by GoGrow

Credits: Improve ActiveRecord Polymorphic Associations.

Install

Install the gem and add to the application's Gemfile by executing:

$ bundle add safe_polymorphic

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install safe_polymorphic

Usage

In your model you can do the following:

class Address < ActiveRecord::Base
    belongs_to :addressabel, polymorphic: [User, Company]
end

Define a belongs_to relatinoship and instead of just adding the option polymorphic: true, we are able to specify the allowed polymorphic classes.

By using this, we will create a polymorphic relationship in Address called addressable which can only be a User or a Company. If we try to set an addressable from a class rather than the aforementioend ones, it will return the following error:

#ActiveRecord::RecordInvalid: Validation failed: Addressable type OtherThing class is not an allowed class.

We can also use strings and symbols instead of the classes themselves:

class Address < ActiveRecord::Base
    belongs_to :addressable, polymorphic: [:user, 'Company']
end

Provided that the strings and symbols translate to existing classes when used with .classify.constantize.

Usage with namespaced models

class Address < ActiveRecord::Base
    belongs_to :addressable, polymorphic: [Company::User, Company]
end

Helper methods

Class methods:

  • Address.addressable_types: returns the allowed classes
  • Address.with_addressable(#{type}): generic finder method
  • Address.with_addressable_#{allowed_class_name}: scope for each allowed class

Instance methods:

  • Address.addressable_type_#{allowed_class_name}?: check if it is from that specific class

Helper methods with namespaced models

Class methods:

  • Address.with_addressable(Company::User)
  • Address.with_addressable_company_user
  • Address.addressable_type_company_user?

I18n

Safe Polymoprhic uses I18n to define the not allowed class error. To customize it, you can set up your locale file:

en:
  safe_polymoprhic:
    errors:
      messages:
        class_not_allowed: "%{class} is not an allowed class"

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec 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 the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.