Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
activerecord-polytypes
Advanced tools
ActiveRecord Polytypes adds features to ActiveRecord to combinine the best of multiple inheritance, multi-table inheritance, and polymorphic relationships, without the need for schema changes. It supports PostgreSQL, MySQL and SQLite.
Install the gem and add to the application's Gemfile by executing:
$ bundle add activerecord-polytypes
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install activerecord-polytypes
Imagine you have a simple Entity model that can be either a User or an Organisation, and it has associated legal documents and a billing plan.
# An entity, is either a user or an organisation.
# It is also a container for a collection of legal documents,
# and it has an associated billing plan.
class Entity
belongs_to :billing_plan
has_many :documents
end
class User < ApplicationRecord; end
class Organisation < ApplicationRecord; end
At times you may wish to fetch all entities, and their associated documents and billing plans but then vary in behaviour or processing, based on the subtype of entity.
What are our options?
With ActiveRecordPolytypes you can easily query across all subtypes like this:
Entity::Subtype.where("user_created_at > ? OR organisation_country IN (?)", 1.day.ago, %(US)).order(billing_plan_renewal_date: :desc)
To e.g. fetch all entities that are either users created in the last day, or organisations in the US. and order by a common attribute:
=> [
#<Entity::User...
#<Entity::Organisation...
#<Entity::User...
#<Entity::User...
#<Entity::Organisation...
It constructs the needed joins so that you can query across all subtypes in a single hit, performing aggregations and filters on subtype attributes, directly in the database. The instantiated subtypes also provide an interface that combines supertype + subtype. E.g. in the above, for each:
You can also create or update supertype + subtype objects in a single hit. E.g.
Entity::User.create(
billing_plan_id: 3, # Entity attributes
user_name: # User attributes
)
Entity::User.find(1).update(
billing_plan_id: 3, # Entity attributes
user_name: # User attributes
)
You can also limit queries (which limit the joined tables) to specific subtypes when applicable. E.g. Limit to specific subtypes
Entity::User.where(user_name: "Bob")
Entity::Organisation.where(organisation_country: "US")
Vs query across all subtypes
Entity::Subtype.where(...)
All you need to do to install ActiveRecordPolytypes into an existing model, is make a call to polymorphic_supertype_of
in the model, and pass in the names of the associations that you want to act as subtypes.
It will work on any existing belongs_to
or has_one
association (respecting any non conventional foreign keys, class overrides etc.)
class Entity < ApplicationRecord
belongs_to :billing_plan
has_many :documents
belongs_to :user
belongs_to :organisation
polymorphic_supertype_of :user, :organisation
end
An entity can act as a subtype to any number of supertypes, so e.g. while Users and Organisations might act as Entities, within a billing context they might also act as SearchableItems within a search API. Inheriting from multiple supertypes is as easy as repeating the pattern above, per supertype. E.g.
class Searchable < ApplicationRecord
validates :searchable_index_string, presence: true
belongs_to :user
belongs_to :post
belongs_to :category
polymorphic_supertype_of :user, :post, :category
end
Searchable::Subtype.all # => [#<Searchable::User..., #<Searchable::Post.., #<Searchable::Category.., #<Searchable::User..]
Subtypes also inherit the relationships of their supertypes, so you can even eager or preload these. E.g.
On the parent type
# Load the user_posts relation for all users. Empty for non users
# Load the organisation_documents relation for all organisations. Empty for non organisations
assert Entity::Subtype.preload(:user_posts, :organisation_documents).all? { |s| s.user_posts.loaded? && s.organisation_documents.loaded? }
assert Entity::Subtype.eager_load(:user_posts, :organisation_documents).all? { |s| s.user_posts.loaded? && s.organisation_documents.loaded? }
On the subtype
# Load the user_posts relation for all users. Empty for non users
assert Entity::User.preload(:posts).all? { |s| s.posts.loaded? }
assert Entity::User.eager_load(:posts).all? { |s| s.posts.loaded? }
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 the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/wouterken/activerecord-polytypes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Activerecord::Mti project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that activerecord-polytypes demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.