Skip to content

Instantly share code, notes, and snippets.

@sergio-fry
sergio-fry / docker-dev
Created December 1, 2022 15:11
docker run helpers
#!/bin/bash
docker_dev_home=$HOME/.docker-dev
project_name=${PWD##*/}
image_name=`echo $project_name | tr '[:upper:]' '[:lower:]'`-dev
docker create --name $image_name-volumes -v "/usr/local/bundle" -v "/root/.gradle" busybox || true
#!/bin/bash
docker_dev_home=$HOME/.docker-dev
project_name=${PWD##*/}
image_name=`echo $project_name | tr '[:upper:]' '[:lower:]'`-dev
docker create --name $image_name-volumes -v "/usr/local/bundle" -v "/root/.gradle" busybox || true
@sergio-fry
sergio-fry / example.rb
Last active October 30, 2020 10:55
Code example
class User < ApplicationRecord
has_many :posts
after_create :notify_moderator_by_sms_about_new_user
def deactivate!
puts "User deactivation started"
notify_moderator_by_sms_about_user_deactivation
self.update_attribute :role, :disabled
@sergio-fry
sergio-fry / fixtures.rb
Created July 9, 2020 13:45
RSpec fixtures loader helper
module RSpec
module Fixtures
def fixture_file(name)
File.open(Rails.root.join('spec', 'fixtures', name))
end
def fixture_content(name)
fixture_file(name).read
end
@sergio-fry
sergio-fry / rake_helpers.rb
Last active June 5, 2020 12:02
RSpec rake support module
APP_RAKEFILE = Rails.root.join('Rakefile')
module Support
module RakeHelpers
extend ActiveSupport::Concern
def rake(task)
Rake::Task[task].reenable
Rake.application.invoke_task(task)
end
#!/usr/bin/env ruby
require "yaml"
require "ostruct"
class Service
attr_reader :compose, :name
def initialize(compose, name)
@name = name
@compose = compose
@sergio-fry
sergio-fry / code.rb
Last active November 1, 2019 07:14
clean arch examples
#############################################
# Example 1
BookRepository.new.where(author_id: 23).order(:published_at).limit(8)
#############################################
# Example 2
@sergio-fry
sergio-fry / git_message.rb
Created October 29, 2019 08:49
Git message generator
#!/usr/bin/env ruby
# frozen_string_literal: true
last_comment = `git log --pretty=oneline --abbrev-commit | head -n 1`.split("\n").first
def generate_message(n)
"changes ##{n} at #{Time.now.strftime "%H:%M:%S %a %d.%m.%y"}"
end
message = if last_comment.nil?
@sergio-fry
sergio-fry / my_controller_spec.rb
Created February 22, 2019 18:49
Controller spec example
require 'rails_helper'
RSpec.describe MyController, type: :controller do
describe 'GET #index' do
render_views
include_context 'with admin authenticated'
it 'returns http success' do
get :index, params: params
expect(response).to have_http_status(:success)
@sergio-fry
sergio-fry / active_record_to_json.rb
Last active January 25, 2019 06:56
ActiveRecord JSON examples
User.limit(2).as_json
# User Load (2.0ms) SELECT "users".* FROM "users" LIMIT ? [["LIMIT", 2]]
#=> [{"id"=>1, "name"=>"Анна Терентьева", "created_at"=>Wed, 23 Jan 2019 21:47:37 UTC +00:00, "updated_at"=>Wed, 23 Jan 2019 21:47:37 UTC +00:00, "photo_url"=>nil, "nickname"=>nil, "first_name"=>nil, "last_name"=>nil, "about"=>nil, "birthday"=>nil}, {"id"=>2, "name"=>"Абрамова София", "created_at"=>Wed, 23 Jan 2019 21:47:38 UTC +00:00, "updated_at"=>Wed, 23 Jan 2019 21:47:38 UTC +00:00, "photo_url"=>nil, "nickname"=>nil, "first_name"=>nil, "last_name"=>nil, "about"=>nil, "birthday"=>nil}]
User.select(:id, :name).limit(2).as_json
# User Load (1.4ms) SELECT "users"."id", "users"."name" FROM "users" LIMIT ? [["LIMIT", 2]]
#=> [{"id"=>2, "name"=>"Абрамова София"}, {"id"=>20, "name"=>"Ангелина Нестерова"}]
User.select(:id, :name).where('start_at > ?', Time.now).limit(2).as_json