Skip to content

Instantly share code, notes, and snippets.

View sergii's full-sized avatar

Serhii Ponomarov sergii

View GitHub Profile
@sergii
sergii / gist:78cfd5a5aed7faa91bf610a821b05d3c
Created June 13, 2021 07:35 — forked from mozamimy/gist:52c0004c8370f78df2c2
Validations for IP address and MAC address on Ruby on Rails
require "resolv"
class Model < ActiveRecord::Base
validates :ip_address, format: { with: Resolv::IPv4::Regex }
validates :mac_address, format: { with: /\A([0-9A-F]{2}[-:]){5}([0-9A-F]{2})\z/ }
end
@sergii
sergii / make_acronym.rb
Created June 12, 2021 12:47 — forked from lrechert/make_acronym.rb
Ruby Weekly Challenge - Make Acronym
# Implement a function called make_acronym that returns the first letters
# of each word in a passed in string. Make sure the letters returned are
# uppercase.
# If the value passed in is not a string return 'Not a string'
# If the value passed in is a string which contains only characters
# other than spaces and alphabet letters, return 'Not letters'
def make_acronym(input_string)
return nil unless input_string
select
*
from
tables
join (
select table_id
from books
where rating = 1
) as t1 on t1.table_id = tables.table_id
join (
@sergii
sergii / intersection_authorize.rb
Created June 11, 2021 13:21 — forked from travisofthenorth/intersection_authorize.rb
Intersection use case - authorize a user who has an allowed role/ability
class PaymentsController < AuthenticatedController
before_action { authorize!(roles: [:superuser], abilities: [:charge_user, :manage_payments]) }
end
class AuthenticatedController < ApplicationController
def authorize!(roles: [], abilities: [])
# current
(roles & current_user.roles).any? || (abilities & current_user.abilities).any?
# desired
@sergii
sergii / .__mini_rails__
Created May 27, 2021 10:24 — forked from eikes/.__mini_rails__
Single file rails app
__mini_rails__
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
@sergii
sergii / readme.md
Created May 18, 2021 13:34 — forked from maxivak/readme.md
Integrating Gem/Engine and Main Rails App
@sergii
sergii / rspec_model_testing_template.rb
Created April 1, 2021 10:35 — forked from PWSdelta/rspec_model_testing_template.rb
Rails Rspec model testing skeleton & cheat sheet using rspec-rails, shoulda-matchers, shoulda-callbacks, and factory_girl_rails. Pretty much a brain dump of examples of what you can (should?) test in a model. Pick & choose what you like, and please let me know if there are any errors or new/changed features out there. Reddit comment thread: http…
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems:
@sergii
sergii / rspec_model_testing_template.rb
Created April 1, 2021 10:34 — forked from rcoproc/rspec_model_testing_template.rb
Rails Rspec model testing skeleton & cheat sheet using rspec-rails, shoulda-matchers, shoulda-callbacks, and factory_girl_rails. Pretty much a brain dump of examples of what you can (should?) test in a model. Pick & choose what you like, and please let me know if there are any errors or new/changed features out there. Reddit comment thread: http…
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems:
@sergii
sergii / database_migration_best_practices.md
Created March 29, 2021 12:19 — forked from HelioCampos/database_migration_best_practices.md
Database migration best practices for Rails

Database migration best practices for Rails

Never ever change data on schema changes! [1]

Use rake tasks to change the data! [1]

This decouples a deployment from completed migrations. Give us control of the data manipulation proccess by encapsulatin it in on place. need to remember to:

  1. Run it in one of the ways bellow: a. Add this rake task the deployment script or;