Permalink
Browse files

finish charts

  • Loading branch information...
1 parent 46c6465 commit 13a8186e676af0fd067fb62ede131178d1d17e32 Luis Gutierrez committed Oct 4, 2012
@@ -1,112 +1,66 @@
class CarsController < ApplicationController
+ respond_to :json,:html
+
before_filter :get_car, except: [:index,:new,:create]
def index
if user_signed_in?
-
@user = current_user
-
@cars = Car.cars(@user)
-
- respond_to do |format|
- format.html
- format.json { render json: @cars }
- end
-
+ respond_with @cars
else
-
flash.now[:error] = "You must be logged in to access this section, please sign in."
redirect_to new_user_session_path
-
end
end
def new
-
@car = Car.new
-
- respond_to do |format|
- format.html
- format.json { render json: @car }
- end
-
+ respond_with @car
end
def create
-
@car = Car.new(params[:car])
@user = current_user.id
@car.user_id = @user
-
if @car.save
- respond_to do |format|
- format.html { redirect_to cars_path, success: 'Car was successfully created.' }
- format.json { render json: @car, status: :created, location: @car }
- end
-
+ respond_with @car
else
- respond_to do |format|
- format.html { render action: "new", notice: 'Car was not successfully created.' }
- format.json { render json: @car.errors, status: :unprocessable_entity }
- end
- end
+ respond_with @car.errors
+ end
end
def show
-
- respond_to do |format|
- format.html
- format.json { render json: @car }
- end
-
+ respond_with @car
end
def edit
-
- respond_to do |format|
- format.html
- format.json { render json: @car }
- end
-
+ respond_with @car
end
def update
if @car.update_attributes(params[:car])
-
- respond_to do |format|
- format.html { redirect_to cars_path }
- format.json { head :no_content }
- end
-
+ respond_with :no_content
else
- respond_to do |format|
- format.html { render :edit }
- format.json { render json: @car.errors, status: :unprocessable_entity }
- end
-
+ respond_with @car.errors
end
end
def destroy
-
@car.destroy
- respond_to do |format|
- format.html { redirect_to cars_path }
- format.json { head :no_content }
- end
-
+ respond_with :no_content
end
@@ -1,29 +1,25 @@
class ChartsController < ApplicationController
+
+ respond_to :json, :html
+
def index
if user_signed_in?
+ @user = current_user
- /Station with gasoline more expensive/
-
-
-
- /Station where gasoline yields more/
-
-
+ @report1 = GasStation.gasoline_more_expensive
+ @report2 = GasStation.gasoline_yields_more
- /'History of Tanking by car'/
- @user = current_user
- @report3 = TankingLog.joins(:car, :gas_station).where("cars.user_id = ?",@user.id).group(:car_id).count(:id)
- respond_to do |format|
- format.html
- format.json { render json: @report3 }
- end
+ @final_report = { :report1 => @report1, :report2 => @report2 }
+
+ respond_with @final_report
else
flash.now[:error] = "You must be logged in to access this section, please sign in."
redirect_to new_user_session_path
end
end
+
end
@@ -1,16 +1,14 @@
class GasStationsController < ApplicationController
-
+
+ respond_to :json, :html
before_filter :get_gstation, except: [:index,:new,:create]
def index
if user_signed_in?
@gas_stations = GasStation.all
- respond_to do |format|
- format.html
- format.json { render json: @gas_stations }
- end
+ respond_with @gas_stations
else
flash.now[:error] = "You must be logged in to access this section, please sign in."
@@ -21,72 +19,41 @@ def index
def new
@gas_station = GasStation.new
-
- respond_to do |format|
- format.html
- format.json { render json: @gas_station }
- end
-
+ respond_with @gas_station
end
def create
@gas_station = GasStation.new(params[:gas_station])
if @gas_station.save
- respond_to do |format|
- format.html { redirect_to gas_stations_path, notice: 'Gas Station was successfully created.' }
- format.json { render json: @gas_station, status: :created, location: @gas_station }
- end
-
+ respond_with @gas_station
else
- respond_to do |format|
- format.html { render action: "new" }
- format.json { render json: @gas_station.errors, status: :unprocessable_entity }
- end
+ respond_with @gas_station.errors
end
end
def show
- respond_to do |format|
- format.html
- format.json { render json: @gas_station }
- end
+ respond_with @gas_station
end
def edit
- respond_to do |format|
- format.html
- format.json { render json: @gas_station }
- end
+ respond_with @gas_station
end
def update
if @gas_station.update_attributes(params[:gas_station])
-
- respond_to do |format|
- format.html { redirect_to gas_stations_path }
- format.json { head :no_content }
- end
-
+ respond_with :no_content
else
-
- respond_to do |format|
- format.html { render :edit }
- format.json { render json: @gas_station.errors, status: :unprocessable_entity }
- end
-
+ respond_with @gas_station.errors
end
end
def destroy
@gas_station.destroy
- respond_to do |format|
- format.html { redirect_to gas_stations_path }
- format.json { head :no_content }
- end
+ respond_with :no_content
end
private
@@ -1,5 +1,6 @@
class TankingLogsController < ApplicationController
-
+
+ respond_to :json, :html
before_filter :get_tank, except: [:index,:new,:create]
def index
@@ -9,10 +10,7 @@ def index
@user = current_user
@tanking_logs = TankingLog.all_user_logs(@user)
- respond_to do |format|
- format.html
- format.json { render json: @tanking_logs }
- end
+ respond_with @tanking_logs
else
flash.now[:error] = "You must be logged in to access this section, please sign in."
@@ -24,77 +22,46 @@ def index
def new
@tanking_log = TankingLog.new
-
- respond_to do |format|
- format.html
- format.json { render json: @tanking_log }
- end
-
+ respond_with @tanking_log
end
def create
@tanking_log = TankingLog.new(params[:tanking_log])
if @tanking_log.save
- respond_to do |format|
- format.html { redirect_to tanking_logs_path, notice: 'Tanking logs was successfully created.' }
- format.json { render json: @tanking_log, status: :created, location: @tanking_log }
- end
-
+ respond_with @tanking_log
else
- respond_to do |format|
- format.html { render action: "new" }
- format.json { render json: @tanking_log.errors, status: :unprocessable_entity }
- end
+ respond_with @tanking_log.errors
end
end
def show
- respond_to do |format|
- format.html
- format.json { render json: @tanking_log }
- end
+ respond_with @tanking_log
end
def edit
- respond_to do |format|
- format.html
- format.json { render json: @tanking_log }
- end
+ respond_with @tanking_log
end
def update
if @tanking_log.update_attributes(params[:tanking_log])
-
- respond_to do |format|
- format.html { redirect_to tanking_logs_path }
- format.json { head :no_content }
- end
-
+ respond_with :no_content
else
-
- respond_to do |format|
- format.html { render :edit }
- format.json { render json: @tanking_log.errors, status: :unprocessable_entity }
- end
-
+ respond_with @tanking_log.errors
end
end
def destroy
@tanking_log.destroy
- respond_to do |format|
- format.html { redirect_to tanking_logs_path }
- format.json { head :no_content }
- end
+ respond_with :no_content
end
View
@@ -1,7 +1,20 @@
class GasStation < ActiveRecord::Base
- has_many :tanking_logs
+
+ has_many :tanking_logs
attr_accessible :name
+ validates_presence_of :name
+
+ class << self
+
+ def gasoline_more_expensive
+ maximum("tanking_logs.cost", joins: :tanking_logs, :group => :name)
+ end
+
+ def gasoline_yields_more
+ maximum("(tanking_logs.gallon/tanking_logs.km)", :joins => :tanking_logs, :group => :name)
+ end
+
+ end
- validates_presence_of :name
end
Oops, something went wrong.

0 comments on commit 13a8186

Please sign in to comment.