Deploying NextJS to Heroku

This took me quite sometime to figure out, so I hope this helps:

  1. Follow Heroku’s instructions on Getting Started on Node.js up until “Deploy the app”
  2. At this point, visiting your site via heroku open will only show an error
  3. Modify your package.json to include the following scripts:
  4. "scripts": {
      // ...
      "heroku-postbuild": "next build",
      "start": "next start -p $PORT"
    }
  5. Push to Heroku again, and voila, it should now work.

Retention Hypothesis: Trust and Responsibility

For a company in the service business, especially one as small as us, as cliché as it sounds, our people are our greatest asset.

Retaining talent is very important for us to execute at the level that we do.

My hypothesis are the following:

  • Trust your employees to do what’s right for the company
  • Reduce the impact of mistakes

In a fundamental level, the basis of our retention hypothesis is trust. We provide the vision, the direction and the goals but we trust the employees on the implementation details. This is incredibly hard to do, especially if you’re as big as a control freak as I am.

But trust is a two-way street. Once the trust has been broken, it will be an incredibly long process to get it back.

More details in a future post.

Happy Labor Day!

 

Making ActiveRecord associations readonly based on state

My use-case:

* Trip has many destinations and travelers
* Trip states are: 
draft -> checked out -> paid
draft -> checked out -> failed (which should allow them to edit again)

* Trip workflow actually is: 
being edited -> for confirmation -> go to payment gateway (set to checked out) -> go back to success/fail
payment gateway sends payment details to system whether payment was success (paid) or failed (failed)

To disable editing on trip:

class Trip < ApplicationRecord
  def readonly?
    !draft?
  end

  def draft?
    !checked_out?
  end

  def checked_out?
    return false if checked_out_on_changed?
    # this is required, otherwise saving the relations will always fail
    # as draft will always return false even during the saving process
    
    !checked_out_on.blank?
  end
end

To disable changes on related records (destination/traveler):

class Destination < ApplicationRecord
  validate :trip_is_in_draft

protected
  def trip_is_in_draft
    errors.add(:trip, "should be in draft") if !trip.draft?
  end
end

Testing Email Deliveries using Cucumber/Capybara/Minitest


# features/support/env.rb
Around([email protected]') do |scenario, block|
  ActionMailer::Base.delivery_method = :test
  ActionMailer::Base.perform_deliveries = true
  ActionMailer::Base.deliveries.clear

  old_adapter = ActiveJob::Base.queue_adapter
  ActiveJob::Base.queue_adapter = :inline
  # ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
  block.call

  ActiveJob::Base.queue_adapter = old_adapter
end


# features/steps/*_step.rb

Then(/^I should receive an email$/) do
  # assuming mailcatcher
  assert_difference -> { ActionMailer::Base.deliveries.length } do
    # do stuff that is supposed to send an email
  end
end


# features/*.feature

@email
Scenario: Checkout page
  When I press the checkout button
  Then I should receive an email

Removing field name from validation error message

http://stackoverflow.com/a/40254247/365218

Related to the accepted answer and another answer down the list:

I’m confirming that nanamkim’s fork of custom-err-msg works with Rails 5, and with the locale setup.

You just need to start the locale message with a caret and it shouldn’t display the attribute name in the message.

A model defined as:

class Item < ApplicationRecord
  validates :name, presence: true
end

with the following en.yml:

en:
  activerecord:
    errors:
      models:
        item:
          attributes:
            name:
              blank: "^You can't create an item without a name."

item.errors.full_messages will display:

You can't create an item without a name

instead of the usual

Name You can't create an item without a name

How to create a PORO model and use it in Rails Form Helpers

First off, I’ll start with our use-case:

We have a fairly simple form for an item with some complexity. It would have default values derived from the current_user‘s attributes but would still have the field on its own. Overriding it in Item model feels like overkill (and starts to be a violation of SRP) so we set out to create a PORO model for it. Full code and explanation below the fold.

Continue reading “How to create a PORO model and use it in Rails Form Helpers”

Numbers

The same song has been playing for 2 hours now.
It’s sunny outside.
I’ve been working for 2 hours now.
The view of the pool from our window is calming.
We’ve been here for 32 days now.
I kind of like it here.

The plates have shifted.
It has been 10 months now.
But the boiling hot anger and tremors of disappointment are ever stronger.
It has been 5 years for me since I had an idea, though.
I don’t feel anything, because I knew this was coming.

I’ll stay here until I’m 30.
That’s when I plan to revisit my plans.
But now, I’m still 27.
So there’s nothing else to do but to execute.
I had an 8-year head-start.
Why do I feel like I’m so far behind?

Love Gaming. Hate Gaming.

I actually have a love-hate relationship with gaming.

  • I used to steal from my dad’s wallet just to play in the nearest playstation rental place (remember those?)
  • It started me poking around my computer and lead me to my understanding of computer hardware today.
  • Me and my brother have spent upwards of 10,000 pesos over the course of 4+ years on internet cards and ragnarok cards as well as countless hours playing various MMO games (Ragnarok then Khan Online then Flyff then Dekaron and a bunch of other MMOs that I’m forgetting.)
  • It started me into writing scripts for bots and exercised my nascent logic skills (which turned out useful for programming.)

I love playing games. But it just sucks so much of my time and attention, because it’s too interesting and I’m finding out that I’m a really competitive person (even when I claim that I’m not.) Especially if it’s a game that has infinite replayability and a grind component (e.g. most MMORPGs), I’ll be gone for a week.

I’ve been able to stave this off because I play it on a dedicated computer but mobile games are now the bane of my existence. The accessibility makes it hard to compartmentalize and it provides a “good enough” dose to tide me over.

I wish to find the middle ground. Something like, play only games on weekends. Or in the evening. Something like that. But I’m realizing that it _is_ _fucking_ _incredibly_ hard.

It’s either I don’t play at all, or I just play all the time.

And I’m slowly finding out that I might have to give up gaming altogether if I want to be at 100%.
I’m trying to come to terms with this realization.

Game over, huh?

Peer-to-Peer Psychological Easing

If you follow my sister’s blog, you’ll sort of glean that we have this family problem. (Which family doesn’t, right?)

Oh, didn’t catch that? Or don’t read my sister’s blog?
Oh well, cat’s out of the bag. *shrug*

Anyway, everything that’s going on with my family is actually the least of my concerns at the moment. I’m emotionally distancing myself from the issue, because the situation really needs someone level-headed and impartial.

This is not to say I don’t have emotions – I do. I’m not bottling it up. On the contrary, I just say it to everyone I know that cares, reputation be damned. The more I tell it to other people, the less intense it becomes.

I think “peer-to-peer psychological easing” should be a thing. You heard it first here!