Git: Apply a diff
git apply allows you to apply a diff onto your HEAD. Most often you can achieve the same result with a rebase & merge.
Example:
master                commit1 - commit3
feature-branch                \ commit2 - commit4
git checkout feature-branch
git reset --hard commit3
git diff ..commit4 | git apply
master                commit1 - commit3
feature-branch                          \ Unstaged commit 2 & 4
You can also [create a patch and apply it afterwards](https://makandracards.com/makandra/2521-git-how-to...
Adding Jasmine JavaScript specs to a Webpack(er) project
The goal is to get Jasmine specs running in a Rails project using Webpacker, with the browser based test runner. Should be easily adaptable to a pure Webpack setup.
Step 1: Install Jasmine
yarn add jasmine-core
Step 2: Add two separate packs
Since we do not want to mix Jasmine into our regular Javascript, we will create two additional packs. The first only contains Jasmine and the test runner. The second will contain our normal application code and the specs themselves.
We cannot...
How to get a backtrace if rspec (or any other ruby process) hangs with no output
If rspec hangs with no output and you dont get a backtrace neither with --backtrace nor by just killing it with crtl-c,
you can put the following in your spec/spec_helper.rb:
puts "rspec pid: #{Process.pid}"
trap 'USR1' do
  threads = Thread.list
  puts
  puts "=" * 80
  puts "Received USR1 signal; printing all #{threads.count} thread backtraces."
  threads.each do |thr|
    description = thr == Thread.main ? "Main thread" : thr.inspect
    puts
    puts "#{description} backtrace: "
    puts thr.backtrace.join("\n")
  end
...
Cucumber: Clear localStorage after each scenario
Capybara clears cookies before each scenario, but not other client-side data stores. If your app is using localStorage or sessionStorage, contents will bleed into the next scenario.
Use this hook to remove all site data after each scenario:
After do
  if Capybara.current_driver == :selenium && !Capybara.current_url.starts_with?('data:')
    page.execute_script <<-JAVASCRIPT
      localStorage.clear();
      sessionStorage.clear();
    JAVASCRIPT
  end
end
Unpoly: Testing values for presence or blankness
In Ruby on Rails, all objects have a useful blank? method. It returns true for nil but also for empty strings or empty arrays. There is also a universal method present? which returns true for all values that are not blank?.
In JavaScript you need to roll your own implementation of blank? and present?.
If your application uses [Unpoly](...
How to: Fix json 1.8.3 with Ruby 2.5
The gem json fails to install for Ruby 2.5 if you use a version equal or below 1.8.3.
Run bundle update json --conservative to solve this issue.
The backtrace you will encounter looks like this:
Building native extensions. This could take a while...
ERROR:  Error installing json:
	ERROR: Failed to build gem native extension.
    current directory: /home/user/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/json-1.8.3/ext/json/ext/generator
/home/user/.rbenv/versions/2.5.3/bin/ruby -I /home/user/.rbenv/versions/2.5.3/lib/ruby/si...
Katapult EOL
Katapult was an endeavor to dramatically speed up starting a new Rails application. However, it turned out to save less time than expected, while requiring more time for maintenance than anticipated. Since its benefits fell too low, we have decided to not continue developing Katapult.
You can still use Katapult for generating ready-to-run applications with model CRUD, authentication and all of Katapult's features, but the rapid development of the web will quickly render the generated code antiquated. Nevertheless, its architecture may remai...
faviconit.com: Super-simple favicon generator
Eduardo Russo was tired of complex favicon creation and created his own favicon generator. It's really easy and allows a lot of image editing before rendering the favicons, in all needed sizes, formats and with the HTML needed to include them!
In Rails applications with Haml:
- put all the favicon files into /public
- store the HTML to app/views/layouts/_favicon.html
- add = render 'layouts/favicon'to<head>in your application layout(s)
... and you're all...
LoDash: isBlank and isPresent mixins
When you need to check a value for presence, don't rely on JavaScript since it considers 0 or "0" false. Also don't rely on LoDash's _.isEmpty:
if ('0') { ... } // false
if (0) { ... } // false
^
if (!.isEmpty('0')) { ... } // true (= good)
if (!.isEmpty(0)) { ... } // false (= not good)
This is because isEmpty it is only meant for objects with a length.
While the name implies that it's meant only for collections, you probably still want something like isBlank or `is...
Active Record and PostgreSQL — Ruby on Rails Guides
Rails guide that covers PostgreSQL-specific column types and usages for Active Record.
You should especially keep in mind the special datatypes that PostgreSQL offers. \
Types like json and array take away a lot of the pain that you had on MySQL projects.
Example use cases for array are tags or storing foreign keys (instead of a join model). You can even index them.
Deal with certain travis CI failures
Travis changed their default distribution from Ubuntu 14.04 (trusty) to 16.04 (precise). This might break your test setup for new builds.
You can solve this issue by freezing your test distribution in the .travis.yml to Ubuntu 14.04 until you have the time to solve all the issues you will have in 16.04:
dist: trusty
Error details
Here are few indicators that you ran into this issue:
Connection to the PostgreSQL database does not work anymore
Your travis-ci builds might have started failing on the usual
psql -c...
Yarn: How to recognize that you are using a different node version than your colleagues
The issue in this card can occur if the node_modules directory is checked into your Git repository. We usually recommend to exclude node_modules from version control.
In any case you should document which version of node to use in your project in a .nvmrc file.
I saw a strange behaviour after we introduced webpack in one of our projects and finally found out the reason: The person who committed the files used a node version that is older than mine.
Every time I wanted to run my rai...
cucumber_factory: How to keep using Cucumber 2 Transforms in Cucumber 3
Cucumber up to version 2 had a neat feature called Step Argument Transforms which was dropped in favor of Cucumber 3 ParameterTypes. While I strongly encourage you to drop your legacy Transforms when upgrading to Cucumber 3, it might not always be possible due to their different design.
This is a guide on how to keep the exact same functionality of your old Transforms while writing them in the style of new `Paramet...
Bundler: How to install version 1 instead of 2 (latest version)
When installing a gem you can use version comparators like >= or ~>. That way it is possible to fetch the latest version of Bundler 1 with this command:
gem install bundler -v '~>1'
How to install bundler for Ruby < 2.3 is a common usecase where you might need Bundler 1.
Selenium may break with ChromeDriver 75+
When you update your ChromeDriver to version 75 or beyond, you might get w3c errors in your tests.
For example, reading the browser console for errors no longer works, and page.driver.browser.manage.logs.get(:browser) will raise an error like "undefined method `log' for #<Selenium::WebDriver::Remote::W3C::Bridge:0x000055903f307aa8>".
Add options.add_option('w3c', false) to your Selenium configuration (e.g. features/support/selenium.rb) and you should be back to normal:
Capybara.register_driver :selenium do |app|
  options ...
How to recognize CVE-2019-5418
If you get requests with values for formats like this:
{:locale=>[:de], :formats=>["../../../../../../../../../../etc/services{{"], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml]}
or fails like this:
Invalid query parameters: invalid %-encoding (../../../../../../../../../etc/passwd%%0000.html)
Someone tries to exploit CVE-2019-5418.
If you use the latest Rails (or latest Rails LTS) you're...
Execution of shell code in Ruby scripts
Deprecated ways to execute shell code in Ruby
This is just a reference for legacy code. For new code, always use capture3.
%x{ } or backticks – quick and easy
Returns the standard output of running the given command in a subshell. This is an alias for `...`, and you can use string interpolation.
Example:
name = 'ls'
result = `which #{name}`
It does not escape anything you inject in the string, so be aware of possible security vulnerabilities...
How to install npm packages globally without sudo on Linux
All quoted.
- 
Set up a package root in your homedir to hold the Node "global" packages: $ NPM_PACKAGES="$HOME/.npm-packages" $ mkdir -p "$NPM_PACKAGES"
- 
Set NPM to use this directory for its global package installs: $ echo "prefix = $NPM_PACKAGES" >> ~/.npmrc
- 
Configure your PATH and MANPATH to see commands in your $NPM_PACKAGES prefix by adding the following to your .bashrc: # NPM packages in homedir NPM_PACKAGES="$HOME/.npm-packages" # Tell our environment about user-ins...
Rails Asset Pipeline: Building an Icon Font from SVG Files
Webpacker can automatically create an icon font from SVG files, which is really handy. When you're using the asset pipeline, you can still have an icon font from SVG files, but it requires some manual work.
Creating the icon font
- Install the NPM package icon-font-generator. If you're not usingnvm, runsudo npm install -g icon-font-generator
- Put all SVG icons for the font into their own directory.
- The icon name will be taken from the SVG file name
 
- Download the attached script and update the Configure...
Migrating from CoffeeScript to ES6
It is quite easy to migrate from CoffeeScript to ES6. You can use decaffeinate to convert your CoffeeScript source to modern JavaScript.
Install decaffeinate globally:
npm install -g decaffeinate
Call decaffeinate on each .coffee file, relaxing some options to get the most modern (and concise) JS:
decaffeinate file.coffee --use-cs2 --loose --optional-chaining --logical-assignment
Tip
If you use Babel and see errors while decaffeinati...
Fun with Ruby: Returning in blocks "overwrites" outside return values
In a nutshell: return statements inside blocks cause a method's return value to change. This is by design (and probably not even new to you, see below) -- but can be a problem, for example for the capture method of Rails.
Consider these methods:
def stuff
  puts 'yielding...'
  yield
  puts 'yielded.'
  true
end
We can call our stuff method with a block to yield. It works like t...
Better compression for /boot partition
If you struggle with a /boot partition that is too small for updates, and you are too intimidated by the way to resize your /boot partition, there might be an easier fix:
It is possible to configure a better compression algorithm for the images in /boot. To do this, edit /etc/initramfs-tools/initramfs.conf and change the existing line to
COMPRESS=xz
Then rebuild the images using
sudo update-initramfs -u -k all
If you get an error during the last step, please immediately get help, because otherwise...
Use "overflow: hidden" to avoid floating elements from wrapping a container's text
Consider this HTML:
<div id="container">
  <div id="actions">
    <a href="#">Click me!</a>
  </div>
  <div id="content">
    Hello Universe! Hello Universe! Hello Universe! Hello Universe! Hello Universe! Hello Universe!
  </div>
</div>
If you want the actions element to float on the left, you'd just say this in your CSS:
#actions { float: left; }
Unfortunately, any content of the content's text will wrap underneath it:
![paja9.png](https://makandracards.com/makandra/9245-use-overflow-hidden-to-a...
Function Composition in Ruby
Along with a number of other cool new features and performance improvements, Ruby 2.6 added function composition to the Proc and Method classes. Today we’ll take a look at how this allows us to use some functional programming goodness in our Ruby code.