Sha256: 9c03c87cc57c9877047bb989eb51d4b65c8ff32c4a06b9d164d9d53078e4e1e0

Contents?: true

Size: 1.89 KB

Versions: 28

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Tramway::Core::Associations::ObjectHelper
  def class_name(association)
    if association.polymorphic?
      object.send(association.name).class
    else
      unless association.options[:class_name]
        Tramway::Error.raise_error(
          :tramway, :core, :associations, :object_helper, :please_specify_association_name,
          association_name: association.name, object_class: object.class,
          association_class_name: association.name.to_s.singularize.camelize
        )
      end
      association.options[:class_name]
    end
  end

  def check_association(object, association_name, association)
    return unless association.nil?

    Tramway::Error.raise_error(
      :tramway, :core, :associations, :class_helper, :model_does_not_have_association,
      object_class: object.class, association_name: association_name
    )
  end

  def association_type(association)
    association.class.to_s.split('::').last.sub(/Reflection$/, '').underscore.to_sym
  end

  def associations_collection(object, association_name, decorator_class_name)
    object.send(association_name).active.map do |association_object|
      decorator_class_name.decorate association_object
    end
  end

  def add_association_form_class_name(object, association_name)
    form_class = "Admin::#{object.class.to_s.pluralize}::Add#{association_name.to_s.camelize.singularize}Form"

    begin
      form_class.constantize
    rescue StandardError
      Tramway::Error.raise_error(
        :tramway, :core, :associations, :object_helper, :habtm_add_class_not_defined,
        class: form_class, association_name: association_name
      )
    end
  end

  def association_has_one_without_object(object, association_name, association_type)
    association_type == :has_one && object.send(association_name).nil?
  end

  def association_object(object, association_name)
    object.send association_name
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
tramway-core-2.0.2.2 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.2.1 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.2 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.1.2 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.1.1 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.1 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.0.8 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.0.7 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.0.6 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.0.5 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.0.4 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.0.3 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.0.2 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0.0.1 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-2.0 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-1.18.6.1 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-1.18.6 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-1.18.5.2 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-1.18.5.1 app/decorators/tramway/core/associations/object_helper.rb
tramway-core-1.18.5 app/decorators/tramway/core/associations/object_helper.rb