Skip to content

Commit

Permalink
Ajaxif'd the dashboard.
Browse files Browse the repository at this point in the history
Updated .gitignore with more temp files;
  • Loading branch information
James Lee committed Apr 14, 2013
1 parent 7d60aed commit b0d7e37
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -13,6 +13,10 @@
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
/out

# Ignoring OSX files
.DS_Store

# Ignore all Intellij Files
/*.iml
Expand Down
9 changes: 9 additions & 0 deletions app/assets/javascripts/dashboard.js.coffee
@@ -1,3 +1,12 @@
# 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/
jQuery ->
$('#new_commute').bind 'ajax:success', (event, data, status, xhr) ->
console.log data
$('#cash_score').text data.cash_score
$('#cal_score').text data.cal_score
$('#envfp_score').text data.envfp_score
$('.score').show()
.bind 'ajax:error', ->
alert 'No!'
14 changes: 10 additions & 4 deletions app/controllers/commutes_controller.rb
Expand Up @@ -44,8 +44,11 @@ def create

respond_to do |format|
if @commute.save
redirect_to dashboard_path(@commute), notice: 'Commute was successfully created.'
format.json { render json: @commute, status: :created, location: @commute }
format.html { render action: "show" }
format.json { render json: @commute.attributes.merge({:cal_score => @commute.cal_score,
:cash_score => @commute.cash_score,
:envfp_score => @commute.envfp_score
}), status: :ok, location: @commute }
else
format.html { render action: "new" }
format.json { render json: @commute.errors, status: :unprocessable_entity }
Expand All @@ -60,8 +63,11 @@ def update

respond_to do |format|
if @commute.update_attributes(params[:commute])
redirect_to dashboard_path(@commute), notice: 'Commute was successfully update.'
format.json { head :no_content }
format.html { render action: "show" }
format.json { render json: @commute.attributes.merge({:cal_score => @commute.cal_score,
:cash_score => @commute.cash_score,
:envfp_score => @commute.envfp_score
}), status: :ok, location: @commute }

This comment has been minimized.

Copy link
@vosechu

vosechu Apr 17, 2013

Serializer objects would help reduce this duplication a little bit. Easier to do render json: CommuteSerializer.new(@commute). Now you can run tests and ensure that status and location are getting set correctly. Otherwise I would worry about coworkers mangling it. JSON is so delicate sometimes.

This comment has been minimized.

Copy link
@misttar

misttar Apr 24, 2013

Collaborator

Wow, the serializer stuff was excellent. Just added a gem, and created what I wanted, and no more merging crap. And I didn't have to do the CummteSerializer.new. The serializer is detected automatically, and used when you turn a commute into json.

Lots of love for this.

else
format.html { render action: "edit" }
format.json { render json: @commute.errors, status: :unprocessable_entity }
Expand Down
30 changes: 18 additions & 12 deletions app/models/commute.rb
Expand Up @@ -11,28 +11,34 @@ class Commute < ActiveRecord::Base
def cash_score
# calculate cash score
# car cash value - other cash value
if cash_fr
(CommuteType.baseline.cash - cash)
else
(CommuteType.baseline.cash * distance) * (1 + passenger) - (cash * distance)
if id?

This comment has been minimized.

Copy link
@vosechu

vosechu Apr 17, 2013

If you're asking about whether the model is new or not, consider new_record?. It's a little more stable than asking about the id since some dbs don't use sequential ids (and some don't use ids at all). http://apidock.com/rails/ActiveRecord/Persistence/new_record%3F

This comment has been minimized.

Copy link
@misttar

misttar Apr 24, 2013

Collaborator

That's what I get for learning from the internet, didn't know about new_record. Thatnks!

if cash_fr
(CommuteType.baseline.cash - cash)
else
(CommuteType.baseline.cash * distance) * (1 + passenger) - (cash * distance)
end
end
end

def cal_score
# calculate calories score
if calories_fr
calories
else
calories * distance
if id?
if calories_fr
calories
else
calories * distance
end
end
end

def envfp_score
# calculate envfp score
if envfp_fr
(CommuteType.baseline.envfp - envfp)
else
(CommuteType.baseline.envfp * distance) * (1 + passenger) - (envfp * distance)
if id?
if envfp_fr
(CommuteType.baseline.envfp - envfp)
else
(CommuteType.baseline.envfp * distance) * (1 + passenger) - (envfp * distance)
end
end
end
end
4 changes: 2 additions & 2 deletions app/views/commutes/index.html.erb
Expand Up @@ -17,8 +17,8 @@
<tbody>
<% @commutes.each do |commute| %>
<tr>
<td><%= link_to commute.name, commute_path(commute) %></td>
<td><%= commute.commute_type %></td>
<td><%= link_to commute.name, dashboard_path(commute) %></td>
<td><%= commute.commute_type.name %></td>
<td><%= commute.start %></td>
<td><%= commute.end %></td>
<td><%= commute.distance %></td>
Expand Down
4 changes: 2 additions & 2 deletions app/views/dashboard/_form.html.erb
Expand Up @@ -2,15 +2,15 @@
<h4 style="color:#BDC3C7; text-align:right; font-size:1.1em; margin-right:40px;"><%= Time.now.strftime("%A, %B %d %Y") %></h4>
</div><!-- end row -->
<div class="row" style=" margin-bottom:20px;">
<%= form_for @commute, :html => { :class => 'form-horizontal' } do |f| %>
<%= form_for @commute, :html => { :class => 'form-horizontal', :'data-type' => 'json' }, :remote => true do |f| %>

This comment has been minimized.

Copy link
@vosechu

vosechu Apr 17, 2013

<3


<h1 class="text-center" style="margin: 25px 0;">Today I am
<%= f.collection_select :commute_type_id, CommuteType.all, :id, :name, :class => 'collection_select' %> to
<%= f.collection_select :distance, Destination.all, :distance, :name, :class => 'collection_select' %> with
<%= f.select( "passenger", { "0 people" => "0", "1 person" => "1", "2 people" => "2", "3 people" => "3"}) %>
</h1>
<div class="form-actions">
<%= f.submit "Save Commute", :class => 'btn btn-primary' %>
<%= f.submit 'Save Commute', :class => 'btn btn-primary' %>
</div>
<% end %>
</div><!-- end row -->
8 changes: 5 additions & 3 deletions app/views/dashboard/_score.html.erb
@@ -1,9 +1,10 @@
<div class="score" <% unless @commute.id? %>style="display: none"<% end %> >
<div class="row">
<div class="span2" style="padding-left:25px;">
<%= image_tag "icons/icon-env.svg" %>
</div>
<div class="span6" style="margin-top:30px;">
<h4>You will save $<%= @commute.cash_score %>.</h4>
<h4>You will save $<span id='cash_score'><%= @commute.cash_score || 0 %></span>.</h4>
</div>
<div class="span4" style="margin-top:30px;">
<div class="progress">
Expand All @@ -20,7 +21,7 @@
<%= image_tag "icons/icon-health.svg" %>
</div>
<div class="span6" style="margin-top:30px;">
<h4>You will burn <%= @commute.cal_score %> calories.</h4>
<h4>You will burn <span id='cal_score'><%= @commute.cal_score || 0 %></span> calories.</h4>
</div>
<div class="span4" style="margin-top:30px;">
<div class="progress">
Expand All @@ -37,7 +38,7 @@
<%= image_tag "icons/icon-money.svg" %>
</div>
<div class="span6" style="margin-top:30px;">
<h4>You will reduce your carbon footprint by <%= @commute.envfp_score %> grams.</h4>
<h4>You will reduce your carbon footprint by <span id='envfp_score'><%= @commute.envfp_score || 0%></span> grams.</h4>
</div>
<div class="span4" style="margin-top:30px;">
<div class="progress">
Expand All @@ -51,3 +52,4 @@
</div><!-- end row -->
<div class="row" style="background:#1ABC9C; height:150px; margin-top:25px; margin-bottom:25px; margin-right:0; width: 100%;">
</div><!-- end row -->
</div>
1 change: 1 addition & 0 deletions app/views/dashboard/main.html.erb
@@ -1,5 +1,6 @@
<div class="container">
<%= render :partial => 'form' %>
<%= render :partial => 'score' %>
<%= render :partial => 'rewards' %>
</div>

Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Expand Up @@ -9,9 +9,9 @@

root :to => 'dashboard#intro'

match '/dashboard' => 'dashboard#main'
match 'dashboard' => 'dashboard#main'

get 'dashboard/:id', :to => 'dashboard#show'
match 'dashboard/:id', :to => 'dashboard#show', :as => 'dashboard'

This comment has been minimized.

Copy link
@vosechu

vosechu Apr 17, 2013

I'm sad about this too, but match is deprecated in Rails 4. rails/rails#5964

This comment has been minimized.

Copy link
@misttar

misttar Apr 24, 2013

Collaborator

I see there point... swapped everything out for more specific matchers.


# The priority is based upon order of creation:
# first created -> highest priority.
Expand Down

0 comments on commit b0d7e37

Please sign in to comment.