Skip to content

Commit

Permalink
setup helpers for 'owner?' logic to clean up view
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirv committed May 31, 2017
1 parent 9cdfe14 commit eed2a1a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def user?(user)
return true unless user.nil?
return false
end

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


def gravatar_for(user, options = { size: 80})
Expand Down
13 changes: 3 additions & 10 deletions app/helpers/recipes_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
module RecipesHelper
def is_owner?
if current_user.id == @recipe.user_id then
return true
else
return false
end
end

def user?(user)
return true unless user.nil?
def owner?
owner_check(@recipe.user_id)
end

end
8 changes: 8 additions & 0 deletions app/helpers/workouts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module RecipesHelper

def owner?
# owner_check(@recipe.user_id)
owner_check(@workout.user_id)
end

end
9 changes: 3 additions & 6 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,9 @@
<% end %>
</div>
</div>
<% if logged_in? and is_owner? %>
<% if logged_in? and owner? %>
<%= link_to "Edit this Recipe", edit_recipe_path(@recipe), class: "btn btn-success pull-right edit" %>
<% end %>

<% if logged_in? and (is_owner?) %>
<%= link_to "[Delete Recipe]", @recipe, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger pull-right delete"%>
<%= link_to "[Delete Recipe]", @recipe, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger pull-right delete"%>
<% end %>
<div class="social-share-button">
Share this! <%= social_share_button_tag(@recipe.name) %>
Expand All @@ -76,7 +73,7 @@
</div>
<p><%= c.body %></p>
<i><small><%= c.updated_at.strftime("%m/%d/%Y") %></small></i>
<% if is_owner? %>
<% if owner? %>
<%= link_to "Delete", review_recipe_path(:revid => c.id), class:"btn btn-danger btn-small pull-right", method: :delete, data: { confirm: "Are you sure you want to delete this review?" } %>
<% end %>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<div class="row">
<div class="container">
<% if @user == current_user %>
<% if owner_check(@user.id) %>
<h3><em><%= "Your Recipes" %></em></h3>
<% else %>
<h3><em><%= "Recipes from: " + @user.username %></em></h3>
Expand Down Expand Up @@ -48,7 +48,7 @@
<hr>
<div class="row">
<div class="container">
<% if @user == current_user %>
<% if owner_check(@user.id) %>
<h3><em><%= "Your Workout plans" %></em></h3>
<% else %>
<h3><em><%= "Workouts from: " + @user.username %></em></h3>
Expand Down
4 changes: 2 additions & 2 deletions app/views/workouts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<div class="social-share-button">
Share this workout!<%= social_share_button_tag( "#{@workout.user.username}'s Workout Plan") %>
</div>
<% if logged_in? and (@workout.user == current_user || current_user.admin?) %>
<% if logged_in? and (owner? || current_user.admin?) %>
<%= link_to "Edit this Workout", edit_workout_path(@workout), class: "btn btn-success pull-right edit" %>
&nbsp&nbsp
<%= link_to "[Delete Workout]", @workout, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger pull-right delete"%>
Expand All @@ -81,7 +81,7 @@
</div>
<p> <%= c.body %> </p>
<i><small><%= c.updated_at.strftime("%m/%d/%Y") %></small></i>
<% if c.user == current_user %>
<% if owner_check(c.user) %>
<%= link_to "Delete Review", review_workout_path(:revid => c.id), class:"btn btn-danger btn-small pull-right", method: :delete, data: { confirm: "Are you sure you want to delete this review?" } %>
<% end %>
</div>
Expand Down

1 comment on commit eed2a1a

@Mirv
Copy link
Collaborator Author

@Mirv Mirv commented on eed2a1a May 31, 2017

Choose a reason for hiding this comment

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

@jatwell93

  • created a helper in app/helpers/application.rb to check against current_user.id
  • created a helper for two view folders (workouts & 'recipes`) to test the owner

Going forward, when you make a new view & want to check if the current_user is the owner, you just check the app/helpers/[view_helper].rb ... if one exists you just copy the helper defined in app/helpers/recipe_helpers.rb called "owner?" and then change the @recipe.user_id to @[controllerVar].user_id. This of course assumes whatever new model/schema/controller you will have a has_many relationship pointing or if not you can do the @[model].user.id type thing.

Please sign in to comment.