jatwell93 / rubyproject

1 stars 1 forks source link

recipe controller - current_user - wrapper/hook #39

Open Mirv opened 7 years ago

Mirv commented 7 years ago

Refactor - hooks - current_user is in 5 locations in recipe controller - should be in one & then the private method is called by all other spots to make changes easier.


Status: Thus far the recipe MVC is set in regards to isolating current_user (only located in controller x 1 time).


Ok ... so hooks for the current_user ...

All controllers ...

jatwell93 commented 7 years ago

good pick up!

Mirv commented 7 years ago

Yea - my issues sections tend to read like sticky notes on a desk :)

jatwell93 commented 7 years ago

hahahaa i like it :)

Mirv commented 7 years ago

Also pending 41 mentions of current_user in the views ... one day we want all the logic stored in helpers ... for now we settle for controller isolation, then models, then views.

Searching for current_user in/app/views (Found 41 matches in 13 files)

/app/views/conversations/show.html.erb:
    7:     <% if conversation.is_trashed?(current_user) %>

/app/views/dashboard/index.html.erb:
    17:             <%= link_to gravatar_for(current_user, size: 100), user_path(current_user) %>
    20:           <% if current_user %>
    22:                 class: "btn btn-primary pull-right" unless current_user.follows_or_same?(athlete) %>

/app/views/dashboard/search.html.erb:
    7:       <% if current_user %>
    9:             class: "btn btn-primary pull-right" unless current_user.follows_or_same?(athlete) %>

/app/views/exercises/_form.html.erb:
    4:       <%= form_for([current_user, @exercise], :html => {class: "form-horizontal", role: "form"}) do |f| %>

/app/views/exercises/edit.html.erb:
    1: <h1 class="text-center" >Edit Exercise for <%=current_user.username%></h1>
    8:             <%= link_to "Show", [current_user, @exercise], class: "btn btn-primary link", id: "new-exer-btn" %>
    11:             <%= link_to "Back", user_exercises_path(current_user), class: "btn btn-primary link", id: "new-exer-btn" %>

/app/views/exercises/index.html.erb:
    22:             <td><%= link_to "Show", [current_user, exercise] %></td>
    23:             <td><%= link_to "Edit", [:edit, current_user, exercise] %></td>
    24:             <td><%= link_to "Destroy", [current_user, exercise], method: :delete, data: {confirm: "Are you sure?"} %></td>
    36:     <%= link_to "New Workout", new_user_exercise_path(current_user), class: "btn btn-primary link" %>
    49:           <td> <%= link_to gravatar_for(current_user, size: 100), user_path(current_user) %></td>
    50:           <td class="pull-left"><%= link_to friend.full_name, friendship_path(current_user.current_friendship(friend)) %></td>
    54:             <td><%= link_to "Unfollow", friendship_path(current_user.current_friendship(friend)), method: :delete,

/app/views/exercises/new.html.erb:
    1: <h1 class="text-center" >New Exercise for <%=current_user.full_name%></h1>
    8:             <%= link_to "Back", user_exercises_path(current_user), class: "btn btn-primary link", id: "new-exer-btn" %>

/app/views/exercises/show.html.erb:
    74:     <%= link_to "Back", user_exercises_path(current_user), class:"btn btn-lg btn-primary" %>

/app/views/friendships/show.html.erb:
    30:         <%= link_to "Back", user_exercises_path(current_user) %>

/app/views/layouts/_navigation2.html.erb:
    107:             <li><%= link_to "Dashboard", dashboard_index_path(current_user) %></li>
    110:             <%="#{current_user.username}"%> <b class="caret"></b>
    122:             <li><%= link_to "Your Home Page", user_path(current_user) %></li>
    123:             <li><%= link_to "Log a Workout", new_user_exercise_path(current_user) %></li>
    124:             <li><%= link_to "Measurements", new_user_bodyweight_path(current_user) %></li>

/app/views/users/finish_signup.html.erb:
    3:   <%= form_for(current_user, :as => 'user', :url => finish_signup_path(current_user), :html => { role: 'form'}) do |f| %>
    4:     <% if @show_errors && current_user.errors.any? %>
    6:         <% current_user.errors.full_messages.each do |msg| %>

/app/views/users/show.html.erb:
    100:                 <td><%= link_to "Show", [current_user, exercise] %></td>
    101:                 <td><%= link_to "Edit", [:edit, current_user, exercise] %></td>
    102:                 <td><%= link_to "Destroy", [current_user, exercise], method: :delete, data: {confirm: "Are you sure?"} %></td>
    114:           <%= link_to "New Workout", new_user_exercise_path(current_user), class: "btn btn-primary link" %>
    129:               <td> <%= link_to gravatar_for(current_user, size: 100), user_path(current_user) %></td>
    130:               <td class="pull-left"><%= link_to friend.full_name, friendship_path(current_user.current_friendship(friend)) %></td>
    134:                 <td><%= link_to "Unfollow", friendship_path(current_user.current_friendship(friend)), method: :delete,

