Skip to content

Commit 65f95b2

Browse files
committed
initial import
0 parents  commit 65f95b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+8227
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
log/*
2+
tmp/*
3+
*.sqlite3

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2008 Ryan Bates
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Complex Form Examples
2+
---------------------
3+
4+
Several different ways to handle the multi-model form problem with a
5+
one-to-many association. The various approaches are in git branches.
6+
See them for examples.

Rakefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
5+
6+
require 'rake'
7+
require 'rake/testtask'
8+
require 'rake/rdoctask'
9+
10+
require 'tasks/rails'

app/controllers/application.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Filters added to this controller apply to all controllers in the application.
2+
# Likewise, all the methods added will be available for all controllers.
3+
4+
class ApplicationController < ActionController::Base
5+
helper :all # include all helpers, all the time
6+
7+
# See ActionController::RequestForgeryProtection for details
8+
# Uncomment the :secret if you're not using the cookie session store
9+
protect_from_forgery # :secret => '942733c28af903e7eb303ba250f52f1b'
10+
11+
# See ActionController::Base for details
12+
# Uncomment this to filter the contents of submitted sensitive data parameters
13+
# from your application log (in this case, all fields with names like "password").
14+
# filter_parameter_logging :password
15+
end
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class ProjectsController < ApplicationController
2+
def index
3+
@projects = Project.find(:all)
4+
end
5+
6+
def show
7+
@project = Project.find(params[:id])
8+
end
9+
10+
def new
11+
@project = Project.new
12+
end
13+
14+
def create
15+
@project = Project.new(params[:project])
16+
if @project.save
17+
flash[:notice] = "Successfully created project."
18+
redirect_to @project
19+
else
20+
render :action => 'new'
21+
end
22+
end
23+
24+
def edit
25+
@project = Project.find(params[:id])
26+
end
27+
28+
def update
29+
@project = Project.find(params[:id])
30+
if @project.update_attributes(params[:project])
31+
flash[:notice] = "Successfully updated project."
32+
redirect_to @project
33+
else
34+
render :action => 'edit'
35+
end
36+
end
37+
38+
def destroy
39+
@project = Project.find(params[:id])
40+
@project.destroy
41+
flash[:notice] = "Successfully destroyed project."
42+
redirect_to projects_url
43+
end
44+
end

app/helpers/application_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Methods added to this helper will be available to all templates in the application.
2+
module ApplicationHelper
3+
end

app/helpers/layout_helper.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# These helper methods can be called in your template to set variables to be used in the layout
2+
# This module should be included in all views globally,
3+
# to do so you may need to add this line to your ApplicationController
4+
# helper :layout
5+
module LayoutHelper
6+
def title(page_title, show_title = true)
7+
@content_for_title = page_title.to_s
8+
@show_title = show_title
9+
end
10+
11+
def show_title?
12+
@show_title
13+
end
14+
15+
def stylesheet(*args)
16+
content_for(:head) { stylesheet_link_tag(*args.map(&:to_s)) }
17+
end
18+
19+
def javascript(*args)
20+
args = args.map { |arg| arg == :defaults ? arg : arg.to_s }
21+
content_for(:head) { javascript_include_tag(*args) }
22+
end
23+
end

app/helpers/projects_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ProjectsHelper
2+
end

app/models/project.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Project < ActiveRecord::Base
2+
has_many :tasks
3+
validates_presence_of :name
4+
end

app/models/task.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Task < ActiveRecord::Base
2+
belongs_to :project
3+
validates_presence_of :name
4+
end
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3+
<html>
4+
<head>
5+
<title><%= h(yield(:title) || "Untitled") %></title>
6+
<%= stylesheet_link_tag 'application' %>
7+
<%= yield(:head) %>
8+
</head>
9+
<body>
10+
<div id="container">
11+
<%- flash.each do |name, msg| -%>
12+
<%= content_tag :div, msg, :id => "flash_#{name}" %>
13+
<%- end -%>
14+
15+
<%- if show_title? -%>
16+
<h1><%=h yield(:title) %></h1>
17+
<%- end -%>
18+
19+
<%= yield %>
20+
</div>
21+
</body>
22+
</html>

app/views/projects/_form.html.erb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<%= error_messages_for :project %>
2+
<% form_for @project do |f| %>
3+
<p>
4+
<%= f.label :name %><br />
5+
<%= f.text_field :name %>
6+
</p>
7+
<p><%= f.submit "Submit" %></p>
8+
<% end %>

app/views/projects/edit.html.erb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<% title "Edit Project" %>
2+
3+
<%= render :partial => 'form' %>
4+
5+
<p>
6+
<%= link_to "Show", @project %> |
7+
<%= link_to "View All", projects_path %>
8+
</p>

app/views/projects/index.html.erb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<% title "Projects" %>
2+
3+
<table>
4+
<tr>
5+
<th>Name</th>
6+
</tr>
7+
<% for project in @projects %>
8+
<tr>
9+
<td><%=h project.name %></td>
10+
<td><%= link_to "Show", project %></td>
11+
<td><%= link_to "Edit", edit_project_path(project) %></td>
12+
<td><%= link_to "Destroy", project, :confirm => 'Are you sure?', :method => :delete %></td>
13+
</tr>
14+
<% end %>
15+
</table>
16+
17+
<p><%= link_to "New Project", new_project_path %></p>

app/views/projects/new.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<% title "New Project" %>
2+
3+
<%= render :partial => 'form' %>
4+
5+
<p><%= link_to "Back to List", projects_path %></p>

app/views/projects/show.html.erb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<% title "Project" %>
2+
3+
<p>
4+
<strong>Name:</strong>
5+
<%=h @project.name %>
6+
</p>
7+
8+
<h3>Tasks</h3>
9+
10+
<ul>
11+
<% for task in @project.tasks %>
12+
<li><%=h task.name %></li>
13+
<% end %>
14+
</ul>
15+
16+
<p>
17+
<%= link_to "Edit", edit_project_path(@project) %> |
18+
<%= link_to "Destroy", @project, :confirm => 'Are you sure?', :method => :delete %> |
19+
<%= link_to "View All", projects_path %>
20+
</p>

config/boot.rb

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Don't change this file!
2+
# Configure your app in config/environment.rb and config/environments/*.rb
3+
4+
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5+
6+
module Rails
7+
class << self
8+
def boot!
9+
unless booted?
10+
preinitialize
11+
pick_boot.run
12+
end
13+
end
14+
15+
def booted?
16+
defined? Rails::Initializer
17+
end
18+
19+
def pick_boot
20+
(vendor_rails? ? VendorBoot : GemBoot).new
21+
end
22+
23+
def vendor_rails?
24+
File.exist?("#{RAILS_ROOT}/vendor/rails")
25+
end
26+
27+
def preinitialize
28+
load(preinitializer_path) if File.exist?(preinitializer_path)
29+
end
30+
31+
def preinitializer_path
32+
"#{RAILS_ROOT}/config/preinitializer.rb"
33+
end
34+
end
35+
36+
class Boot
37+
def run
38+
load_initializer
39+
Rails::Initializer.run(:set_load_path)
40+
end
41+
end
42+
43+
class VendorBoot < Boot
44+
def load_initializer
45+
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46+
Rails::Initializer.run(:install_gem_spec_stubs)
47+
end
48+
end
49+
50+
class GemBoot < Boot
51+
def load_initializer
52+
self.class.load_rubygems
53+
load_rails_gem
54+
require 'initializer'
55+
end
56+
57+
def load_rails_gem
58+
if version = self.class.gem_version
59+
gem 'rails', version
60+
else
61+
gem 'rails'
62+
end
63+
rescue Gem::LoadError => load_error
64+
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
65+
exit 1
66+
end
67+
68+
class << self
69+
def rubygems_version
70+
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
71+
end
72+
73+
def gem_version
74+
if defined? RAILS_GEM_VERSION
75+
RAILS_GEM_VERSION
76+
elsif ENV.include?('RAILS_GEM_VERSION')
77+
ENV['RAILS_GEM_VERSION']
78+
else
79+
parse_gem_version(read_environment_rb)
80+
end
81+
end
82+
83+
def load_rubygems
84+
require 'rubygems'
85+
86+
unless rubygems_version >= '0.9.4'
87+
$stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
88+
exit 1
89+
end
90+
91+
rescue LoadError
92+
$stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
93+
exit 1
94+
end
95+
96+
def parse_gem_version(text)
97+
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
98+
end
99+
100+
private
101+
def read_environment_rb
102+
File.read("#{RAILS_ROOT}/config/environment.rb")
103+
end
104+
end
105+
end
106+
end
107+
108+
# All that for this:
109+
Rails.boot!

config/database.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SQLite version 3.x
2+
# gem install sqlite3-ruby (not necessary on OS X Leopard)
3+
development:
4+
adapter: sqlite3
5+
database: db/development.sqlite3
6+
timeout: 5000
7+
8+
# Warning: The database defined as "test" will be erased and
9+
# re-generated from your development database when you run "rake".
10+
# Do not set this db to the same as development or production.
11+
test:
12+
adapter: sqlite3
13+
database: db/test.sqlite3
14+
timeout: 5000
15+
16+
production:
17+
adapter: sqlite3
18+
database: db/production.sqlite3
19+
timeout: 5000

0 commit comments

Comments
 (0)