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 / gist:31116
Created December 2, 2008 13:34 — forked from kpumuk/gist:31109
# Redefines const value during block execution.
#
# Usage example:
# SOMECONST = 1 # SOMECONST == 1
# 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)
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
jQuery.fn.extend({
// 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');
jQuery.fn.extend({
// 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');
jQuery.extend({
// 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.unshift(k);
result = this._findValue.apply(this, [v[options.fields.parent_id], result]);
var _that = this;
jQuery.extend({
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) {
console.log(arg);
}
@dmitry
dmitry / gist:188489
Created September 17, 2009 13:32 — forked from therobot/gist:102770
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"
else
rake = fetch(:rake, "rake")
rails_env = fetch(:rails_env, "production")
migrate_env = fetch(:migrate_env, "")
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
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]
attributes.delete(v)
end
@dmitry
dmitry / deploy.rb
Created November 26, 2009 00:42 — forked from netzpirat/deploy.rb
set :sync_directories, ["assets", "galleries"]
set :sync_backups, 3