Skip to content

Instantly share code, notes, and snippets.

@cristianogregnanin
cristianogregnanin / jsonapi_testing.rb
Created February 26, 2024 10:08 — forked from LoreRizzetto/jsonapi_testing.rb
Jsonapi testing in ruby
require 'json'
require "faraday"
response = Faraday.get("http://127.0.0.1:8081/products")
raise "Test fallito" unless response.status == 200
gotten = JSON.parse(response.body)
raise "Test fallito" unless gotten.has_key?("data")
raise "Test fallito" unless gotten["data"].is_a? Array
raise "Test fallito" unless gotten["data"][0].has_key?("type")
@cristianogregnanin
cristianogregnanin / Backup-and-Restore-GPG-and-SSH-keys.md
Created January 11, 2023 10:10 — forked from colematt/Backup-and-Restore-GPG-and-SSH-keys.md
[Backup and Restore GPG and SSH keys] #git #gpg #ssh

Backup

  1. Copy both id_rsa and id_rsa.pub from ~/.ssh/ to a USB drive. Identify the private key by executing the following command.
    gpg --list-secret-keys --keyid-format LONG
    
  2. It will show something similar to this.
    sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
    
@cristianogregnanin
cristianogregnanin / config.ru
Last active August 26, 2015 16:22 — forked from chooh/config.ru
Simple Rack web server for static files with index.html
# This is the root of our app
@root = File.expand_path(File.join(File.dirname(__FILE__), "www"))
run Proc.new { |env|
# Extract the requested path from the request
req = Rack::Request.new(env)
index_file = File.join(@root, req.path_info, "index.html")
if File.exists?(index_file)
# Rewrite to index

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example

@cristianogregnanin
cristianogregnanin / capybara cheat sheet
Last active August 29, 2015 14:25 — forked from zhengjia/capybara cheat sheet
capybara cheat sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
# lib/tasks/db.rake
namespace :db do
desc "Dumps the database to db/APP_NAME.dump"
task :dump => :environment do
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_dump --host #{host} --username #{user} --verbose --clean --no-owner --no-acl --format=c #{db} > #{Rails.root}/db/#{app}.dump"
end
puts cmd
# Italian translation for Devise 3.2
# Date: 2013-20-12
# Author: xpepper
# Note: Thanks to fuzziness (https://gist.github.com/fuzziness/5262777)
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
it:
devise:
confirmations:
confirmed: "Il tuo account è stato correttamente confermato. Ora sei collegato."