Flowdock
method

define_delegated_type_methods

Importance_0
v6.1.3.1 - Show latest stable - 0 notes - Class: DelegatedType
define_delegated_type_methods(role, types:) 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 176
      def define_delegated_type_methods(role, types))
        role_type = "#{role}_type"
        role_id   = "#{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

        types.each do |type|
          scope_name = type.tableize.gsub("/", "_")
          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}_id" do
            public_send(role_id) if public_send(query)
          end
        end
      end
Register or log in to add new notes.