Skip to content

Instantly share code, notes, and snippets.

@xarimanx
xarimanx / web-servers.md
Last active August 29, 2015 14:27 — forked from willurd/web-servers.md
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000
@xarimanx
xarimanx / tutor.md
Last active August 29, 2015 14:26
dummy app for gem

Original - https://gist.github.com/ramontayag/1045123

Intro

Problem: I learned how to run RSpec to test classes and modules in my gem. But, if I wanted to test my gem as if it were already included in a Rails app, I couldn't find much info about it online.

I usually do unit tests with RSpec, and I test the whole stack with Cucumber. So here we will just setup Cucumber to run the Rails app that you embed in your gem.

Before we begin:

@xarimanx
xarimanx / nested_with_virtual
Last active August 29, 2015 14:19
virtual attributes in nested attributes
Accepts Nested Attribute with a virtual attribute
I have a Project model which accepts nested attributes for tasks. And Task has a virtual attribute "name". So every time I change the name, it gets persisted as encrypted_task_name before update. On the project edit page the form has a input field for task name (and not encrypted_task_name). When I change the name and since name is a virtual attribute, Rails doesn't detect a change in Task and doesn't update that task while updating Project.
How do I make sure that task is saved even if its virtual attributes are changed during Project update?
One option that I don't want to use is :autosave => true on task.rb since I task is rarely updated.
---------
I ran into the same problem. Using :autosave => true didn't even work for me. I managed to solve it by adding attribute_will_change!(:my_virtual_attribute) to the writer for my virtual attribute. So, in your case:
# cap staging rake TASK=ts:config
namespace :rake do
desc "remote rake task"
task :default do
run "cd #{current_path}; RAILS_ENV=#{rails_env} rake #{ENV['TASK']}"
end
end
@xarimanx
xarimanx / db_backup.rake
Created January 16, 2015 15:59
Backup project database. Options: DIR=backups RAILS_ENV=production MAX=7
# encoding: utf-8
namespace :db do desc "Backup project database. Options: DIR=backups RAILS_ENV=production MAX=7"
task :backup => [:environment] do
datestamp = Time.now.strftime("%Y-%m-%d_%H-%M-%S")
base_path = Rails.root
base_path = File.join(base_path, ENV['DIR'] || "backups")
backup_base = File.join(base_path, 'db_backups')
file_name = "#{datestamp}_#{Rails.env}_dump.sql"
backup_file = File.join(backup_base, file_name)
@xarimanx
xarimanx / scheduler.js
Created May 7, 2014 11:31
dhtmx scheduler init
$(document).ready(function() {
scheduler.config.xml_date="%Y-%m-%d %H:%i";
scheduler.config.start_on_monday = false;
scheduler.config.show_loading = true;
scheduler.config.prevent_cache = true;
/*
scheduler.config.collision_limit = 1
scheduler.attachEvent("onEventCollision", function (ev, evs){
console.log(ev);
console.log(evs);
http://codepen.io/akademy/pen/FlkzB
#Model
@user.should have(1).error_on(:username) # Checks whether there is an error in username
@user.errors[:username].should include("can't be blank") # check for the error message
#Rendering
response.should render_template(:index)
#Redirecting
response.should redirect_to(movies_path)
@xarimanx
xarimanx / screen.js
Created April 17, 2014 08:00
JS screenshots
(function (exports) {
function urlsToAbsolute(nodeList) {
if (!nodeList.length) {
return [];
}
var attrName = 'href';
if (nodeList[0].__proto__ === HTMLImageElement.prototype
|| nodeList[0].__proto__ === HTMLScriptElement.prototype) {
attrName = 'src';
}
@xarimanx
xarimanx / assets.rake
Created April 7, 2014 08:42
# check that all assets have the correct encoding # https://gist.github.com/1301199
# check that all assets have the correct encoding
# https://gist.github.com/1301199
namespace :assets do
desc "Check that all assets have valid encoding"
task :check => :environment do
paths = ["app/assets", "lib/assets", "vendor/assets"]
extensions = ["js", "coffee", "css", "scss"]