WILT: aliasing a ‘splitting by character’ shortcut

Gist

Today I needed to inspect the $PATH variable. When I echoed it, of course it returned a colon-delimited string as always. But the OC in me wanted to view these paths per line. So what I did is I used sed for it:

~$ sed 's/:/\n/g' <<< $PATH

However, I wanted this to be reusable. I wanted to automate everything I use more than once. So what I did is, I created an alias in my ~/.bashrc :

alias splitbychar='function _split() { sed "s/$1/\n/g" <<< "$2"; }; _split'

Now an alias called splitbychar exists. I can easily call it like this:

~$ splitbychar : $PATH

As expected, my colon delimited $PATH string is split into a line per path.

Usage

  • Handy for splitting long strings separated by a certain character
  • Other delimiters can be used, like comma, etc. Haven’t tried space though.
  • Can be used in *nix environments that can run sed

Reference

  • I got the sed technique here
  • I got the aliasing technique here

Mavensmate: Temporary workaround for compile_active_file & compile_tabs both pointing to the same key-combination

NOTE: This is for WINDOWS USERS. For Mac/Linus, I think there aren’t any problems with your key bindings.

As of MavensMate-SublimeText v7.0.2, this commit, which is supposed to fix the key bindings that prioritizes CTRL over SUPER, inadvertently introduced an error wherein both compile_active_file and compile_tabs now point to the same key binding (ctrl+shift+s).

As a work around, since ctrl+shift+s already became part of my workflow in compiling the current file I’m working on, I explicitly declared the key combination in my user keymap. To do this, follow below instructions:

  1. Go to Preferences > Key Bindings > Default (Windows).sublime-keymap — User
  2. Inside the square brackets, add this:
    { "keys": ["ctrl+shift+s"], "command": "compile_active_file" }
  3. Make sure other key bindings are enclosed in curly braces, and that all curly braced bindings within the square bracket is comma separated.

This will override Mavensmate’s default, and will bind ctrl+shift+s to the compile_active_file command.

Do note that compile_tabs will lose its key binding. If you really need it, feel free to add another binding. But if it’s not that critical, then you can still access it by:

  1. Press ctrl+shift+p
  2. Type “Compile Tabs”
  3. Press Enter

 

Cheers!

Mavensmate: Running specific test methods for a test class

Recently, I created new test methods in an already existing test class. It contains a lot of other test methods and it takes a long time to run the class. Since I do not know how to run specific test methods, I run the whole test class EVERY TIME I needed to check if my test method is working. Thank God I found the solution after a couple of hours of research.

There are 2 ways to do this. (BTW, I’m using sublime text build 3126 and Mavensmate 7.0.2, just in case you encounter something odd)

  1. As I’ve mentioned here, you must open the test class containing the test method you wanted to run. While keeping that test class tab active, press ctrl+shift+p, then type “apex unit”. The options will pop up, then everything is self explanatory.
  2. If you’re really lazy (like me at times), then make a key binding.
    1. In the menu bar, click “Preferences”
    2. Click “Key Bindings”
    3.  In the user keymap (Default (Windows) .sublime-keymap — User), add this line inside the square brackets:
      { "keys": ["ctrl+r+m"], "command": "run_async_apex_test_method" }
    4. NOTE 1: The keys can be anything you like.
    5. NOTE 2: If you have an existing key binding, make sure that your old and new bindings are both enclosed in their own curly braces, and that they are comma delimited.
    6. Test it by going to the test class and pressing your keys combination
    7. A prompt will appear at the bottom part of sublimetext, enter the test method name(s) you want to run there.
    8. Press Enter.

Both approaches will show the result of the executed test method(s) in the MavensMate panel. If it’s not visible, then press Ctrl+Shift+M.

Hope this helps.

