Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: paulcook/tripd
base: 92dc08959b
...
head fork: paulcook/tripd
compare: ac54920287
  • 5 commits
  • 26 files changed
  • 0 commit comments
  • 2 contributors
Showing with 295 additions and 64 deletions.
  1. +1 −2  Gemfile
  2. +7 −2 Gemfile.lock
  3. +1 −0  app/assets/javascripts/application.js
  4. +5 −2 app/assets/javascripts/legs.js.coffee
  5. +71 −0 app/assets/javascripts/nested_form.js
  6. +3 −0  app/assets/javascripts/trips.js.coffee
  7. +0 −16 app/assets/stylesheets/legs.css.scss
  8. +3 −0  app/assets/stylesheets/trips.css.scss
  9. +68 −5 app/controllers/trips_controller.rb
  10. +2 −0  app/helpers/trips_helper.rb
  11. +1 −0  app/models/leg.rb
  12. +2 −3 app/models/trip.rb
  13. +4 −0 app/views/layouts/application.html.erb
  14. +21 −16 app/views/trips/_form.html.erb
  15. +0 −2  app/views/trips/create.html.erb
  16. +6 −2 app/views/trips/edit.html.erb
  17. +25 −2 app/views/trips/index.html.erb
  18. +4 −1 app/views/trips/new.html.erb
  19. +15 −0 app/views/trips/show.html.erb
  20. +0 −2  app/views/trips/update.html.erb
  21. +1 −1  config/routes.rb
  22. +0 −6 db/migrate/20120301214027_add_trip_id_to_legs.rb
  23. +1 −0  db/migrate/{20120301213432_create_trips.rb → 20120308215936_create_trips.rb}
  24. +1 −2  db/schema.rb
  25. +49 −0 test/functional/trips_controller_test.rb
  26. +4 −0 test/unit/helpers/trips_helper_test.rb
