Skip to content

Commit 5076bf8

Browse files
authored
Merge pull request chi-otters-2017#48 from chi-otters-2017/merge-helper
Merge helper
2 parents 8189e3f + 1bb9bda commit 5076bf8

File tree

13 files changed

+92
-49
lines changed

13 files changed

+92
-49
lines changed

app/assets/javascripts/measures.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
// Place all the behaviors and hooks related to the matching controller here.
22
// All this logic will automatically be available in application.js.
3+
4+
$(document).on('ready', function(){
5+
$('#recipe-data form').on('submit', function(e) {
6+
e.preventDefault();
7+
var data = $(this).serialize();
8+
var url = $(this).attr("action");
9+
var $section = $(this).closest('#recipe-data');
10+
11+
$.ajax({
12+
url: url,
13+
type: 'POST',
14+
data: data
15+
})
16+
.done(function(response) {
17+
$section.append(response);
18+
});
19+
20+
});
21+
22+
});

app/controllers/measures_controller.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class MeasuresController < ApplicationController
2-
def new
3-
@recipe = Recipe.find_by(id: params[:recipe_id])
4-
@measure = Measure.new
5-
end
2+
# def new
3+
# # @recipe = Recipe.find_by(id: params[:recipe_id])
4+
# @measure = Measure.new
5+
# puts "IM IN MESURES"
6+
# end
67

78
def create
89
@recipe = Recipe.find_by(id: params[:recipe_id])
910
@measure = @recipe.measures.new(measure_deets)
1011
if @measure.save
11-
redirect_to new_recipe_measure_path(@recipe)
12+
redirect_to recipe_path(@recipe)
1213
else
1314
raise 'ur dum'
1415
end

app/controllers/ratings_controller.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
class RatingsController < ApplicationController
22
include SessionsHelper
3+
include RatingsHelper
34

45
def create
56
get_recipe
7+
if !already_rated?(@recipe)
8+
@rating = @recipe.ratings.new(rating_params)
9+
@rating.update_attributes(user_id: current_user.id)
10+
if @rating.save
11+
flash[:notice] = "Rating has been recorded."
612

7-
@rating = @recipe.ratings.new(rating_params)
8-
@rating.update_attributes(user_id: current_user.id)
9-
if @rating.save
10-
flash[:notice] = "Rating has been recorded."
11-
12-
redirect_to recipe_path(@recipe)
13+
redirect_to recipe_path(@recipe)
14+
else
15+
render file: 'public/404.html'
16+
end
1317
else
1418
render file: 'public/404.html'
1519
end

app/controllers/recipes_controller.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@ def new
1111
def create
1212
@recipe = get_category.recipes.new(recipe_params)
1313
current_user.recipes << @recipe
14-
15-
if @recipe.save
16-
flash.notice = "Your recipe has been added!"
17-
18-
redirect_to "/categories/#{@category.name}", notice: "You recipe was successfully added!"
14+
if request.xhr?
15+
if @recipe.save
16+
flash.notice = "Your recipe has been added!"
17+
render partial: 'measures/new', recipe: @recipe
18+
else
19+
render :new
20+
end
1921
else
20-
render :new
22+
if @recipe.save
23+
flash.notice = "Your recipe has been added!"
24+
25+
redirect_to "/categories/#{@category.name}", notice: "You recipe was successfully added!"
26+
else
27+
render :new
28+
end
2129
end
2230
end
2331

app/controllers/sales_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ def index
99
def create
1010
@sale = Sale.new(sale_deets)
1111
@sale.recipe_id = recipe_params[:recipe_id]
12-
raise @sale.inspect
1312
if @sale.save
1413
flash[:notice] = 'Success!'
15-
redirect_to :index
14+
redirect_to recipe_sales_path(@sale)
1615
else
1716
flash[:alert] = "Save failed!"
1817
redirect_to :index

app/controllers/users_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def destroy
4343
redirect_to users_path
4444
else
4545
flash[:not_admin] = "You do not have permission to view this page."
46-
redirect_to "/"
46+
redirect_to users_path
4747
end
4848
end
4949

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module RatingHelper
1+
module RatingsHelper
22
def recipe_rating(recipe)
33
total = 0
44
recipe.ratings.each do |rating|

app/models/sale.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ class Sale < ApplicationRecord
66

77

88
def gross_sales
9-
self.cents.dup.insert(-2, '.').insert(0, '$')
9+
['$', self.cents / 100, ".00"].join
1010
end
1111

1212
private
1313
def set_cents
14-
working_price = self.sale_price
15-
raise working_price.inspect
16-
unless working_price.include?('.')
14+
working_price = self.sale_price.to_s
15+
if working_price.include?('.')
1716
working_price.concat('00')
1817
end
19-
clean_price = working_price.split('').delete_if{|char| (char =~ /[[:punct:][$][\s]]/)}.join
20-
clean_price.to_i *= self.volume
21-
self.cents = clean_price.to_s
18+
clean_price = working_price.split('').delete_if{|char| (char =~ /[[:punct:][$][\s]]/)}.join.to_i
19+
clean_price *= self.volume
20+
self.cents = clean_price
2221
end
2322
end
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</div>
1313
<div class='recipe-ingredient container'>
1414
<% if @recipe.measures %>
15-
<h4><%= @recipe.title%></h4>
15+
1616
<ul>
1717
<% @recipe.measures.each do |ingred| %>
1818
<li>
@@ -23,7 +23,7 @@
2323
<% end %>
2424
</div>
2525

26-
<%= form_for @measure, url: recipe_measures_path(@recipe) do |f|%>
26+
<%= form_for Measure.new, url: recipe_measures_path(@recipe) do |f|%>
2727
<span> <%= f.label :quantity %>
2828
<%= f.text_field :quantity %>
2929
<%= f.label :units %>

app/views/recipes/new.html.erb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@
88
<% end %>
99
<% end %>
1010

11-
<%= form_for @recipe, url: category_recipes_path do |f| %>
11+
<section id='recipe-data'>
12+
<%= form_for @recipe, url: category_recipes_path do |f| %>
1213

13-
<p><%= f.label :title %></p>
14-
<p><%= f.text_field :title %></p>
14+
<p><%= f.label :title %></p>
15+
<p><%= f.text_field :title %></p>
1516

16-
<p><%= f.label :directions %></p>
17-
<p><%= f.text_area :directions %></p>
17+
<p><%= f.label :directions %></p>
18+
<p><%= f.text_area :directions %></p>
1819

19-
<p><%= f.label :time %></p>
20-
<span><%= f.text_field :time %></span>
21-
<span><%= f.label "in minutes" %></span>
20+
<p><%= f.label :time %></p>
21+
<span><%= f.text_field :time %></span>
22+
<span><%= f.label "in minutes" %></span>
2223

23-
<p><%= f.label :difficulty %></p>
24-
<p><%= f.select :difficulty, options_for_select([['easy', 'easy'], ['medium', 'medium'], ['hard', 'hard']]) %></p><br>
24+
<p><%= f.label :difficulty %></p>
25+
<p><%= f.select :difficulty, options_for_select([['easy', 'easy'], ['medium', 'medium'], ['hard', 'hard']]) %></p><br>
26+
27+
<p><%= f.submit "Submit" %></p>
28+
<% end %>
29+
</section>
2530

26-
<%= f.submit "Add My Recipe!" %>
2731

28-
<% end %>
2932
</article>

0 commit comments

Comments
 (0)