Skip to content

Instantly share code, notes, and snippets.

= form_tag({ action: 'revise', controller: 'search' }, method: 'get') do
  = hidden_field_tag 'tag_list', @tag_ids
  = submit_tag 'Revise Search', class: 'button'

These do the same thing. Pretty interesting that I can just pass the search query in the link_to url (@tag_ids).

= link_to 'Revise Search', collection_revise_search_path(@collection, @note, tag_list: @tag_ids)

Pre-Refactor

~/Desktop/coding_resources/programs/md_notes
22:20:20 davemox@daves-mbp $ time for i in {1..5}; do bundle exec rspec spec/controllers/notes_controller_spec.rb; done
......................

Finished in 1.14 seconds
22 examples, 0 failures

Randomized with seed 55733
module NoteHandler
# params = { "new_tags"=>"People, Entrepreneurs ", "tag_list"=>"Ruby, Rails, ActiveRecord, " }
# @note.add_tags(params[:new_tags], params[:tag_list])
def add_tags(existing_tags, new_tags)
tag_names = tag_names(existing_tags, new_tags)
self.tags << tag_creator(tag_names)
end
def tag_names(existing_tags, new_tags)
(existing_tags + new_tags.strip).split(", ")
# SO MUCH SETUP!!!!!
require 'spec_helper'
describe NoteHandler do
describe "#add_tags" do
it "adds new and existing Tags to the Note", focus: true do
collection = Collection.create(name: "Programming")

Annoying Random Spec Failure

No rhyme or reason as to failure. Just depends on the order of the specs.

~/Desktop/coding_resources/programs/md_notes
21:38:26 davemox@Daves-MacBook-Pro $ bundle exec rspec
...........F.*................................................................*...............................................

Pending:
class Transaction
attr_reader :name, :description, :amount, :purchase_date
def initialize(name, description, amount, purchase_date)
@name = name
@description = description
@amount = amount
@purchase_date = purchase_date
end

Pretty much everything in a Ruby program is some type of 'Object'.

If you haven't already, you'll soon here the phrase 'Object Oriented Programming' thrown around. You'll also hear the abbreviations OO or OOP.

With OOP we create programs and try to model them after the real world. 'Objects' allow us to encapsulate real-world ideas and then manipulate them and provide the user of our program with some output. So the typical flow of any object-oriented program is as follows:

Input - some type of data or value

Program - takes the input and manipulates it

Solution??

Wow. So I instantiate @tag (a new tag object) for simple_form to use. The problem is that this new tag has no id yet. And this new tag gets added to @tag_list. So when I iterate over @tag_list to create the 'edit' links, eventually it hits a tag with no id and throws the error. Least I think this is what's happening.

pry output:

~/Desktop/coding_resources/programs/md_notes
14:31:50 davemox@Daves-MacBook-Pro $ bundle exec rspec --tag feature
Run options: include {:feature=>true}
resources :collections do
resources :notes, :tags
end
def new
@ticket = @project.tickets.build
end
# is basically the same as
def new
@ticket = Ticket.new(project_id: @project.id)

Testing controller 'helper' type methods

I have a couple methods defined in controllers that I call in one of the controller actions.

Example from SearchController:

I've been testing these methods by actually making a request. That can't be the most efficient way to test them. I can't exactly call ApplicationController.assigns_session_tags and then test the result...