Skip to content

Instantly share code, notes, and snippets.

View dmshvetsov's full-sized avatar
🪄

Dmitry Shvetsov dmshvetsov

🪄
View GitHub Profile
@dmshvetsov
dmshvetsov / gem_pg_install_for_fancycase.sh
Last active August 29, 2015 14:08
For PostrgreSQL installed from OS X packages 9.3 and bundle
gem install pg -v '0.17.1' -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
@dmshvetsov
dmshvetsov / next_n_work_days.rb
Last active August 29, 2015 14:16
Collect only 'n' weekdays including current date, but no weekends
require 'date'
def next_n_work_days n
today = Date.today
number_of_weekends_days = ((n / 5) *2)
(1..(number_of_weekends_days + n) - 1).reduce([today]) do |days, plus_num_days|
day = today + plus_num_days
# or
# day = days.last.next
@dmshvetsov
dmshvetsov / file_read_stup.rb
Created May 6, 2015 05:45
RSpec File read stub by Anatoly Spektor
data = {:name => "test" , :description => "test"}.to_json
allow(File).to receive(:read).and_return(data)
@dmshvetsov
dmshvetsov / react_ujs.js
Created December 10, 2015 05:09 — forked from rescribet/react_ujs.js
Non-global react_ujs
/*globals React, Turbolinks*/
/* Modified react_ujs to prevent the components polluting global scope whenever possible.
* Since I use subdirs for my components, it also flattens the structure making
* _componentStore[className] possible.
* Creds for the react_ujs.js file to the people from react-rails (https://github.com/reactjs/react-rails)
*/
var path = require('path');
@dmshvetsov
dmshvetsov / reset_constant.rb
Created February 4, 2016 02:13
Reset a constant. Helpful trick when you test a class/module variable.
if Object.const_defined?(:AbraCadabra)
Object.send(:remove_const, :AbraCadabra)
load 'abra_cadabra.rb'
end
@dmshvetsov
dmshvetsov / resource_dsl.rb
Last active August 6, 2018 00:16
closure_tree + activeadmin sorting feature. Inspired by activeadmin-sortable gem.
# included in ActiveAdmin as Rail initialized
# ::ActiveAdmin::ResourceDSL.send(:include, ActiveAdminExtension::ResourceDSL)
module ActiveAdminExtension
module ResourceDSL
# back-end for the $.fn.activeAdminSortable function
def sortable
member_action :sort, method: :post do
dropable_page = resource.siblings.find_by(position: params[:position])
new_position = params[:position].to_i
@dmshvetsov
dmshvetsov / benchmark_string_concatenation.rb
Created April 9, 2016 07:06
Benchmark different ways of string concatenation.
require 'benchmark'
A_STR = 'a string'
A_PREFIX = 'this is'
Benchmark.bm do |bm|
number_experiment = 1_000_000
bm.report('Interpolation') do
number_experiment.times { "#{A_PREFIX} #{A_STR}" }
@dmshvetsov
dmshvetsov / initvim_edit_reload_commands.vim
Last active April 10, 2016 21:33
Commands for working with $MYVIMRC in (neo)vim
" Working with init.vim
if !exists(":EditInitvim")
command EditInitvim :e $MYVIMRC
endif
if !exists(":ReloadInitvim")
command ReloadInitvim :source $MYVIMRC
endif
@dmshvetsov
dmshvetsov / rename.rake
Created September 8, 2016 22:59 — forked from invisiblefunnel/rename.rake
Rake task for renaming a Rails app
desc "Rename this application"
task :rename, [:name] => :environment do |t, args|
files = Dir.glob(%w(rb yml).map{ |ext| Rails.root.join("**/*.#{ext}") } + %w(Rakefile))
before = Rails.application.class.name.split('::').first
after = args.name or raise "Pass a new name as an argument: $ rake rename[MyCivicApp]"
files.each do |file|
# Swap in the new name
renamed = File.read(file).gsub(/#{before}/, after).gsub(/#{before.underscore}/, after.underscore)
# Write the updated contents
@dmshvetsov
dmshvetsov / _rails_npm_webpack_through_assets_pipeline.md
Last active July 9, 2017 15:38
Ruby on Rails, NPM, Webpack integration through Assets pipeline

The integration of NPM and Webpack in Rails

The idea

Use NPM packages through Webpack so Rails assets pipeline can do the usual job with assets that webpack will produce on output.

No changes how Rails works with assets. No additional or alternative assets helpers. Almost the same assets precompilation. process.

Integration