Collection of ActiveRecord Queries

  1. Parent with at least 1 child
    • Parent.joins(:children).uniq.all
  2. Parent with exactly 1 child
    • Parent.joins(:children).group("children.id").having("COUNT(*) = 1")
  3. Expiring in “N” days
    • Object.where(expiration_date: N.days.from_now)
  4. A transaction created on a certain month
    1. Postgres specific
      • Object.where('extract(month from transaction_date) = ?', month)
    2. Database agnostic
      • TODO

WILT: Fixing the “certificate verify failed” error in Windows

If ever I work on libraries connecting to the internet (Restforce, Faraday, etc) and encounter an error that sounds like “certificate verify failed”, just go hear:

http://stackoverflow.com/questions/5720484/how-to-solve-certificate-verify-failed-on-windows

or do this:

  1. Download http://curl.haxx.se/ca/cacert.pem into c:\cacert.pem
  2. Go to Environment Variables
  3. Create a new System Variable: SSL_CERT_FILE -> C:\cacert.pem
  4. Close all command prompts, including Rails server command prompt, etc.
  5. Start new prompt

WILT: Redirecting to record type selection page, save_new_url, and that nasty ampersand

I learned today that I can redirect a visual page to the standard recordtype selection page. I also learned that by using the save_new_url parameter, I can change the destination of where the user will get redirected to after the user chooses its desired record type.

Example:

https://instance.com/setup/ui/recordtypeselect.jsp?ent=OBJECT_ID_GOING_TO&retURL=%2FOBJECT_ID_GONE_FROM&save_new_url=/apex/CUSTOM_PAGE

Breaking it down by parameter:

  • ent: The object Id of the object I wanted to create a new record for. The recordtype selection page will then show a dropdown list filled with the record types of the object that are available for the profile of the currently logged in user.
  • retURL: id of any object I will get redirected to if:
    • I press the cancel button.
    • the creation of the new record succeeds.
  • save_new_url: After the record type selection, I can redirect to any page if I don’t want to go directly to the new record creation page specified in the ent parameter.

Regarding save_new_url:

Note that the path/url that will serve as the value of the save_new_url parameter should be URL-encoded. You can use any url encoder you prefer, but I used Salesforce’s EncodingUtil.urlEncode method to get the job done. (explained in depth here).

That nasty &!

If you find yourself passing data with ampersands to the save_new_url parameter, you have to url encode the ampersand first before url encoding the whole value passed to the save_new_url parameter. Failing to do that will open a myriad of headache inducing errors.

 

 

Postgre Cheatsheet

List all databases:

\list
\l

 

List all tables in the current db:

\dt

 

Switch databases:

\connect database_name

 

List all users:

SELECT u.usename AS "User name",
  u.usesysid AS "User ID",
  CASE WHEN u.usesuper AND u.usecreatedb THEN CAST('superuser, create database' AS pg_catalog.text)
       WHEN u.usesuper THEN CAST('superuser' AS pg_catalog.text)
       WHEN u.usecreatedb THEN CAST('create database' AS pg_catalog.text)
       ELSE CAST('' AS pg_catalog.text)
  END AS "Attributes"
FROM pg_catalog.pg_user u
ORDER BY 1;

 

Password less login:

sudo -u user_name psql db_name

 

Reset psql user’s password:

ALTER USER "user_name" WITH PASSWORD 'new_password';

 

Show table schema (via SQL):

SELECT column_name, data_type, character_maximum_length 
  FROM INFORMATION_SCHEMA.COLUMNS 
 WHERE table_name = '<name of table>';

 

Show table schema (via PostgreSql commands)

\d table_name
\d+ table_name
\ds sequence

 

Connecting to the database via command line without the need to use local user & password, or the hassle to go through changing /etc/postgresql/9.1/main/pg_hba.conf because you are encountering the dreaded “psql: FATAL:  Peer authentication failed for user “your_username””

psql -U your_username -W -h 127.0.0.1 database_name

 

Check the version of your database

1) CLIENT version: psql --version
2) SERVER version: pg_config --version
3) SERVER version (via sql query): psql -U your_username -W -h 127.0.0.1 database_name -c 'SELECT version();'