Skip to content

Commit

Permalink
mostly view fixes for new helper & wrapping gravatar, slight changes …
Browse files Browse the repository at this point in the history
…to workout controller & updated workout test suite
  • Loading branch information
Mirv committed Jun 23, 2017
1 parent 07708f6 commit 66a540d
Show file tree
Hide file tree
Showing 23 changed files with 180 additions and 186 deletions.
3 changes: 0 additions & 3 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def search
else
@recipes = Recipe.all
end

end

def index
Expand Down Expand Up @@ -51,8 +50,6 @@ def update
flash[:success] = "Your recipe was update success"
redirect_to recipe_path(@recipe)
else
puts "\n\n Recipe debug: #{YAML::dump(params)} \n\n"

render :edit
end
end
Expand Down
25 changes: 15 additions & 10 deletions app/controllers/workouts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class WorkoutsController < ApplicationController
before_action :set_workout, only: [:edit, :update, :show, :like, :review, :destroy]
before_action :authenticate_user!, except: [:show, :index, :like, :search]
before_action :authenticate_user!, except: [:show, :like, :search]
before_action :require_same_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
# before_action :admin_user, only: :destroy

def index
@workouts = Workout.paginate(page: params[:page], per_page: 5)
Expand All @@ -23,7 +23,7 @@ def create

if @workout.save
flash[:success] = "Your workout was created successfully!"
redirect_to workouts_path
redirect_to workout_url(@workout)
else
render :new
end
Expand Down Expand Up @@ -61,7 +61,7 @@ def update
def destroy
Workout.find(params[:id]).destroy
flash[:success] = "Workout was deleted."
redirect_to workout_url
redirect_to workouts_path
end

def review
Expand Down Expand Up @@ -94,7 +94,11 @@ def like
private

def workout_params
params.require(:workout).permit(:name, :summary, :attachment, :user_id, movements_attributes: [:id, :name, :_destroy], reps_attributes: [:id, :amount, :_destroy], zets_attributes: [:id, :quantity, :_destroy], weights_attributes: [:id, :kilogram, :_destroy],)
params.require(:workout).permit(:name, :summary, :attachment, :user_id,
movements_attributes: [:id, :name, :_destroy],
reps_attributes: [:id, :amount, :_destroy],
zets_attributes: [:id, :quantity, :_destroy],
weights_attributes: [:id, :kilogram, :_destroy],)
end

def set_workout
Expand All @@ -103,13 +107,14 @@ def set_workout

def require_same_user
if current_user != @workout.user and !current_user.admin?
flash[:danger] = "You can only edit your own recipes"
redirect_to recipes_path
# flash[:danger] = "You can only edit your own recipes"
# redirect_to recipes_path
end
end

def admin_user
redirect_to workouts_path unless current_user.admin?
# puts "Current user ... #{current_user.admin?}"
# redirect_to workouts_path unless current_user.admin?
end

end
21 changes: 14 additions & 7 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@ module ApplicationHelper
# Returns the full title on a per-page basis.
def full_title(page_title = '')
base_title = "FitnessLabs (A & K LABS)"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
return page_title.empty? ? base_title : page_title + " | " + base_title
end

def user?(user)
return true unless user.nil?
return false
user.nil? ? false : true
end

def owner_check(owner)
current_user.id == owner
end

def call_gravatar(user)
# Wrapper for the gravatar_for in application_helper
if user?(user)
render partial: 'shared/gravatar_section', locals: {user: user}
else
return "Nothing to see here"
end
end

def gravatar_for(user, options = { size: 80})
if !user.nil?
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.first_name, class: "gravatar")
else
"Gravatar User is nil!"
end
end

def active_page(active_page)
Expand Down
1 change: 1 addition & 0 deletions app/models/movement.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Movement < ActiveRecord::Base
belongs_to :exercise
belongs_to :workout
validates :name, presence: true
end
1 change: 1 addition & 0 deletions app/models/rep.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Rep < ActiveRecord::Base
belongs_to :exercise
belongs_to :workout
validates :amount, presence: true
end
17 changes: 7 additions & 10 deletions app/models/workout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ class Workout < ActiveRecord::Base

validates :name, presence: true
validates :summary, presence: true
validates :movements, presence: true
validates :reps, presence: true
validates :zets, presence: true

def thumbs_up_total
self.likes.where(like: true).size
end

def thumbs_up_total
self.likes.where(like: true).size
end

