Hank Holiday

Your Learning Story

How I learned Rails - Part 4 - Building Badge List

  • April 4, 2014 at 2:09 PM
  • Last updated over 10 years ago
  • Visible to public
At this point I was three to six months into my learning process, depending on how you define it.  And though I had done a lot of work up to this point, the real learning didn't actually start until I began building the app (this app, Badge List).

Below are some chaotic notes from the building of Badge List.  I hope to come back and clean these up later.

Bootstrap

https://github.com/twbs/bootstrap/blob/master/less/variables.less >> Bootstrap color variables http://getbootstrap.com/2.3.2/components.html  >> Bootstrap documentation bootstrap sass variables: https://github.com/thomas-mcdonald/bootstrap-sass/blob/2.1-stable/vendor/assets/stylesheets/bootstrap/_variables.scss

Rspec Syntax

Note: The standard documentation is super annoying… looking for good examples / references http://blog.teamtreehouse.com/an-introduction-to-rspec

Maybe this? >> https://www.relishapp.com/rspec/rspec-core/docs >> nope… this one! >> https://www.relishapp.com/rspec/rspec-expectations/v/2-14/docs/built-in-matchers

More tips… http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/

Open Badge Framework

Step 1 >> https://github.com/mozilla/openbadges/wiki/Assertions >> This is like the holy grail. It’s the metadata spec, super well written.
Step 2 >> https://github.com/mozilla/openbadges/wiki/Issuer-API >> This explains how to actually get the badge into your backpack

CarrierWave

This is the best file uploading thing to use… not sure if it supports non-image uploads. http://railscasts.com/episodes/253-carrierwave-file-uploads?view=similar >> yes it does support non-image files >> http://stackoverflow.com/questions/7344618/carrierwave-and-correct-file-extension-depending-on-its-contents

Devise

https://github.com/plataformatec/devise >> Better format here: http://devise.plataformatec.com.br/

Good news… devise now supports mongoid by default!! :D … here’s another guy’s 2011 post on the setup >> http://hafizbadrie.wordpress.com/2011/03/01/mongoid-and-devise-in-rails-3/

Adding a custom user view… http://stackoverflow.com/questions/7086583/creating-a-users-show-page-using-devise

Email

http://myrailslearnings.wordpress.com/2013/02/19/using-amazon-ses-to-send-emails-from-rails/ http://stackoverflow.com/questions/4798437/using-amazon-ses-with-rails-actionmailer

How to send and receive email in Rails >> http://guides.rubyonrails.org/action_mailer_basics.html
Picking a cloud email provider > Done!  I went with postmark primarily because they are 100% focused on transactional email..  I figure we can use another provider for marketing emails altogether.
http://developer.postmarkapp.com/

Tests

https://github.com/rspec/rspec-rails https://github.com/jnicklas/capybara https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

RSpec Generators

https://www.relishapp.com/rspec/rspec-rails/docs/generators
rails generate rspec:model widget >> spec/models/widget_spec.rb rails g rspec:controller width >> spec for the controller

What’s the difference between feature specs and other specs (request specs)? >> The railstutorial guy says he’s moving towards feature specs in future editions http://stackoverflow.com/questions/15173946/rspec-what-is-the-difference-between-a-feature-and-a-request-spec http://www.andylindeman.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html
  • Starting with capybara 2.0 there are two separate folders…
    • spec/requests >> Do not include capybara by default. Best practices: use methods like get, post, put and delete and assert against response. Use describe / it
    • spec/features >> Use capybara and the visit method, usually assert against page. Use feature / scenario
Now I’m getting confused about how models & controllers fit into this… http://pivotallabs.com/testing-strategies-with-rspec/ >> meh, that last one was only passingly helpful given my current understanding level, perhaps it will make more sense later.  from the rspec documentatin here are the different types…
  • Model Specs
  • Controller Specs
  • Request Specs
  • Features Specs
  • These don’t seem as big...
    • View Specs
    • Helper Specs
    • Mailer Specs
    • Routing Specs
Capybara cheat sheet: https://gist.github.com/zhengjia/428105 Capybara rspec matchers >> http://rubydoc.info/github/jnicklas/capybara/master/Capybara/RSpecMatchers Capybara finders >> http://www.ruby-doc.org/gems/docs/c/capybara-rails-2-2-0.4.1.1/Capybara/Node/Finders.html

Controller Tests >> http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html

Simple Form 

Add it to the gem file and update $ rails g simple_form:install --bootstrap then it says to be sure to add the simple form tag into the erb along with a bootstrap form class ex >> simple_form_for(@user, :html => {:class => ‘form-horizontal’}) do |form|

documentation: http://simple-form.plataformatec.com.br/

great example of using simple form with bootstrap >> http://simple-form-bootstrap.plataformatec.com.br/articles/new

