Skip to content

Instantly share code, notes, and snippets.

View artemv's full-sized avatar

Artem Vasilev artemv

View GitHub Profile
@artemv
artemv / nested_attributes_uniqueness_validator.rb
Last active May 21, 2022 13:28
Uniqueness validator for has_many associated items with accepts_nested_attributes_for
class NestedAttributesUniquenessValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
value = value.reject(&:_destroy) # let's ignore the items to be destroyed here
unless value.map(&options[:field]).uniq.size == value.size
record.errors[attribute] << "must be unique"
duplicates = value - Hash[value.map{|obj| [obj[options[:field]], obj]}].values
duplicates.each { |obj| obj.errors[options[:field]] << "has already been taken" }
end
end
end
@artemv
artemv / decode_session_cookie.rb
Last active June 14, 2019 20:41 — forked from pdfrod/decode_session_cookie.rb
A simple script to decode Rails 4 session cookies
@artemv
artemv / capybara cheat sheet
Last active August 9, 2017 19:46 — 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')
@artemv
artemv / migrate_file_uploads_from_database_to_amazon_in_rails.rake
Last active July 6, 2018 14:08
Migrate file uploads from database to Amazon S3 in Rails
task migrate_file_uploads_from_database_to_amazon: :environment do
Document.find_each do |d| # Document is our ActiveRecord model containing the `data` binary column (bytea Postgres type)
next if ENV['ID'] && d.id.to_s != ENV['ID'] # be able to say `ID=225 rake migrate_file_uploads_from_database_to_amazon` to process single record
next unless d.data # d.data is a binary column we were storing files at
print d.id
dir = "tmp/#{d.id}"
Dir.mkdir(dir) unless Dir.exist?(dir)
@artemv
artemv / sample.csv
Created March 28, 2019 11:45 — forked from RobVanGroenewoud/sample.csv
Sample CSV file with header row
firstName lastName email phoneNumber Data1 Data2
John Doe [email protected] 0123456789 X Y
Jane Doe [email protected] 9876543210 Y Z
James Bond [email protected] 0612345678 K Y
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tamedia</title>
<meta name="description" content="Tamedia Landing page">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>