def thumbs_down_total
self.likes.where(like: false).size
end
def thumbs_down_total
self.likes.where(like: false).size
end
end
2 changes: 2 additions & 0 deletions app/models/zet.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Zet < ActiveRecord::Base
belongs_to :exercise
belongs_to :workout

validates :quantity, presence: true
end
1 change: 1 addition & 0 deletions app/views/ingredients/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<div class="col-md-2">
<section class="chef_info_center">
<%= link_to gravatar_for(recipe.chef, size: 100), chef_path(recipe.chef) %>

<h5> Chef name: <%= recipe.chef.chefname %></h5>
</section>
</div>
Expand Down
4 changes: 0 additions & 4 deletions app/views/recipes/_gravatar_section.html.erb

This file was deleted.

58 changes: 24 additions & 34 deletions app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,35 @@

<%= will_paginate %>
<% @recipes.each do |recipe| %>


<div class="row">

<div class="col-md-2">
<section class="chef_info_center">



</section>
<% call_gravatar(recipe.user) %>
</div>

<div class="col-lg-6 well">
<h4><%= link_to recipe.name, recipe_path(recipe) %></h4>
<p><%= recipe.summary %></p>
<% if recipe.styles.any? %>
<p>Styles: <%= render recipe.styles %></p>
<% end %>
<% if recipe.ingredients.any? %>
<p>Ingredients: <%= render recipe.ingredients %></p>
<% end %>

<p>Feeds: <%= recipe.servings_made %></p>

<p>Preptime: <%= recipe.prep_times %></p>


<p>
<span class='quiet'><small>Created: <%= time_ago_in_words(recipe.created_at)%> ago</small></span><br>
<span class='quiet'><small>Last edited: <%= time_ago_in_words(recipe.updated_at)%> ago</small></span>
</p>
<div class="pull-right">
<%= link_to like_recipe_path(recipe, like: true), method: :post do %>
<i class="glyphicon glyphicon-thumbs-up"></i> &nbsp <%= recipe.thumbs_up_total %>
<% end %> &nbsp&nbsp&nbsp&nbsp
<%= link_to like_recipe_path(recipe, like: false), method: :post do %>
<i class="glyphicon glyphicon-thumbs-down"></i> &nbsp <%= recipe.thumbs_down_total %>
<% end %>
</div>
<h4><%= link_to recipe.name, recipe_path(recipe) %></h4>
<p><%= recipe.summary %></p>
<% if recipe.styles.any? %>
<p>Styles: <%= render recipe.styles %></p>
<% end %>
<% if recipe.ingredients.any? %>
<p>Ingredients: <%= render recipe.ingredients %></p>
<% end %>

<p>Feeds: <%= recipe.servings_made %></p>
<p>Preptime: <%= recipe.prep_times %></p>
<p>
<span class='quiet'><small>Created: <%= time_ago_in_words(recipe.created_at)%> ago</small></span><br>
<span class='quiet'><small>Last edited: <%= time_ago_in_words(recipe.updated_at)%> ago</small></span>
</p>
<div class="pull-right">
<%= link_to like_recipe_path(recipe, like: true), method: :post do %>
<i class="glyphicon glyphicon-thumbs-up"></i> &nbsp <%= recipe.thumbs_up_total %>
<% end %> &nbsp&nbsp&nbsp&nbsp
<%= link_to like_recipe_path(recipe, like: false), method: :post do %>
<i class="glyphicon glyphicon-thumbs-down"></i> &nbsp <%= recipe.thumbs_down_total %>
<% end %>
</div>
</div>
<div class="col-md-4">
<%= image_tag(recipe.picture.url, size: "250x150", class: "gravatar") if recipe.picture?%>
Expand Down
35 changes: 15 additions & 20 deletions app/views/recipes/search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,27 @@

<%= will_paginate %>
<div class="row">

<div class="col-md-2">
<section class="chef_info_center">
<%= render partial: 'gravatar_section', locals: {user: @user} %>
</section>
<%= recipe_gravatar(recipe.user) %>
</div>

<div class="col-lg-6 well">
<h4><%= link_to recipe.name, recipe_path(recipe) %></h4>
<p><%= recipe.summary %></p>
<% if recipe.styles.any? %>
<p>Styles: <%= render recipe.styles %></p>
<% end %>