3  Gemfile
View
@@ -4,7 +4,6 @@ gem 'rails', '3.2.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
-
gem 'sqlite3'
gem 'json'
@@ -22,7 +21,7 @@ group :assets do
end
gem 'jquery-rails'
-gem "nested_form"
+gem "nested_form" , :git => "https://github.com/ryanb/nested_form.git"
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
9 Gemfile.lock
View
@@ -1,3 +1,9 @@
+GIT
+ remote: https://github.com/ryanb/nested_form.git
+ revision: 486e0f0e93f3ca455d5d0fc7869053257b6afce2
+ specs:
+ nested_form (0.2.0)
+
GEM
remote: https://rubygems.org/
specs:
@@ -53,7 +59,6 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.1.0)
- nested_form (0.2.0)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.1)
@@ -107,7 +112,7 @@ DEPENDENCIES
coffee-rails (~> 3.2.1)
jquery-rails
json
- nested_form
+ nested_form!
rails (= 3.2.0)
sass-rails (~> 3.2.3)
sqlite3
1  app/assets/javascripts/application.js
View
@@ -13,5 +13,6 @@
//= require jquery
//= require jquery_ujs
//= require_tree .
+//= require nested_form
7 app/assets/javascripts/legs.js.coffee
View
@@ -2,9 +2,12 @@
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
$ ->
- changeDiv = ->
+ trHighlight = ->
$("table tr").removeClass 'active'
$(@).addClass 'active'
+ notrHighlight = ->
+ $("table tr").removeClass 'active'
- $("table tr").mouseenter changeDiv
+ $("table tr").mouseenter trHighlight
+ $('table').mouseleave notrHighlight
71 app/assets/javascripts/nested_form.js
View
@@ -0,0 +1,71 @@
+jQuery(function($) {
+ window.NestedFormEvents = function() {
+ this.addFields = $.proxy(this.addFields, this);
+ this.removeFields = $.proxy(this.removeFields, this);
+ };
+
+ NestedFormEvents.prototype = {
+ addFields: function(e) {
+ // Setup
+ var link = e.currentTarget;
+ var assoc = $(link).attr('data-association'); // Name of child
+ var content = $('#' + assoc + '_fields_blueprint').html(); // Fields template
+
+ // Make the context correct by replacing new_<parents> with the generated ID
+ // of each of the parent objects
+ var context = ($(link).closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
+
+ // context will be something like this for a brand new form:
+ // project[tasks_attributes][new_1255929127459][assignments_attributes][new_1255929128105]
+ // or for an edit form:
+ // project[tasks_attributes][0][assignments_attributes][1]
+ if (context) {
+ var parentNames = context.match(/[a-z_]+_attributes/g) || [];
+ var parentIds = context.match(/(new_)?[0-9]+/g) || [];
+
+ for(var i = 0; i < parentNames.length; i++) {
+ if(parentIds[i]) {
+ content = content.replace(
+ new RegExp('(_' + parentNames[i] + ')_.+?_', 'g'),
+ '$1_' + parentIds[i] + '_');
+
+ content = content.replace(
+ new RegExp('(\\[' + parentNames[i] + '\\])\\[.+?\\]', 'g'),
+ '$1[' + parentIds[i] + ']');
+ }
+ }
+ }
+
+ // Make a unique ID for the new child
+ var regexp = new RegExp('new_' + assoc, 'g');
+ var new_id = new Date().getTime();
+ content = content.replace(regexp, "new_" + new_id);
+
+ var field = this.insertFields(content, assoc, link);
+ $(link).closest("form")
+ .trigger({ type: 'nested:fieldAdded', field: field })
+ .trigger({ type: 'nested:fieldAdded:' + assoc, field: field });
+ return false;
+ },
+ insertFields: function(content, assoc, link) {
+ return $(content).insertBefore(link);
+ },
+ removeFields: function(e) {
+ var link = e.currentTarget;
+ var hiddenField = $(link).prev('input[type=hidden]');
+ hiddenField.val('1');
+ // if (hiddenField) {
+ // $(link).v
+ // hiddenField.value = '1';
+ // }
+ var field = $(link).closest('.fields');
+ field.hide();
+ $(link).closest("form").trigger({ type: 'nested:fieldRemoved', field: field });
+ return false;
+ }
+ };
+
+ window.nestedFormEvents = new NestedFormEvents();
+ $('form a.add_nested_fields').live('click', nestedFormEvents.addFields);
+ $('form a.remove_nested_fields').live('click', nestedFormEvents.removeFields);
+});
3  app/assets/javascripts/trips.js.coffee
View
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
16 app/assets/stylesheets/legs.css.scss
View
@@ -1,16 +0,0 @@
-// Place all the styles related to the legs controller here.
-// They will automatically be included in application.css.
-// You can use Sass (SCSS) here: http://sass-lang.com/
-
-table {
- background-color:#87CEEB;
- font-size:1.5em;
- font-family:Sans-serif;
-}
-table, th, td {
- border: 2px solid white;
-}
-.active {
- background-color:#FFFFFF;
- font-weight:bold;
-}
3  app/assets/stylesheets/trips.css.scss
View
@@ -0,0 +1,3 @@
+// Place all the styles related to the trips controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
73 app/controllers/trips_controller.rb
View
@@ -1,20 +1,83 @@
class TripsController < ApplicationController
-
+ # GET /trips
+ # GET /trips.json
def index
-
+ @trips = Trip.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @trips }
+ end
+ end
+
+ # GET /trips/1
+ # GET /trips/1.json
+ def show
+ @trip = Trip.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @trip }
+ end
end
+ # GET /trips/new
+ # GET /trips/new.json
def new
@trip = Trip.new
- @trip.legs.build
- end
- def create
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @trip }
+ end
end
+ # GET /trips/1/edit
def edit
+ @trip = Trip.find(params[:id])
+ end
+
+ # POST /trips
+ # POST /trips.json
+ def create
+ @trip = Trip.new(params[:trip])
+
+ respond_to do |format|
+ if @trip.save
+ format.html { redirect_to @trip, notice: 'Trip was successfully created.' }
+ format.json { render json: @trip, status: :created, location: @trip }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @trip.errors, status: :unprocessable_entity }
+ end
+ end
end
+ # PUT /trips/1
+ # PUT /trips/1.json
def update
+ @trip = Trip.find(params[:id])
+
+ respond_to do |format|
+ if @trip.update_attributes(params[:trip])
+ format.html { redirect_to @trip, notice: 'Trip was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @trip.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /trips/1
+ # DELETE /trips/1.json
+ def destroy
+ @trip = Trip.find(params[:id])
+ @trip.destroy
+
+ respond_to do |format|
+ format.html { redirect_to trips_url }
+ format.json { head :no_content }
+ end
end
end
2  app/helpers/trips_helper.rb
View
@@ -0,0 +1,2 @@
+module TripsHelper
+end
1  app/models/leg.rb
View
@@ -2,6 +2,7 @@ class Leg < ActiveRecord::Base
validates_presence_of :location, :on => :create, :message => "can't be blank"
validates_presence_of :depart_arrive, :on => :create, :message => "can't be blank"
validate :depart_or_arrive, :on => :create
+ has_one :trip
def depart_or_arrive
if depart_arrive_type != 'Departure' && depart_arrive_type != 'Arrival'
errors.add(:depart_arrive_type, "must be either 'Depart' or 'Arrive'")
5 app/models/trip.rb
View
@@ -1,5 +1,4 @@
class Trip < ActiveRecord::Base
- has_many :legs
-
- accepts_nested_attributes_for :legs
+ has_many :legs, :dependent => :destroy
+ accepts_nested_attributes_for :legs, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
4 app/views/layouts/application.html.erb
View
@@ -5,7 +5,11 @@
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
+
+
+
<%= csrf_meta_tags %>
+
</head>
<body>
37 app/views/trips/_form.html.erb
View
@@ -1,10 +1,10 @@
-<%= form_for(@trip) do |f| %>
+<%= nested_form_for @trip do |f| %>
<% if @trip.errors.any? %>
<div id="error_explanation">
- <h2><%= pluralize(@leg.errors.count, "error") %> prohibited this leg from being saved:</h2>
+ <h2><%= pluralize(@trip.errors.count, "error") %> prohibited this trip from being saved:</h2>
<ul>
- <% @leg.errors.full_messages.each do |msg| %>
+ <% @trip.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
@@ -13,23 +13,28 @@
<div class="field">
<%= f.label :name %><br />
- <%= f.text_field :name, :size=>30 %>
+ <%= f.text_field :name %>
</div>
- <%= f.fields_for :legs do |l| %>
<div class="field">
- <%= l.label :location %><br />
- <%= l.text_field :location %>
- </div>
- <div class="field">
- <%= l.label :depart_arrive_type %><br />
- <%= l.select :depart_arrive_type, ["Departure", "Arrival"] %>
-
- </div>
- <div class="field">
- <%= l.label :depart_arrive %><br />
- <%= l.datetime_select :depart_arrive %>
+ <%= f.label :email %><br />
+ <%= f.text_field :email %>
</div>
+ <div class="field">
+ <%= f.fields_for :legs do |legs_form| %>
+ <%= legs_form.label :location, "Location:" %><br />
+ <%= legs_form.text_field :location %><br />
+
+ <%= legs_form.label :depart_arrive_type, "Departure or Arrival?" %><br />
+ <%= legs_form.select :depart_arrive_type, ["Departure", "Arrival"] %><br />
+
+ <%= legs_form.label :depart_arrive, "Departure/Arrival Date:" %><br />
+ <%= legs_form.datetime_select :depart_arrive %> <br />
+ <%= legs_form.label :details, "Leg details:" %> <br />
+ <%= legs_form.text_field :details %>
+ <%= legs_form.link_to_remove "Remove Leg" %>
<% end %>
+ <p><%= f.link_to_add "Add a Leg", :legs %></p>
+ </div>
<div class="actions">
<%= f.submit %>
</div>
2  app/views/trips/create.html.erb
View
@@ -1,2 +0,0 @@
-<h1>Trip#create</h1>
-<p>Find me in app/views/trip/create.html.erb</p>
8 app/views/trips/edit.html.erb
View
@@ -1,2 +1,6 @@
-<h1>Trip#edit</h1>
-<p>Find me in app/views/trip/edit.html.erb</p>
+<h1>Editing trip</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @trip %> |
+<%= link_to 'Back', trips_path %>
27 app/views/trips/index.html.erb
View
@@ -1,2 +1,25 @@
-<h1>All Trips</h1>
-<p>Find me in app/views/trip/index.html.erb</p>
+<h1>Listing trips</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Email</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @trips.each do |trip| %>
+ <tr>
+ <td><%= trip.name %></td>
+ <td><%= trip.email %></td>
+ <td><%= link_to 'Show', trip %></td>
+ <td><%= link_to 'Edit', edit_trip_path(trip) %></td>
+ <td><%= link_to 'Destroy', trip, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Trip', new_trip_path %>
5 app/views/trips/new.html.erb
View
@@ -1,2 +1,5 @@
<h1>New Trip</h1>
-<%= render "form" %>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', trips_path %>
15 app/views/trips/show.html.erb
View
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Name:</b>
+ <%= @trip.name %>
+</p>
+
+<p>
+ <b>Email:</b>
+ <%= @trip.email %>
+</p>
+
+
+<%= link_to 'Edit', edit_trip_path(@trip) %> |
+<%= link_to 'Back', trips_path %>
2  app/views/trips/update.html.erb
View
@@ -1,2 +0,0 @@
-<h1>Trip#update</h1>
-<p>Find me in app/views/trip/update.html.erb</p>
2  config/routes.rb
View
@@ -1,7 +1,7 @@
Tripd::Application.routes.draw do
-
resources :trips
+
resources :legs
# The priority is based upon order of creation:
6 db/migrate/20120301214027_add_trip_id_to_legs.rb
View
@@ -1,6 +0,0 @@
-class AddTripIdToLegs < ActiveRecord::Migration
- def change
- add_column :legs, :trip_id, :integer
-
- end
-end
1  db/migrate/20120301213432_create_trips.rb → db/migrate/20120308215936_create_trips.rb
View
@@ -3,6 +3,7 @@ def change
create_table :trips do |t|
t.string :name
t.string :email
+
t.timestamps
end
end
3  db/schema.rb
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120301214027) do
+ActiveRecord::Schema.define(:version => 20120308213926) do
create_table "legs", :force => true do |t|
t.string "location"
@@ -20,7 +20,6 @@
t.text "details"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
- t.integer "trip_id"
end
create_table "trips", :force => true do |t|
49 test/functional/trips_controller_test.rb
View
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class TripsControllerTest < ActionController::TestCase
+ setup do
+ @trip = trips(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:trips)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create trip" do
+ assert_difference('Trip.count') do
+ post :create, trip: @trip.attributes
+ end
+
+ assert_redirected_to trip_path(assigns(:trip))
+ end
+
+ test "should show trip" do
+ get :show, id: @trip
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @trip
+ assert_response :success
+ end
+
+ test "should update trip" do
+ put :update, id: @trip, trip: @trip.attributes
+ assert_redirected_to trip_path(assigns(:trip))
+ end
+
+ test "should destroy trip" do
+ assert_difference('Trip.count', -1) do
+ delete :destroy, id: @trip
+ end
+
+ assert_redirected_to trips_path
+ end
+end
4 test/unit/helpers/trips_helper_test.rb
View
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class TripsHelperTest < ActionView::TestCase
+end

No commit comments for this range

Something went wrong with that request. Please try again.