has_integrations
Rails gem for a pattern to manage 3rd party service integrations
Install
gem 'has_integrations'or
gem install has_integrationsOnce the gem is installed, in your app, run
rails generate has_integrations:installWhich will set up the Integration Model and a migration for the Integration model.
Usage
class User < ApplicationRecord
# ...
has_many :integrations,
dependent: :destroy, extend: HasIntegrations,
as: :owner
# ...
endWorking with Integrations
# Get Stripe Integration.
stripe_integration = user.integrations[:stripe]
# Set Stripe Integration.
# This will create and save a new integration
# if there wasn't already a stripe integration record.
user.integrations[:stripe] = { "livemode" => false, "stripe_user_id" => 1 }
# Get the stored config.
user.integrations[:stripe].config
# => { "livemode" => false, "stripe_user_id" => 1 }
# Check if an Integration exists.
user.integrations.has?(:stripe)
# => true or falseThis gem assumes each owning object can only have one type of each integration.
Configuration
In config/initializers/has_integrations.rb:
HasIntegrations.encryption_key = 'my great key'Used for attr_encrypted on the config column
Validations
With the built in Integration class, you can monkey patch to add validations / whatever other logic.
TODO
- Allow the generate to be passed an existing
Integrationclass. This may be a weird config option, because theIntegrationclass needskind,encrypted_config,owner_id, andowner_type-- would all these columns need to be configurable?
Contributing
- Fork
- Open a PR
- :-)