module InheritedResources::BaseHelpers

def apply_scopes_if_available(target_object) #:nodoc:

:nodoc:

It's extend by HasScopeHelpers.
Hook to apply scopes. By default returns only the target_object given.
def apply_scopes_if_available(target_object) #:nodoc:
  respond_to?(:apply_scopes) ? apply_scopes(target_object) : target_object
end

def association_chain


current resource).
Returns the association chain, with all parents (does not include the
def association_chain
  @association_chain ||=
    symbols_for_association_chain.inject([begin_of_association_chain]) do |chain, symbol|
      chain << evaluate_parent(symbol, resources_configuration[symbol], chain.last)
    end.compact.freeze
end

def begin_of_association_chain


and declare that projects belong to user.
If the user actually scopes the url, you should use belongs_to method

project_url(@current_user, @project)

urls, so this is never going to happen when calling resource_url:
The variable set in begin_of_association_chain is not sent when building

@current_user.projects.find(params[:id])
@current_user.projects.build(params[:project])

So every time we instantiate a project, we will do:

end
@current_user
def begin_of_association_chain

could do this:
and that means that they belong to the current logged in user. So you
association chain. For example, usually your projects belongs to users
This class allows you to set a instance variable to begin your
def begin_of_association_chain
  nil
end

def build_resource


instance variable.
methods. If you overwrite it, don't forget to cache the result in an
This method is responsable for building the object on :new and :create
def build_resource
  get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, params[resource_instance_name] || {}))
end

def collection


end
@projects ||= end_of_association_chain.paginate(params[:page]).all
def collection

instance_variable:
for example. When you do that, don't forget to cache the result in an
You might want to overwrite this method if you want to add pagination

This is how the collection is loaded.
def collection
  get_collection_ivar || set_collection_ivar(end_of_association_chain.find(:all))
end

def create_resource(object)


end
object.send_instructions_by_email
def create_resource(object)

:create method, you could do something:
sent password instructions for him. Instead of overwriting the entire
PassworsController who is responsible for finding an user by email and
allow you to control the way resource is saved. Let's say you have a
Responsible for saving the resource on :create method. Overwriting this
def create_resource(object)
  object.save
end

def destroy_resource(object)


end
object.cancel
def destroy_resource(object)

own method for destroing the resource, as:
Handle the :destroy method for the resource. Overwrite it to call your
def destroy_resource(object)
  object.destroy
end

def end_of_association_chain #:nodoc:

:nodoc:

parents chain and returns the scoped association.
This methods gets your begin_of_association_chain, join it with your
def end_of_association_chain #:nodoc:
  if chain = association_chain.last
    if method_for_association_chain
      apply_scopes_if_available(chain.send(method_for_association_chain))
    else
      # This only happens when we specify begin_of_association_chain in
      # a singletion controller without parents. In this case, the chain
      # is exactly the begin_of_association_chain which is already an
      # instance and then not scopable.
      chain
    end
  else
    apply_scopes_if_available(resource_class)
  end
end

def get_collection_ivar #:nodoc:

:nodoc:

Get collection ivar based on the current resource controller.
def get_collection_ivar #:nodoc:
  instance_variable_get("@#{resource_collection_name}")
end

def get_resource_ivar #:nodoc:

:nodoc:

Get resource ivar based on the current resource controller.
def get_resource_ivar #:nodoc:
  instance_variable_get("@#{resource_instance_name}")
end

def method_for_association_build


where we have a parent. This is overwritten in singleton scenarios.
Returns the name of the method used for build the resource in cases
def method_for_association_build
  :build
end

def method_for_association_chain #:nodoc:

:nodoc:

where no method for association chain is called.
of the association chain. This is overwriten by singleton cases
Returns the name of the method to be called, before returning the end
def method_for_association_chain #:nodoc:
  resource_collection_name
end

def method_for_build #:nodoc:

:nodoc:

Returns the appropriated method to build the resource.
def method_for_build #:nodoc:
  (begin_of_association_chain || parent?) ? method_for_association_build : :new
end

def parent?


it's always false and should not be overwriten.
Returns if the controller has a parent. When only base helpers are loaded,
def parent?
  false
end

def resource


probably render a 500 error message.
because it will raise a 404 if nothing can be found. Otherwise it will
You also might want to add the exclamation mark at the end of the method

end
@project ||= end_of_association_chain.find_by_permalink!(params[:id])
def resource

instance_variable:
When you do that, don't forget to cache the result in an
You might want to overwrite this method when you are using permalink.

This is how the resource is loaded.
def resource
  get_resource_ivar || set_resource_ivar(end_of_association_chain.find(params[:id]))
end

def resource_collection_name #:nodoc:

:nodoc:

Fast accessor to resource_collection_name
def resource_collection_name #:nodoc:
  self.resources_configuration[:self][:collection_name]
end

def resource_instance_name #:nodoc:

:nodoc:

Fast accessor to resource_instance_name
def resource_instance_name #:nodoc:
  self.resources_configuration[:self][:instance_name]
end

def respond_with_dual_blocks(object, options, &block) #:nodoc:

:nodoc:

given and returns it. Otherwise returns nil.
It also calculates the response url in case a block without arity is

end
end
failure.html { redirect_to root_url }
create! do |success, failure|
def create

Used to allow to specify success and failure within just one block:
def respond_with_dual_blocks(object, options, &block) #:nodoc:
  args = (with_chain(object) << options)
  case block.try(:arity)
    when 2
      respond_with(*args) do |responder|
        blank_slate = InheritedResources::BlankSlate.new
        if object.errors.empty?
          block.call(responder, blank_slate)
        else
          block.call(blank_slate, responder)
        end
      end
    when 1
      respond_with(*args, &block)
    else
      options[:location] = block.call if block
      respond_with(*args)
  end
end

def set_collection_ivar(collection) #:nodoc:

:nodoc:

Set collection ivar based on the current resource controller.
def set_collection_ivar(collection) #:nodoc:
  instance_variable_set("@#{resource_collection_name}", collection)
end

def set_resource_ivar(resource) #:nodoc:

:nodoc:

Set resource ivar based on the current resource controller.
def set_resource_ivar(resource) #:nodoc:
  instance_variable_set("@#{resource_instance_name}", resource)
end

def symbols_for_association_chain #:nodoc:

:nodoc:

by belongs_to and can be complex in polymorphic cases.
Symbols chain in base helpers return nothing. This is later overwriten
def symbols_for_association_chain #:nodoc:
  []
end

def update_resource(object, attributes)


end
object.reset_password!(attributes)
def update_resource(object, attributes)

way then the usual :update_attributes:
to handle how the resource is gona be updated, let's say in a different
Responsible for updating the resource in :update method. This allow you
def update_resource(object, attributes)
  object.update_attributes(attributes)
end

def with_chain(object)

Adds the given object to association chain.
def with_chain(object)
  association_chain + [ object ]
end