module Sequel::Plugins::DetectUnnecessaryAssociationOptions

  1. lib/sequel/plugins/detect_unnecessary_association_options.rb

The detect_unnecessary_association_options plugin can detect unnecessary association options, and either warn or raise if they are detected. This allows you to find and remove the unnecessary options. Association options are considered unnecessary if they specify the same value as Sequel’s defaults.

To detect unnecessary association options, you should load the plugin into your model base class (e.g. Sequel::Model) before loading your model classes. Then, after all models have been loaded, you can call the detect_unnecessary_association_options on each to check for unnecessary association options. Additionally, if you are calling finalize_associations, it will automatically check for unnecessary association options.

A typical usage would be to combine this with the subclasses plugin:

Sequel::Model.plugin :detect_unnecessary_association_options
Sequel::Model.plugin :subclasses
# load model classes

# implicitly check all subclasses when freezing descendants
Sequel::Model.freeze_descendants

# or, if not freezing all descendants
Sequel::Model.descendants.each(&:detect_unnecessary_association_options)

By default, the plugin warns for every unnecessary association option. To raise an error instead, you can pass the action: :raise option when loading the plugin:

Sequel::Model.plugin :detect_unnecessary_association_options, action: :raise

This plugin only detects the most common unnecessary association options, such as:

  • :class (all associations)

  • :key and :primary_key (associations without join tables)

  • :join_table, :left_key, :right_key, :left_primary_key, :right_primary_key (single join table associations)

  • :left_primary_key, :right_primary_key (*_through_many associations)

Only association types supported by default or supported by a plugin that ships with Sequel are supported by this plugin. Other association types are ignored.

Methods

Public Class

  1. configure

Public Class methods

configure(model, opts={})
[show source]
   # File lib/sequel/plugins/detect_unnecessary_association_options.rb
47 def self.configure(model, opts={})
48   model.instance_variable_set(:@detect_unnecessary_association_options_action, opts[:action] || :warn)
49 end