Sha256: 8e19e4f0c38d52cf7bcb54cdaa4df867ab22c76b258ab5264140cc6c518e7bb1

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

require "active_admin/resource"

module ActiveAdmin
  class Resource
    class BelongsTo

      class TargetNotFound < StandardError
        def initialize(key, namespace)
          super "Could not find #{key} in #{namespace.name} " +
                "with #{namespace.resources.map(&:resource_name)}"
        end
      end

      # The resource which initiated this relationship
      attr_reader :owner

      # The name of the relation
      attr_reader :target_name

      def initialize(owner, target_name, options = {})
        @owner = owner
        @target_name = target_name
        @options = options
      end

      # Returns the target resource class or raises an exception if it doesn't exist
      def target
        resource or raise TargetNotFound.new (@options[:class_name] || @target_name.to_s.camelize), namespace
      end

      def resource
        namespace.resources[@options[:class_name]] ||
          namespace.resources[@target_name.to_s.camelize]
      end

      def namespace
        @owner.namespace
      end

      def optional?
        @options[:optional]
      end

      def required?
        !optional?
      end

      def to_param
        (@options[:param] || "#{@target_name}_id").to_sym
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/resource/belongs_to.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/resource/belongs_to.rb
activeadmin-2.9.0 lib/active_admin/resource/belongs_to.rb
activeadmin-2.8.1 lib/active_admin/resource/belongs_to.rb