Skip to content

Instantly share code, notes, and snippets.

Water, earth and air.

Dmitry Polushkin dmitry

Water, earth and air.
View GitHub Profile
View gist:31116
# Redefines const value during block execution.
# Usage example:
# redefine_const(:SOMECONST, 'hello') do
# puts SOMECONST # SOMECONST == 'hello'
# end
# # SOMECONST == 1
def redefine_const(const, value, &block)
if const_defined = Object.const_defined?(const)
View multi_page_form_in_rails.txt
Creating a Multi Page Form in Rails
Creating a multi-step wizard-like form is pretty difficult in Rails.
Especially when trying to apply REST and validations. There are many
different approaches to this problem, and the right one depends largely
on your application details. Here are four options, and then my final
recommendation. Skip to that if you're in a hurry.
1. Create the model at the beginning on the first page of the form, and
have each successive page update the model. Validations are tricky
View stylizeFileInput.js
// options: width: integer, height: integer, image: string - absolute url path
// example: $("input[type='file']").stylizeFileInput({width: 79, height: 22, image: '/images/file_button.gif'});
stylizeFileInput: function(options) {
return this.each(function(){
var el = $(this);
var div = $('<div>');
div.css('width', options.width);
div.css('height', options.height);
div.css('background', 'url(' + options.image + ') 0 0 no-repeat');
View gist:127178
// upload file through iframe, without refreshing the page, must be wrapped by form
// jquery.timers dependent
// $("input[type='file']").iframeFileUpload({blank_pathname: '/my_blank_file.html', autoSubmit: true, onStart: function(el){}, onStop(el){}});
iframeFileUpload: function(options) {
return this.each(function(){
var blank_pathname = options.blank_pathname || '/blank.html';
var el = $(this);
var form = el.parents('form');
var iframe_id = 'iframe_' + form.attr('id');
View gist:152166
// Example: console.log(jQuery.chainSelect('search_location', locations_object, {levels: ['continent', 'country', 'region', 'city', 'suburb']}));
chainSelect: function(id, json, options) {
this._findValue = function(value, result) {
for (k in this.json) {
var v = this.json[k];
if (k == value) {
result = this._findValue.apply(this, [v[options.fields.parent_id], result]);
View gist:152649
var _that = this;
someFunction: function(onSomeEvent) {
var variable = 'some text';
onSomeEvent.apply(_that); // how to pass current scope variables/functions to this function?
return null;
function _someMethod(arg) {
View gist:188489
before "deploy:rollback:revision", "deploy:rollback_database"
desc "Rolls back database to migration level of the previously deployed release"
task :rollback_database, :roles => :db, :only => { :primary => true } do
if releases.length < 2
abort "could not rollback the code because there is no prior release"
rake = fetch(:rake, "rake")
rails_env = fetch(:rails_env, "production")
migrate_env = fetch(:migrate_env, "")
View gist:189479
desc "Moves files to a new :path NB! backup your files before use this task (set CLASS and PATH, ATTACHMENT is optional)"
task :change_path => :environment do
raise 'PATH must be specified, like PATH=:rails_root/public/system/:class/:attachment/:style/:id.:extension' if ENV['PATH'].blank?
path = ENV['PATH']
for_all_attachments do |instance, name|
object = instance.send(name)
old_path = object.instance_variable_get :'@path'
styles = [:original] + object.styles.keys
View gist:223480
self.include_root_in_json = false
def export
json = to_json(:include => [:door_profile, :door_lift, :door_hinge, :door_handle, :door_glass], :except => [:created_at, :updated_at, :door_id, :id, :order_id, :user_id])
attributes = ActiveSupport::JSON.decode(json)
%w[door_profile door_lift door_hinge door_handle door_glass].each do |v|
if attributes[v]
attributes["#{v.to_s}_attributes"] = attributes[v]
View deploy.rb
set :sync_directories, ["assets", "galleries"]
set :sync_backups, 3
You can’t perform that action at this time.