Skip to content

Instantly share code, notes, and snippets.

Steps

Add to Gemfile

rails g devise:install

Manual Setup

  • action_mailer default url options in config/development.rb and config/production.rb
  • Add and use figaro gem to handle ENV variable for config/production.rb
  • ensure root is defined in routes.rb
  • ensure flash messages are available in app/views/layouts/application.html.haml.

Copy Devise views to app for editing

rails g devise:views

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...

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)

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}

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

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

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:
# 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")
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(", ")

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