/app/views/workouts/show.html.erb:
    58:           <% if logged_in? and (workout_owner? || current_user.admin?) %>

Found 41 matches in 13 files 
Mirv commented 7 years ago

Controller List of current_user .... status 43 mentions in 8 files ... that should be 8 mentions.

earching for current_user in/app/controllers (Found 43 matches in 8 files)

/app/controllers/application_controller.rb:
    7:   # helper_method :current_user
    14:     if current_user && !current_user.email_verified?
    15:       redirect_to finish_signup_path(current_user)
    22:       @mailbox ||= current_user.mailbox
    31:       !!current_user

/app/controllers/conversations_controller.rb:
    9:     conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
    15:     @receipts = conversation.receipts_for(current_user).order("created_at ASC")
    17:     conversation.mark_as_read(current_user)
    22:     current_user.reply_to_conversation(conversation, message_params[:body])
    28:     conversation.move_to_trash(current_user)
    33:     conversation.untrash(current_user)

/app/controllers/exercises_controller.rb:
    6:       @exercises = current_user.exercises.all
    7:       @friends = current_user.friends
    11:        @exercise = current_user.exercises.new
    15:       @exercise = current_user.exercises.new(exercise_params)
    19:         redirect_to [current_user, @exercise]
    28:       @exercises = current_user.exercises.all
    39:         redirect_to [current_user, @exercise]
    50:       redirect_to user_exercises_path(current_user)
    60:         @exercise = current_user.exercises.find(params[:id])

/app/controllers/friendships_controller.rb:
    7:     user_id: current_user.id)) unless current_user.follows_or_same?(friend)
    23:     redirect_to user_exercises_path(current_user)

/app/controllers/omniauth_callbacks_controller.rb:
    5:         @user = User.find_for_oauth(env["omniauth.auth"], current_user)

/app/controllers/recipes_controller.rb:
    35:     # @recipe.user_id = current_user
    65:       flash[:danger] = "#{current_user.username.capitalize} you can only like/dislike once per item."
    94:       @user = User.find(current_user.id)
    112:       if current_user == @recipe.user_id then
    119:       redirect_to recipes_path unless current_user.admin?

/app/controllers/users_controller.rb:
    16:     # @exercise = current_user.exercises.find(params[:id])
    17:     @exercises = current_user.exercises.all
    18:     @friends = current_user.friends
    30:         sign_in(@user == current_user ? @user : current_user, :bypass => true)

/app/controllers/workouts_controller.rb:
    22:     @workout.user_id = current_user.id
    33:     review = Review.create(body: params[:body], user: current_user, workout: @workout)
    68:     review = Review.create(body: params[:body], user: current_user, workout: @workout)
    84:     like = Like.create(like: params[:like], user: current_user, workout: @workout)
    89:       flash[:danger] = "#{current_user.username} " + 'you can only like/dislike once per item.'
    105:       if current_user != @workout.user and !current_user.admin?
    112:       redirect_to workouts_path unless current_user.admin?

Found 43 matches in 8 files 
jatwell93 commented 7 years ago

This is probably a dumb question but to fix that would you have to create a private method then call a before action?

Mirv commented 7 years ago

There are no "right" ways to do it except ways that are easier for others to understand and modify thus saving time.

There are also no dumb questions.

So the short answer is yes, you'd setup a method(doesn't have to be private as private just tells programs to respect the flow of information in the code or before_action).

Exta: It doesn't have to use before_action, infact there are people who say while before_action DRY's the controller - there's times (like when testing) when before_actions obscure what the code is doing. I can't recall all things that should never be done with before_actions, but stuff like commiting a record to the database (unless it's logging, then it's ok) can be really confusing and bad for using the controller's before_action.

The long answer is that what you will be doing eventually is using this "modules" which hold "classes" that have "methods" in them, in order to "mixin" behavior to multiple controllers or models even. Sometimes I've seen people call from the view helper's to these modules.

jatwell93 commented 7 years ago

Oh ok so before_actions can skew the way the code is running. I think i'll spend some time reading up on that tonight. I've never really thought about it but it does make sense.

I've just started touching on modules in my current udemy Rails course. I'll have to brush up some more.

Mirv commented 7 years ago

It is a hook - but there's things you need to be careful with - because a year later you might not remember all the things it's doing or to check that header, but for people who don't know & are just reading your tests....they are lost in space.