Flowdock
method

define_delegated_type_methods

Importance_0
v7.1.3.2 - Show latest stable - 0 notes - Class: DelegatedType
define_delegated_type_methods(role, types:, options:) private

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/delegated_type.rb, line 217
      def define_delegated_type_methods(role, types,, options))
        primary_key = options[:primary_key] || "id"
        role_type = options[:foreign_type] || "#{role}_type"
        role_id   = options[:foreign_key] || "#{role}_id"

        define_method "#{role}_class" do
          public_send(role_type).constantize
        end

        define_method "#{role}_name" do
          public_send("#{role}_class").model_name.singular.inquiry
        end

        define_method "build_#{role}" do |*params|
          public_send("#{role}=", public_send("#{role}_class").new(*params))
        end

        types.each do |type|
          scope_name = type.tableize.tr("/", "_")
          singular   = scope_name.singularize
          query      = "#{singular}?"

          scope scope_name, -> { where(role_type => type) }

          define_method query do
            public_send(role_type) == type
          end

          define_method singular do
            public_send(role) if public_send(query)
          end

          define_method "#{singular}_#{primary_key}" do
            public_send(role_id) if public_send(query)
          end
        end
      end
Register or log in to add new notes.