Seth

3 minute read

lion

I’m on a killer roll right now blowing through this NYC lab. Built my 3 tables and 1 JOIN table migrations. Built out the associated models. Built out all the views and one controller. Have one controller and it’s views left now. If I didn’t have kids that were going to wake me up between 6:30am and 8:00am I’d finish it up. I think it’ll take me maybe 30 more mins (mostly because A LOT of it is going to be similar to the controller & views I just built) but sleep is needed. I also need to finish doing my part of the household chores (dishes tonight) before bed so in the interest of rest I’ve stopped.

However, I’m pretty hype and think I’ll still be on schedule to finish the review videos tomorrow night. I also feel more confident about attacking the portfolio project. The only part I haven’t really dug into is the UI. I figure a basic bootstrap will do. Utilize the layout.erb to have a consistent header and footer and I’ll be happy at this point in time (when that time comes).

I’m contemplating creating helper methods for my controllers. Currently the one controller I finished tonight (I’ll do the other and all it’s views tomorrow) is:

class FiguresController < ApplicationController

  get '/figures' do
    @figures = Figure.all
    erb :'figures/index'
  end

  get '/figures/new' do
    @figures = Figure.all
    @landmarks = Landmark.all
    @titles = Title.all
    erb :'figures/new'
  end

  get '/figures/:id' do
    @figure = Figure.find(params[:id])
    erb :'figures/show'
  end

  post '/figures/:id' do
    @figure = Figure.find(params[:id])
    @figure.update(params[:figure])

    if !params[:title][:name].empty?
      if !!params[:figure][:title_ids]
        params[:figure][:title_ids] << Title.create(params[:title]).id
      else
        params[:figure][:title_ids] = []
        params[:figure][:title_ids] << Title.create(params[:title]).id
      end
    end
    @figure.title_ids = params[:figure][:title_ids]

    if !params[:landmark][:name].empty?
      if !!params[:figure][:landmark_ids]
        params[:figure][:landmark_ids] << Landmark.create(params[:landmark]).id
      else
        params[:figure][:landmark_ids] = []
        params[:figure][:landmark_ids] << Landmark.create(params[:landmark]).id
      end
    end
    @figure.landmark_ids = params[:figure][:landmark_ids]

    @figure.save

    redirect to "/figures/#{@figure.id}"
  end

  get '/figures/:id/edit' do
    @figure = Figure.find(params[:id])
    @landmarks = Landmark.all
    @titles = Title.all
    erb :'figures/edit'
  end

  post '/figures' do
    @figure = Figure.create(params[:figure])

    if !params[:title][:name].empty?
      if !!params[:figure][:title_ids]
        params[:figure][:title_ids] << Title.create(params[:title]).id
      else
        params[:figure][:title_ids] = []
        params[:figure][:title_ids] << Title.create(params[:title]).id
      end
    end
    @figure.title_ids = params[:figure][:title_ids]

    if !params[:landmark][:name].empty?
      if !!params[:figure][:landmark_ids]
        params[:figure][:landmark_ids] << Landmark.create(params[:landmark]).id
      else
        params[:figure][:landmark_ids] = []
        params[:figure][:landmark_ids] << Landmark.create(params[:landmark]).id
      end
    end
    @figure.landmark_ids = params[:figure][:landmark_ids]

    @figure.save

    redirect to "/figures/#{@figure.id}"
  end
end

The main parts thar violate DRY (don’t repeat yourself) should be readily evident in:

if !params[:title][:name].empty?
  if !!params[:figure][:title_ids]
    params[:figure][:title_ids] << Title.create(params[:title]).id
  else
    params[:figure][:title_ids] = []
    params[:figure][:title_ids] << Title.create(params[:title]).id
  end
end
@figure.title_ids = params[:figure][:title_ids]

if !params[:landmark][:name].empty?
  if !!params[:figure][:landmark_ids]
    params[:figure][:landmark_ids] << Landmark.create(params[:landmark]).id
  else
    params[:figure][:landmark_ids] = []
    params[:figure][:landmark_ids] << Landmark.create(params[:landmark]).id
  end
end
@figure.landmark_ids = params[:figure][:landmark_ids]

Not only are these lines with a nested if statement not elegant but they are carbon copies of each other. In addition, I forsee using almost the same exact methods in my other controller. I’ll probably have one of these if blocks utilizing figure_ids but other than that it’ll be pretty close. We’ll see and if that’s true I’ll definitely create 3 helper methods and just use those if I can. It’ll be good practice in building helper methods for the controller because I haven’t done that before. I’ve built a module for the models in my last lab.

A note I took that didn’t make sense at the time was:

Define helper methods in the Application Controller

After tonight I see exactly why. This is because the individual controllers (at least in my case) are inherting from the app controller. I was thinking initially I would have to create a Helper class of some sort and then utilize them that way but I have this basically empty file that the controllers are already using so it makes sense to put helper methods right in there.

Time spent today: 2:09
Time spent total: 177:17
Lessons completed today: 0
Lessons completed total: 401

comments powered by Disqus