Sha256: 2b926fc31d2952b8fbc1b6e6996ae4d1b9e70b89f33c344ed94ae3b66e9b6c4e

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

module ActiveCollection
  module MemberClass
    def self.included(mod)
      mod.extend(ClassMethods)
    end

    module ClassMethods
      #
      # If the name of the class held by your collection cannot be derived from
      # the name of the collection class (by removing the word Collection from
      # the end of the collection class name) then use model to set it.
      #
      # Example:
      #
      #     class WeirdNamedCollection
      #       model "Normal"
      #     end
      # 
      # This will use the class Normal to do counts and finds.
      def model(model_name)
        (@model_class_name = model_name) && @model_class = nil
      end

      # The actual member class.
      #
      # Prints a useful error message if you define your model class wrong.
      def model_class
        begin
          @model_class ||= model_class_name.constantize
        rescue NameError => e
          raise NameError, %|#{e} - Use 'model "Class"' in the collection to declare the correct model class for #{name}|
        end
      end

      # Table name of the member class.
      def table_name
        model_class.table_name
      end

      # Plural human name of the member class.
      def human_name(*args)
        model_class.human_name(*args).pluralize
      end

      def model_class_name
        @model_class_name ||
          (superclass != ActiveCollection::Base && superclass.model_class_name) ||
          name.sub(/Collection$/,'')
      end
    end

    # The actual member class.
    def model_class
      self.class.model_class
    end

    # Table name of the member class.
    def table_name
      self.class.table_name
    end

    # Plural human name of the member class.
    def human_name(*args)
      self.class.human_name(*args)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_collection-0.2.6 lib/active_collection/member_class.rb
active_collection-0.2.5 lib/active_collection/member_class.rb