module InheritedResources::ClassMethods

def belongs_to(*symbols, &block)


type of polymorphic association)
* :optional - Tell the association is optional (it's a special

* :singleton - Tell it's a singleton association.

* :polymorphic - Tell the association is polymorphic.

You supply the collection name.

@company.admin_projects

But if you want to retrieve instead:

@company.projects

down the road:
which belongs to companies. This will do somewhere
suppose you have Tasks which belongs to Projects
* :collection_name - Tell how to retrieve the next collection. Let's

helper. By default is association name.
* :route_name - Allows you to specify what is the route name in your url

Default is :association_id, which in this case is :project_id.
* :param - Allows you to specify params key to retrieve the id.

Project.find(params[:project_id])

Instead of:

Project.find_by_title!(params[:project_id])

This will make your projects be instantiated as:

belongs_to :project, :finder => :find_by_title!

* :finder - Specifies which method should be called to instantiate the parent.

belongs_to :project, :instance_name => :my_project

* :instance_name - The instance variable name. By default is the name of the association.

give a string. Added for ActiveRecord belongs to compatibility.
* :class_name - Also allows you to specify the parent class, but you should

belongs_to :project, :parent_class => AdminProject

* :parent_class - Allows you to specify what is the parent class.

== Options

belongs_to :projects

Defines that this controller belongs to another resource.
def belongs_to(*symbols, &block)
  options = symbols.extract_options!
  options.symbolize_keys!
  options.assert_valid_keys(:class_name, :parent_class, :instance_name, :param,
                            :finder, :route_name, :collection_name, :singleton,
                            :polymorphic, :optional, :shallow)
  optional    = options.delete(:optional)
  shallow     = options.delete(:shallow)
  polymorphic = options.delete(:polymorphic)
  finder      = options.delete(:finder)
  include BelongsToHelpers if self.parents_symbols.empty?
  acts_as_polymorphic! if polymorphic || optional
  acts_as_shallow!     if shallow
  raise ArgumentError, 'You have to give me at least one association name.' if symbols.empty?
  raise ArgumentError, "You cannot define multiple associations with options: #{options.keys.inspect} to belongs to." unless symbols.size == 1 || options.empty?
  symbols.each do |symbol|
    symbol = symbol.to_sym
    if polymorphic || optional
      self.parents_symbols << :polymorphic unless self.parents_symbols.include?(:polymorphic)
      self.resources_configuration[:polymorphic][:symbols]   << symbol
      self.resources_configuration[:polymorphic][:optional] ||= optional
    else
      self.parents_symbols << symbol
    end
    self.resources_configuration[:self][:shallow] = true if shallow
    config = self.resources_configuration[symbol] = {}
    class_name = ''
    config[:parent_class] = options.delete(:parent_class) ||
      begin
        class_name = if options[:class_name]
          options.delete(:class_name).to_s.pluralize.classify
        else
          namespace = self.name.deconstantize
          model_name = symbol.to_s.pluralize.classify
          klass = model_name
          while namespace != ''
            new_klass = "#{namespace}::#{model_name}"
            if new_klass.safe_constantize
              klass = new_klass
              break
            else
              namespace = namespace.deconstantize
            end
          end
          klass = model_name if klass.start_with?('::')
          klass
        end
        class_name.constantize
      rescue NameError => e
        raise unless e.message.include?(class_name)
        nil
      end
    config[:singleton]       = options.delete(:singleton) || false
    config[:collection_name] = options.delete(:collection_name) || symbol.to_s.pluralize.to_sym
    config[:instance_name]   = options.delete(:instance_name) || symbol
    config[:param]           = options.delete(:param) || :"#{symbol}_id"
    config[:route_name]      = options.delete(:route_name) || symbol
    config[:finder]          = finder || :find
  end
  if block_given?
    class_eval(&block)
  else
    create_resources_url_helpers!
  end
  helper_method :parent, :parent?
end