<h4><%= link_to recipe.name, recipe_path(recipe) %></h4>
<p><%= recipe.summary %></p>
<% if recipe.styles.any? %>
<p>Styles: <%= render recipe.styles %></p>
<% end %>
<p>
<span class='quiet'><small>Created: <%= time_ago_in_words(recipe.created_at)%> ago</small></span><br>
<span class='quiet'><small>Last edited: <%= time_ago_in_words(recipe.updated_at)%> ago</small></span>
<span class='quiet'><small>Created: <%= time_ago_in_words(recipe.created_at)%> ago</small></span><br>
<span class='quiet'><small>Last edited: <%= time_ago_in_words(recipe.updated_at)%> ago</small></span>
</p>
<div class="pull-right">
<%= link_to like_recipe_path(recipe, like: true), method: :post do %>
<i class="glyphicon glyphicon-thumbs-up"></i> &nbsp <%= recipe.thumbs_up_total %>
<% end %> &nbsp&nbsp&nbsp&nbsp
<%= link_to like_recipe_path(recipe, like: false), method: :post do %>
<i class="glyphicon glyphicon-thumbs-down"></i> &nbsp <%= recipe.thumbs_down_total %>
<% end %>
</div>
<%= link_to like_recipe_path(recipe, like: true), method: :post do %>
<i class="glyphicon glyphicon-thumbs-up"></i> &nbsp <%= recipe.thumbs_up_total %>
<% end %> &nbsp&nbsp&nbsp&nbsp
<%= link_to like_recipe_path(recipe, like: false), method: :post do %>
<i class="glyphicon glyphicon-thumbs-down"></i> &nbsp <%= recipe.thumbs_down_total %>
<% end %>
</div>
</div>
<div class="col-md-4">
<%= image_tag(recipe.picture.url, size: "250x150", class: "gravatar") if recipe.picture?%>
Expand Down
6 changes: 3 additions & 3 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
</div>

<div class="col-md-5 pull-right">
<% if user?(@user) %>
<%= render partial: 'gravatar_section', locals: {user: @user} %>
<% end %>
<% if user?(@user) %>
<%= render partial: 'shared/gravatar_section', locals: {user: @user} %>
<% end %>

<h3>Reviews</h3>
<% if @recipe.reviews.any? %>
Expand Down
6 changes: 6 additions & 0 deletions app/views/shared/_gravatar_section.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<section class="chef_info_center">
<p>
<%= link_to gravatar_for(user, size: 100), user_path(user) %>
<h5><%= link_to "#{user.first_name} (#{user.username})", user %></h5
</p>
</section>
5 changes: 1 addition & 4 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
<% @users.each do |user| %>
<div class ="row">
<div class="col-md-4 col-md-offset-4 center">
<%= link_to gravatar_for(user, size: 200), user_path(user) %>
<p>
<h5>User Name: <%= user.username %></h5>
</p>
<%= call_gravatar(user) %>
<hr>
<br>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<div class ="row">
<div class="col-md-4 col-md-offset-4 center">
<%= gravatar_for @user, size: 200 %>
<%= call_gravatar(@user) %>
<hr>
</div>
</div>
Expand Down
7 changes: 2 additions & 5 deletions app/views/workouts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@
<% @workouts.each do |workout| %>
<div class="row">
<div class="col-md-2">
<section class="chef_info_center">
<%= link_to gravatar_for(workout.user, size: 100), user_path(workout.user) %>
<h5> User name: <%= workout.user.username %></h5>
</section>
<%= call_gravatar(workout.user) %>
</div>
<div class="col-lg-6 well">
<h4><%= link_to workout.name, workout_path(workout) %></h4>

<p><%= truncate(workout.summary, length: 350) %><%= link_to 'click to see workout', workout_path(workout)%></p>
<% if workout.attachment? %>
<p><%= link_to "Download Workout", workout.attachment_url %></p>
Expand Down
8 changes: 4 additions & 4 deletions app/views/workouts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
</div>
</div>
<div class="col-md-5 pull-right">
<%= link_to gravatar_for(@workout.user, size: 200), user_path(@workout.user) %>
<p>
<h5>Brought to you by: <%= @workout.user.username %></h5>
</p>
<% if user?(@workout.user) %>
<%= render partial: 'shared/gravatar_section', locals: {user: @workout.user} %>
<% end %>

<hr>
</br>
</div>
Expand Down
Loading

1 comment on commit 66a540d

@Mirv
Copy link
Collaborator Author

@Mirv Mirv commented on 66a540d Jun 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one accidentally went straight to master branch - when I did my git pull it was from my test branch - but the git pull moved me to master branch before I noticed.

Please sign in to comment.