Sha256: 2c2d2f640bb2bbe8b51dbe1d70f87cd582c866a991deb7d5f5b728a61e429b00

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 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

      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
        :"#{@target_name}_id"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activeadmin-2.2.0 lib/active_admin/resource/belongs_to.rb