Initial Mongoid Setup

  • create the app
  • rails new app_name --skip-active-record
  • add this to the gemfile gem ‘mongoid' gem 'bson_ext' then run bundle update
  • create a scaffold for the badges >> rails g scaffold badge name image_url summary description

Mongoid Notes

http://mongoid.org/en/mongoid/docs/querying.html http://mongoid.org/en/origin/docs/selection.html >> NOTE: The DSL for querying actually comes from Origin >> http://mongoid.org/en/origin/docs/selection.html

Querying into arrays >> http://stackoverflow.com/questions/9392438/how-to-match-mongoid-documents-using-array-fields-in-query

http://webcache.googleusercontent.com/search?q=cache:VBHQkWWu7JUJ:blog.wiemann.name/mongoid-cheat-sheet+&cd=4&hl=en&ct=clnk&gl=us

For future reference: Carrierwave image uploading to mongoid / gridfs https://coderwall.com/p/lqtsya >> this is a good walkthrough, but… this is the generic / best place to start… https://github.com/carrierwaveuploader/carrierwave-mongoid

How to serve images from a database field http://stackoverflow.com/questions/4294427/rails-displaying-an-image-from-a-blob-field-in-a-database

Many to many relationships in mongoid?? Looks like this is not best practice? >> http://mongoid.org/en/mongoid/docs/relations.html http://codecraft.io/2011/07/31/mongoid-multiple-many-to-many-relations/ >> my exact use case!

Ruby Notes

Ruby and rails variable naming conventions >> http://itsignals.cascadia.com.au/?p=7

Finding syntax errors in a rake task… http://spectator.in/2010/02/19/ruby-1-dot-9-can-help-quickly-find-syntax-errors/ basically just try to compile it with the “w” option >> ruby -wc lib/finder.rb

Rails Notes

Temporarily turning off automatic rails timestamps
http://stackoverflow.com/questions/861448/is-there-a-way-to-avoid-automatically-updating-rails-timestamp-fields

Research 1: http://guides.rubyonrails.org/action_controller_overview.html >> http://guides.rubyonrails.org/layouts_and_rendering.html Research 2: I need to truly understand how ajax is supposed to work in rails.  

How to create a new app and specify the version… $ rails _2.1.0_ new myapp

when first using bundler be sure to do this >> bundle install --without production >> the w/o production part will be remembered in future bundle calls

also remember to update the gitignore file! (at least i think this is important)

add rspec / capybara right away then do this >> $ rails generate rspec:install >> configures app to use rspec instead of test::unit

Best practices for bulk data updating in a controller (in a restful manner) >> http://stackoverflow.com/questions/7965949/best-practice-for-bulk-update-in-controller

Time Zones

http://spilth.org/notes/rails3-date-time/ >> how to get a list of time zones… $ rake -D time
$ rake time:zones:all
$ rake time:zones:us

Environment Variables

http://railsapps.github.io/rails-environment-variables.html

Relationships

http://guides.rubyonrails.org/association_basics.html relationships in mongoid >> http://mongoid.org/en/mongoid/docs/relations.html

Semantic URLs / Vanity URLs / Understanding custom routing

This is so hard to find info on.
But I think this is it: http://jasoncodes.com/posts/rails-3-nested-resource-slugs >> NOPE… too complex, depends on the friendly url gem which is (a) overkill and (b) i believe doesn’t support mongoid


This is the best one: https://gist.github.com/cdmwebs/1209732 >> This does do a great job of explaining all the pieces. ++ >. This is also useful for reference …. http://railscasts.com/episodes/63-model-name-in-url-revised
  • First you have to nest the routes and use the :path parameter to allow them to be matched without the model name in the url
  • Then you have to override the to_param instance method
  • Then you have to either override self.find in the model OR add a validation method that implements an alternate find strategy
This is an important piece as well: http://stackoverflow.com/questions/10682346/friendly-urls-in-github

Two other ref links: http://apidock.com/rails/ActiveRecord/Base/to_param, http://stackoverflow.com/questions/13850819/can-i-determine-if-a-string-is-a-mongodb-objectid

Validating Semantic Profile URLs

http://edgeguides.rubyonrails.org/active_record_validations.html http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html >> this is actually the one that mongoid uses, though i think it’s the same validating from a list and including nil >> http://stackoverflow.com/questions/4730058/rails-validation-limit-input-to-specific-values Validating “vanity profile urls” This is the easy way >> http://stackoverflow.com/questions/9913064/how-can-you-make-sure-a-username-wont-conflict-with-an-existing-route

This looks like the better way to do it but sounds harder >> http://scribdtech.wordpress.com/2010/09/01/vanity-user-profile-urls-in-rails/ there’s also a gem that does super fancy stuff >> https://github.com/norman/friendly_id

UTF-8 Encoding

I had some problems storing the badge images directly in the MongoDB documents.  It turns out they were just problems with the encoding getting garbled.

http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/ ;