Sha256: 7d627f2a7ec81efad5e575d3805b7f5e729676fc2146dc58a5184e78dc653f15
Contents?: true
Size: 1.96 KB
Versions: 4
Compression:
Stored size: 1.96 KB
Contents
# -*- encoding: utf-8 -*- # -*- frozen_string_literal: true -*- # -*- warn_indent: true -*- # The +UnitMeasurements::Rails::ActiveRecord::Density+ module provides a convenient # way to define density-measured attributes in +ActiveRecord+ models. # # It acts as a wrapper for the +measured+ method, simplifying the definition of # density-measured attributes without directly invoking the +measured+ method. # # @author {Harshal V. Ladhe}[https://shivam091.github.io/] # @since 1.3.0 module UnitMeasurements::Rails::ActiveRecord::Density # @!scope class # Defines _density-measured_ attributes in the +ActiveRecord+ model. # # This method serves as a wrapper around the +measured+ method and allows easy # definition of density-measured attributes by accepting an array of attribute # names along with their options. # # @param [Array<String|Symbol>] measured_attrs # An array of the names of density-measured attributes. # @param [Hash] options A customizable set of options # @option options [String|Symbol] :quantity_attribute_name The name of the quantity attribute. # @option options [String|Symbol] :unit_attribute_name The name of the unit attribute. # # @example Define single density-measured attribute: # class Substance < ActiveRecord::Base # measured_density :total_density # end # # @example Define multiple density-measured attributes: # class Substance < ActiveRecord::Base # measured_density :internal_density, :external_density # end # # @return [void] # # @see .measured # @author {Harshal V. Ladhe}[https://shivam091.github.io/] # @since 1.3.0 def measured_density(*measured_attrs, **options) measured(UnitMeasurements::Density, *measured_attrs, **options) end end # ActiveSupport hook to extend ActiveRecord with the `UnitMeasurements::Rails::ActiveRecord::Density` module. ActiveSupport.on_load(:active_record) do ::ActiveRecord::Base.send(:extend, UnitMeasurements::Rails::ActiveRecord::Density) end
Version data entries
4 entries across 4 versions & 1 rubygems