Sha256: 86089e95724d373b117feda0bbd63acd89da10c86dc18f032c92ca73b6a012d6

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true
require 'pp'

module Cookbook
  # Cookbook::Use is basically a linking table with extra information.
  class Use < ApplicationRecord
    attr_accessor(:used_in_tables)

    # Relationships
    belongs_to :use_in, polymorphic: true, inverse_of: :uses
    belongs_to :use_of, polymorphic: true, inverse_of: :uses

    class << self
      def add_use_of(table_sym)
        singular = table_sym.to_s.singularize
        model_name = singular.classify.constantize.to_s
        belongs_to singular.to_sym, class_name: model_name, foreign_key: :use_of_id, inverse_of: :uses, optional: true
      end

      def add_use_in(table_sym)
        singular = table_sym.to_s.singularize
        model_name = singular.classify.constantize.to_s
        belongs_to singular.to_sym, class_name: model_name, foreign_key: :use_in_id, inverse_of: :uses, optional: true
      end
    end

    # Methods
    def object_label
      return use_of.name if use_of.respond_to?(:name)
      return use_of.title if use_of.respond_to?(:title)

      super
    end

    def quantity_with_unit
      [quantity, unit].compact.join(' ')
    end

    def quantity
      min = quantity_minimum&.format_quantity
      max = quantity_maximum&.format_quantity

      [min, max].compact.join('–') # ndash, not hyphen
    end

    if defined?(RailsAdmin)
      rails_admin do
        visible false
        object_label_method { :object_label }
        edit do
          include_all_fields
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cookbook-0.1.6 app/models/cookbook/use.rb
cookbook-0.1.5 app/models/cookbook/use.rb
cookbook-0.1.4 app/models/cookbook/use.rb
cookbook-0.1.3 app/models/cookbook/use.rb