This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set :sync_directories, ["assets", "galleries"] | |
set :sync_backups, 3 |
OlderNewer