Rob Lacey

Brighton, UK - contact@robl.me

Software Engineer working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working at Juniper Education making code for UK schools.


HAML whitespace

I came across a weird HAML syntax in a client codebase that I was just confused about.

Haml::Engine.new(%q{
- title = '1'
.title== #{title}<br/>
}).to_html
=> <div class='title'>1<br/></div>

I found an alternative syntax, I am not sure I like it but it’s certainly better than the above.

Haml::Engine.new(%q{
- title = '1'
.title<
 #{title}
 %br>
}).to_html
=> <div class='title'>1<br></div>

Basically you can kill whitespace with < >

Bundle deprecations

If you want to search your bundle for anything in particular, in this case the deprecated Digest::Digest syntax

New-MacBook-Pro:that cex$ bundle show --paths | xargs grep -r Digest::Digest
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/aws-sdk-1.6.4/lib/aws/core/signer.rb:        OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new(digest), key, value)
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/fog-1.4.0/lib/fog/core/hmac.rb:      @digest = OpenSSL::Digest::Digest.new('sha1')
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/fog-1.4.0/lib/fog/core/hmac.rb:        @digest = OpenSSL::Digest::Digest.new('sha256')
/Users/cex/.rvm/gems/ruby-2.2.10@that/gems/fog-1.4.0/lib/fog/cloudstack.rb:    @@digest  = OpenSSL::Digest::Digest.new('sha1')
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_20/objects/root.dat matches
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_20/object_types matches
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_19/objects/root.dat matches
Binary file /Users/cex/.rvm/gems/ruby-2.2.10@that/gems/pry-doc-0.4.6/lib/pry-doc/core_docs_19/object_types matches

Everything you need in life

I’ve thought about this plenty and I’m more convinced than ever that the list of things you need in your life are

If there any suggestions on things to add to this list I will consider them but probably not too thoroughly.

When Postgresql doesnt start on OSX

New-MacBook-Pro:mini-epic cex$ ./bin/server 
=> Booting Puma
=> Rails 4.2.1 application starting in development on http://0.0.0.0:8001
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/cex/.rvm/gems/ruby-2.2.2@epic-invite/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize': could not connect to server: No such file or directory (PG::ConnectionBad)
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

I tried restarting with brew

New-MacBook-Pro:mini-epic cex$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Same deal. Thanks to https://stackoverflow.com/questions/13410686/postgres-could-not-connect-to-server Remove the PID and you’re sorted.

-rw-------  1 cex  admin  88  2 Mar 15:02 /usr/local/var/postgres/postmaster.pid
New-MacBook-Pro:mini-epic cex$ rm /usr/local/var/postgres/postmaster.pid
New-MacBook-Pro:mini-epic cex$ brew services start postgresql
Service `postgresql` already started, use `brew services restart postgresql` to restart.
New-MacBook-Pro:mini-epic cex$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Sometimes....

Sometimes it gets bloody…

AASM kick in the balls bug / typo / misunderstanding

Pretty average AASM block right? Wrong….

I was just dealing with a bug where object.mark_sent! wasn’t saving the record.

included do
  include AASM
  aasm(:state) do
    state :in_checkout, initial: true
    state :processing
    state :available
    state :completed
    state :inactive
    state :sent

    event :start_processing! do
      transitions from: [:in_checkout, :processing, :available], to: :processing
    end

    event :processed! do
      transitions from: [:in_checkout, :processing], to: :available
    end

    event :mark_sent! do
      transitions from: [:available, :completed], to: :sent
    end

    after_all_transitions :set_aasm_timestamp
  end
end

There’s a mark_sent! event, so surely mark_sent! should change the state and save the record right? No.

event :mark_sent! do
  transitions from: [:available, :completed], to: :sent
end

This actually creates two method mark_sent! which makes the state change and it’s counterpart that also saves the record mark_sent!!. You can’t call a method with a double exclamation directly.

[26] pry(main)> e.mark_sent!!
[26] pry(main)* 
[26] pry(main)* e.send(:'mark_sent!!')

When in Vegas, don’t define mark your events with an exclamation. DOH.

How to vendor a gem

