Class: Para::ApplicationRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/para/application_record.rb

Overview

Base class for all para-specific models.

Class Method Summary collapse

Class Method Details

.with_belongs_to_optional_option_if_needed(&block) ⇒ Object

Adds the ‘optional: true` option to the belongs_to calls inside the provided block, but only for Rails 5.1+



10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/para/application_record.rb', line 10

def self.with_belongs_to_optional_option_if_needed(&block)
  belongs_to_accepts_optional = ActiveRecord::Associations::Builder::BelongsTo
                                .send(:valid_options, {})
                                .include?(:optional)

  if belongs_to_accepts_optional
    with_options(optional: true, &block)
  else
    block.call
  end
end