Skip to content

Instantly share code, notes, and snippets.

View dmitry's full-sized avatar
🇪🇪
Water, earth and air.

Dmitry Polushkin dmitry

🇪🇪
Water, earth and air.
View GitHub Profile
@dmitry
dmitry / mechanize_auto_utf8.rb
Created January 21, 2012 20:13
Auto-convert all mechanize pages to UTF-8
class Mechanize
alias_method :original_get, :get
def get *args
doc = original_get *args
encoding = doc.encodings.last
doc.content.encode!('UTF-8', encoding)
doc
end
end
@dmitry
dmitry / autosave_nested_errors.rb
Created July 13, 2012 12:32
Temporary fix of roundlake/joosy issue #40
module AutosaveNestedErrors
extend ActiveSupport::Concern
included do
def autosave_nested_errors
valid? # can invoke errors second time
errors = self.errors.to_hash
reflections.each do |name, association|
if association.options[:autosave]
@dmitry
dmitry / add_widget.js.coffee
Created July 16, 2012 13:21
Joosy addWidget extension
# container - may be null or may be jquery tag
# widget - widget object
# func - can be string (append, prepend, after, before) or function with: container and content arguments
@Joosy.Modules.NewWidgetsManager =
addWidget: (container, widget, func) ->
uuid = container?.attr('id') || Joosy.uuid()
container?.attr('id', uuid)
render = ->
@dmitry
dmitry / autosave_errors.rb
Created August 2, 2012 17:04
Ruby on Rails extension to ActiveRecord to output nested errors for nested attributes in a right order and associate them with collection index. TODO make a better description of what it used for
def autosave_errors(params)
#valid?
errors = {}
params.each do |key, nested_params|
_, association_name = key.to_s.match(/\A(.+)_attributes\Z/).to_a
if association_name
association = association(association_name)
@dmitry
dmitry / handlers.rb
Created November 16, 2012 19:16
Rails rb handler to output json with partial support, tested with Rails 3.2
module Handlers
class Rb
def self.call(template)
if template.virtual_path.split('/').last =~ /^_/
"instance_eval { #{template.source} }"
else
"instance_eval { #{template.source} }.to_json"
end
end
end
@dmitry
dmitry / jquery.sticky.js
Last active October 13, 2015 06:38
Jquery sticky sidebar plugin
$.fn.sticky = function (obj, options) {
var $w = $(window);
return this.each(function () {
var o = $(this);
var height = o.outerHeight();
var placeholderObj = $("<div/>").css({
height: height + "px",
width: o.outerWidth() + "px",
top: o.css('top'),
@dmitry
dmitry / _search.html.haml
Created December 8, 2012 15:45
Custom filters/search in sidebar for active_admin using a partial
= form_for @search, url: collection_path,
as: :q,
builder: ActiveAdmin::Filters::CustomFormBuilder,
html: {method: :get, class: :filter_form} do |f|
= f.input :code_contains, as: :string
= f.input :code_contains, as: :filter_string
= f.form_buffers.last
@dmitry
dmitry / sort_photos.rb
Created December 8, 2012 17:18
Sort photos in Rails using FIND_IN_SET MySQL function
# TODO can be optimized to use custom class and `model_ids` method
module SortPhotos
def sort_photos!(ids)
raise ArgumentError, "ids isn't an array" if !ids.is_a?(Array)
ids.map!(&:to_i)
old_ids = self.photo_ids
ids = ids - (ids - old_ids)
raise ArgumentError, "Not all the ids were passed to sort! method" if old_ids.size != ids.size
reflections[:photos].klass.update_all(['position = FIND_IN_SET(id, ?)', ids.join(',')], {:id => ids})
@dmitry
dmitry / nginx-site.conf
Last active March 8, 2023 15:53 — forked from anonymous/nginx-site.conf
Shows how to generate sitemap with sitemap_generator gem for multilingual site and set rewrite urls in nginx conf.
rewrite ^/sitemap1.xml.gz /sitemaps/$host/sitemap1.xml.gz last;
rewrite ^/sitemap_index.xml.gz /sitemaps/$host/sitemap_index.xml.gz last;
# or
location /sitemap {
# it can be already switched on for globally
# gzip_static on;
rewrite ^/sitemap.xml /sitemaps/$host/sitemap.xml last; # that's will catch .gz files and served as normal static file, but with gzip header
namespace :routes do
desc "Writes doc/routes.html. Requires Graphviz (dot)"
task :visualizer => :environment do
File.open(Rails.root.join('doc', 'routes.html'), 'wb') do |f|
f.write Rails.application.routes.router.visualizer
end
end
end