Skip to content

Commit

Permalink
pulling in some of the changes from deep branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Aug 5, 2009
1 parent 3bb8ec1 commit 4e46236
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
10 changes: 5 additions & 5 deletions app/helpers/application_helper.rb
@@ -1,19 +1,19 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
def remove_task_link(name, f)
def remove_child_link(name, f)
f.hidden_field(:_delete) + link_to_function(name, "remove_fields(this)")
end

def add_task_link(name, f)
fields = new_child_fields(f, :tasks)
link_to_function(name, "insert_fields('tasks', '#{escape_javascript(fields)}')")
def add_child_link(name, f, method)
fields = new_child_fields(f, method)
link_to_function(name, h("insert_fields(this, \"#{method}\", \"#{escape_javascript(fields)}\")"))
end

def new_child_fields(form_builder, method, options = {})
options[:object] ||= form_builder.object.class.reflect_on_association(method).klass.new
options[:partial] ||= method.to_s.singularize
options[:form_builder_local] ||= :f
form_builder.fields_for(method, options[:object], :child_index => 'NEW_RECORD') do |f|
form_builder.fields_for(method, options[:object], :child_index => "new_#{method}") do |f|
render(:partial => options[:partial], :locals => { options[:form_builder_local] => f })
end
end
Expand Down
4 changes: 1 addition & 3 deletions app/views/projects/_form.html.erb
Expand Up @@ -6,11 +6,9 @@
</p>

<h3>Tasks</h3>
<div id="tasks">
<% f.fields_for :tasks do |task_form| %>
<%= render :partial => 'task', :locals => { :f => task_form } %>
<% end %>
</div>
<p><%= add_task_link "New Task", f %></p>
<p><%= add_child_link "New Task", f, :tasks %></p>
<p><%= f.submit "Submit" %></p>
<% end %>
2 changes: 1 addition & 1 deletion app/views/projects/_task.html.erb
@@ -1,7 +1,7 @@
<div class="field">
<%= f.label :name, "Task" %>
<%= f.text_field :name %>
<%= remove_task_link "remove", f %>
<%= remove_child_link "remove", f %>
<%# Alternatively you can use a check box: %>
<%# f.check_box :_delete %>
<%# f.label :_delete, "(remove)" %>
Expand Down
9 changes: 5 additions & 4 deletions public/javascripts/application.js
@@ -1,9 +1,10 @@
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function insert_fields(element_id, content) {
function insert_fields(link, method, content) {
var new_id = new Date().getTime();
$(element_id).insert({
bottom: content.replace(/NEW_RECORD/g, new_id)
var regexp = new RegExp("new_" + method, "g")
$(link).up().insert({
before: content.replace(regexp, new_id)
});
}

Expand All @@ -12,5 +13,5 @@ function remove_fields(link) {
if (hidden_field) {
hidden_field.value = '1';
}
$(link).up(".field").remove();
$(link).up(".field").hide();
}

0 comments on commit 4e46236

Please sign in to comment.