Skip to content

Commit

Permalink
more doc patches
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.jamesgolick.com/attribute_fu/trunk@45 80b79608-713f-0410-8737-d8c0d0c1b50c
  • Loading branch information
james committed Dec 2, 2007
1 parent 7497698 commit e665323
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/attribute_fu/associated_form_helper.rb
Expand Up @@ -115,7 +115,7 @@ def add_associated_link(name, object, opts = {})
# * <tt>:partial</tt> - specify the name of the partial in which the form is located.
# * <tt>:fields_for</tt> - specify additional options for the fields_for_associated call
# * <tt>:locals</tt> - specify additional variables to be passed along to the partial
# * <td>:render</td> - specify additional options to be passed along to the render :partial call
# * <tt>:render</tt> - specify additional options to be passed along to the render :partial call
#
def render_associated_form(associated, opts = {})
associated = associated.is_a?(Array) ? associated : [associated] # preserve association proxy if this is one
Expand Down
27 changes: 22 additions & 5 deletions lib/attribute_fu/associations.rb
@@ -1,7 +1,7 @@
module AttributeFu
module Associations

def self.included(base)
def self.included(base) #:nodoc:
base.class_eval do
extend ClassMethods
class << self; alias_method_chain :has_many, :association_option; end
Expand All @@ -13,7 +13,7 @@ class << self; alias_method_chain :has_many, :association_option; end
end
end

def method_missing(method_name, *args)
def method_missing(method_name, *args) #:nodoc:
if method_name.to_s =~ /.+?\_attributes=/
association_name = method_name.to_s.gsub '_attributes=', ''
association = managed_association_attributes.detect { |element| element == association_name.to_sym } || managed_association_attributes.detect { |element| element == association_name.pluralize.to_sym }
Expand All @@ -29,7 +29,7 @@ def method_missing(method_name, *args)
end

private
def has_many_attributes(association_id, attributes)
def has_many_attributes(association_id, attributes) #:nodoc:
association = send(association_id)
attributes = {} unless attributes.is_a? Hash

Expand All @@ -53,7 +53,7 @@ def has_many_attributes(association_id, attributes)
end
end

def save_managed_associations
def save_managed_associations #:nodoc:
managed_association_attributes.each do |association_id|
association = send(association_id)
association.each(&:save)
Expand All @@ -65,11 +65,28 @@ def save_managed_associations
end
end

def removal_variable_name(association_id)
def removal_variable_name(association_id) #:nodoc:
"@#{association_id.to_s.pluralize}_to_remove"
end

module ClassMethods

# Behaves identically to the regular has_many, except adds the option <tt>:attributes</tt>, which, if true, creates
# a method called association_id_attributes (i.e. task_attributes, or comment_attributes) for setting the attributes
# of a collection of associated models.
#
# The format is as follows:
#
# @project.task_attributes = {
# @project.tasks.first.id => {:title => "A new title for an existing task"},
# :new => {
# "0" => {:title => "A new task"}
# }
# }
#
# Any existing tasks that are not present in the attributes hash will be removed from the association when the (parent) model
# is saved.
#
def has_many_with_association_option(association_id, options = {}, &extension)
unless (config = options.delete(:attributes)).nil?
self.managed_association_attributes << association_id
Expand Down

0 comments on commit e665323

Please sign in to comment.