I’ve got a really annoying DEPRECATION WARNING. It appears the wck gem is spitting out errors and it’s no longer being updated. All I want to do is calm down the errors. The styles are all still helpful.

DEPRECATION WARNING: Extra .css in SCSS file is unnecessary. Rename /Users/cex/.rvm/gems/ruby-2.2.2@epic-invite/gems/wck-0.0.5/app/assets/stylesheets/wck.css.scss to /Users/cex/.rvm/gems/ruby-2.2.2@epic-invite/gems/wck-0.0.5/app/assets/stylesheets/wck.scss. (called from _app_views_home_index_html_haml___3330030305515264653_70130897119780 at /Users/cex/repos/epic-invite/app/views/home/index.html.haml:9)

Best course of action is to vendor the gem. So All I need to do is unpack it into my application and fix the issue.

New-MacBook-Pro:epic-invite cex$ gem unpack wck --target=vendor/gems/
Unpacked gem: '/Users/cex/repos/epic-invite/vendor/gems/wck-0.0.5'

Gemfile

...
gem 'high_voltage', '~> 3.0.0'
gem 'wck', path: 'vendor/gems/wck-0.0.5'
gem 'cancancan'
...

And bundle install to install from that path.

New-MacBook-Pro:epic-invite cex$ bundle install
...
Using valid_email 0.0.11
Using wck 0.0.5 from source at `vendor/gems/wck-0.0.5`
Using whenever 0.9.4
....

The solution is to just rename the file, and satisfying to avoid seeing so many errors.

mv wck.css.scss wck.scss

Death's Head Vs.... Han Solo

I genuinely expected Solo to be a load of old bollocks, it wasn’t as good as Rogue One but it was still better than the prequel trilogy that we don’t talk about.

Sorry that Death’s Head has a miserable head on that day.

Create MySQL users has changed

After all this time, the way of creating users and granting privileges on user in one statement just doesn’t work anymore. I guess I just missed the memo

New-MacBook-Pro:robl cex$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.15 Homebrew

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> GRANT ALL ON robl.* to robl@localhost IDENTIFIED BY 'robl';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL 

Ok, so we need to create the user and then do the grant

mysql> CREATE USER robl IDENTIFIED BY 'robl';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL ON robl.* TO robl@localhost;
ERROR 1410 (42000): You are not allowed to create a user with GRANT
mysql> CREATE USER robl@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER robl@localhost iDENTIFIED BY 'robl';
ERROR 1396 (HY000): Operation CREATE USER failed for 'robl'@'localhost'
mysql> DROP USER robl;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER robl@localhost IDENTIFIED BY 'robl';
ERROR 1396 (HY000): Operation CREATE USER failed for 'robl'@'localhost'
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER robl@localhost IDENTIFIED BY 'robl';
ERROR 1396 (HY000): Operation CREATE USER failed for 'robl'@'localhost'
mysql> DROP USER robl;
ERROR 1396 (HY000): Operation DROP USER failed for 'robl'@'%'

Ok, let’s just start again.

mysql> DROP USER robl@localhost;
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER robl@localhost IDENTIFIED BY 'robl';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL ON robl.* TO robl@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> 

Ok, now I can get this started.

Ruby fuzzy matching

I am running through a data set which is lots of answers and trying to make the data consistent as human input is always going to be wonky from typos, spaces instead of hiphens to complete misspelling. Found this…

https://github.com/seamusabshere/fuzzy_match

2.3.3 :002 > require 'fuzzy_match'
=> true
2.3.3 :003 > fm = FuzzyMatch.new(['Uwe Rosenberg', 'X-Com'])
 =>#<FuzzyMatch:0x007ff02b05f370 @read=nil, @groupings=[], @identities=[], @stop_words=[], @default_options={:must_match_grouping=>false, :must_match_at_least_one_word=>false, :gather_last_result=>false, :find_all=>false, :find_all_with_score=>false, :threshold=>nil, :find_best=>false, :find_with_score=>false}, @haystack=[w("Uwe Rosenberg"), w("X-Com")]>
2.3.3 :004 > fm.find('Use Roenberb')
=> "Uwe Rosenberg" 

Perfect. Time to de-dupe some dodgy data.