Friday, January 20, 2017

Fun with Ruby and CSV

https://ruby-doc.org/stdlib-2.4.0/libdoc/csv/rdoc/CSV.html

https://www.sitepoint.com/guide-ruby-csv-library-part/

http://technicalpickles.com/posts/parsing-csv-with-ruby/

In Ruby, you can import your CSV file either at once (storing all of the file content in memory) or read from it row-by-row

Either way you do it, Ruby will store each table row as an array, with each cell being a string element of the array.

Tuesday, November 29, 2016

Thursday, September 1, 2016

View your Gems and its methods locally with no internet

In this example I am viewing my Devise Gem.
(Your paths will be different than mine.)

$ gem which devise
==> /home/vagrant/.rvm/gems/ruby-2.3.0/gems/devise-4.2.0/lib/devise.rb

$ gem open devise

$ cd /home/vagrant/.rvm/gems/ruby-2.3.0/gems/devise-4.2.0/lib

$  ls
==> devise  devise.rb  generators

$ cd devise/

$ ls
==> controllers     hooks       models.rb    orm                     rails.rb         time_inflector.rb
delegator.rb    mailers     modules.rb   parameter_filter.rb     strategies       token_generator.rb
encryptor.rb    mapping.rb  omniauth     parameter_sanitizer.rb  test             version.rb
failure_app.rb  models      omniauth.rb  rails                   test_helpers.rb

$ cd controllers/

$ ls
==> helpers.rb  rememberable.rb  scoped_views.rb  sign_in_out.rb  store_location.rb  url_helpers.rb

$ cd ..

Here I am looking for the RegistrationsController in all the files with Grep. This is like when you are in Sublime and search all the files.

$ grep -r RegistrationsC .
==> ./rails/routes.rb:    #    class RegistrationsController < Devise::RegistrationsController
./parameter_sanitizer.rb:  # +password_confirmation+ for the `RegistrationsController`), and you can
./parameter_sanitizer.rb:    #    # Inside the `RegistrationsController#create` action.

$ cd rails/

$ ls
==> routes.rb  warden_compat.rb

This is where it is. I open it in vim and look around.
$ vim routes.rb 

Thursday, August 25, 2016

git add with sophictication

I found this git command that I have been looking for forever.
I used to use it then I forgot it and have been googling for it ever since/.

The perfect pairing to git add -p, drum roll please...

git add . -N && git add -p
  • The -N flag means is short for --intent-to-add 
  • git add . -N will stage an empty file representing your newly added file.
    When git add --patch is called 
  • Git will do the normal patch procedure over your newly created file's changes.
  • If no changes are patched in,
    the empty file will not be included in your commit.

Always be sure to check your status!

Sunday, August 14, 2016

Rails Cheat Sheet: Create Models, Tables and Migrations

Create a new table in Rails

rails g model Supplier name:string
rails g model Product name:string:index sku:string{10}:uniq count:integer description:text supplier:references popularity:float 'price:decimal{10,2}' available:boolean availableSince:datetime image:binary
Resulting migrations:
class CreateSuppliers < ActiveRecord::Migration
  def change
    create_table :suppliers do |t|
      t.string :name

      t.timestamps null: false
    end
  end
end

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :name
      t.string :sku, limit: 10
      t.integer :count
      t.text :description
      t.references :supplier, index: true, foreign_key: true
      t.float :popularity
      t.decimal :price, precision: 10, scale: 2
      t.boolean :available
      t.datetime :availableSince
      t.binary :image

      t.timestamps null: false
    end
    add_index :products, :name
    add_index :products, :sku, unique: true
  end
end

Rails migration to add a column

rails g migration AddKeywordsSizeToProduct keywords:string size:string
Resulting migration:
class AddKeywordsSizeToProduct < ActiveRecord::Migration
  def change
    add_column :products, :keywords, :string
    add_column :products, :size, :string
  end
end

Rails migration to remove a column

rails g migration RemoveKeywordsFromProduct keywords
Resulting migration:
class RemoveKeywordsFromProduct < ActiveRecord::Migration
  def change
    remove_column :products, :keywords, :string
  end
end

Rails migration to rename a column

rails g migration RenameProductPopularityToRanking
You need to add the rename_column command manually to the resulting migration:
class RenameProductPopularityToRanking < ActiveRecord::Migration
  def change
    rename_column :products, :popularity, :ranking
  end
end

Rails migration to change a column type

rails g migration ChangeProductPopularity
You need to add the change_column command manually to the resulting migration:
class ChangeProductPopularity < ActiveRecord::Migration
  def change
      change_column :products, :ranking, :decimal, precision: 10, scale: 2
  end
end

Running migrations

rake db:migrate
In production:
rake db:migrate RAILS_ENV="production" 

Wednesday, April 13, 2016

Notes on Redis

https://github.com/LARailsLearners/sarcastic_messages_tutorial/blob/master/redis_notes.md#cache-vs-data

Thursday, April 7, 2016

Squashing a commit


  • You just sent a pull request to GitHub.
  • Someone commented on it.
  • You fixed the errors. You added them, commited them and pushed them again. 

This actually adds another commit to that pull request and makes it really hard to read for the person who is trying to accept that commit.

What you can do to combine these commits into one, easy to read commit is to squash them together.

This is officially called squashing a commit.

https://github.com/ginatrapani/todo.txt-android/wiki/Squash-All-Commits-Related-to-a-Single-Issue-into-a-Single-Commit

Squash a commit:
$ git rebase -i HEAD~3

You'll see your commits all mashed together

pick Add to README
pick Add .gitignore
pick Edit README

and a list of options

what you want to do is Put the commit you want at the top and squash the other ones into it by removing the work pick in front of it and replacing that with the letter 's' for squash.

So it looks like this

pick Add to README
s Add .gitignore
s Edit README

save it and force push it to GitHub like this:

$ git push origin branch-name --force


Then instead of doing the whole thing again if you make another change just
git add  
git commit --amend
git push origin branch-name --force

and you can add to you former commit