Skip to content

Commit

Permalink
Added Tag as a quick test of has_and_belongs_to_many with nested_params.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Oct 9, 2008
1 parent 7799d94 commit e0ae3c9
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/controllers/projects_controller.rb
Expand Up @@ -10,6 +10,7 @@ def show
def new
@project = Project.new
@project.tasks.build
@project.tags.build
end

def create
Expand All @@ -26,6 +27,7 @@ def edit
@project = Project.find(params[:id])
# add an extra new record for debugging purposes
@project.tasks.build
@project.tags.build
end

def update
Expand Down
10 changes: 10 additions & 0 deletions app/helpers/projects_helper.rb
Expand Up @@ -8,4 +8,14 @@ def add_task_link(name, form)
}
end
end

def add_tag_link(name, form)
link_to_function name do |page|
tag = render(:partial => 'tag', :locals => { :pf => form, :tag => Tag.new })
page << %{
var new_tag_id = "new_" + new Date().getTime();
$('tags').insert({ bottom: "#{ escape_javascript tag }".replace(/new_\\d+/g, new_tag_id) });
}
end
end
end
1 change: 1 addition & 0 deletions app/models/project.rb
Expand Up @@ -2,6 +2,7 @@ class Project < ActiveRecord::Base
# Automatically turns on autosave and thus also validates
has_one :author, :nested_params => true
has_many :tasks, :dependent => :destroy, :nested_params => true, :destroy_missing => true
has_and_belongs_to_many :tags, :nested_params => true, :destroy_missing => true

validates_presence_of :name
end
3 changes: 3 additions & 0 deletions app/models/tag.rb
@@ -0,0 +1,3 @@
class Tag < ActiveRecord::Base
validates_presence_of :name, :message => "can't just be blank"
end
18 changes: 18 additions & 0 deletions app/views/projects/_form.html.erb
Expand Up @@ -32,6 +32,24 @@
<p>
<%= add_task_link "Add a task", f %>
</p>

<div id="tags">
<%#= render :partial => 'task', :collection => @project.tasks, :locals => { :pf => f } %>
<!-- For the sake of demoing the fields_for which yields each record. -->
<% f.fields_for :tags do |tf| %>
<div class="tag">
<p>
<%= tf.label :name, "Tag:" %>
<%= tf.text_field :name %>
<%= link_to_function "remove", "$(this).up('.tag').remove()" %>
</p>
</div>
<% end %>
</div>
<p>
<%= add_tag_link "Add a tag", f %>
</p>

<p>
<%= f.submit "Submit" %>
</p>
Expand Down
9 changes: 9 additions & 0 deletions app/views/projects/_tag.html.erb
@@ -0,0 +1,9 @@
<div class="tag">
<% pf.fields_for :tags, tag do |f| %>
<p>
<%= f.label :name, "Tag:" %>
<%= f.text_field :name %>
<%= link_to_function "remove", "$(this).up('.tag').remove()" %>
</p>
<% end %>
</div>
10 changes: 9 additions & 1 deletion app/views/projects/show.html.erb
Expand Up @@ -13,11 +13,19 @@
<h3>Tasks</h3>

<ul>
<% for task in @project.tasks %>
<% @project.tasks.each do |task| %>
<li><%=h task.name %></li>
<% end %>
</ul>

<h3>Tags</h3>

<ul>
<% @project.tags.each do |tag| %>
<li><%=h tag.name %></li>
<% end %>
</ul>

<p>
<%= link_to "Edit", edit_project_path(@project) %> |
<%= link_to "Destroy", @project, :confirm => 'Are you sure?', :method => :delete %> |
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20081009223323_create_tags.rb
@@ -0,0 +1,12 @@
class CreateTags < ActiveRecord::Migration
def self.up
create_table :tags do |t|
t.string :name
t.timestamps
end
end

def self.down
drop_table :tags
end
end
14 changes: 14 additions & 0 deletions db/migrate/20081009223404_create_projects_tags_join_table.rb
@@ -0,0 +1,14 @@
class CreateProjectsTagsJoinTable < ActiveRecord::Migration
def self.up
create_table :projects_tags, :id => false do |t|
t.integer :project_id
t.integer :tag_id
end
add_index :projects_tags, :project_id
add_index :projects_tags, :tag_id
end

def self.down
drop_table :projects_tags
end
end
16 changes: 15 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20080918210237) do
ActiveRecord::Schema.define(:version => 20081009223404) do

create_table "authors", :force => true do |t|
t.integer "project_id"
Expand All @@ -26,6 +26,20 @@
t.datetime "updated_at"
end

create_table "projects_tags", :id => false, :force => true do |t|
t.integer "project_id"
t.integer "tag_id"
end

add_index "projects_tags", ["tag_id"], :name => "index_projects_tags_on_tag_id"
add_index "projects_tags", ["project_id"], :name => "index_projects_tags_on_project_id"

create_table "tags", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "tasks", :force => true do |t|
t.integer "project_id"
t.string "name"
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/tags.yml
@@ -0,0 +1,7 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
name: MyString

two:
name: MyString
8 changes: 8 additions & 0 deletions test/unit/tag_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class TagTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

0 comments on commit e0ae3c9

Please sign in to comment.