Skip to content

Instantly share code, notes, and snippets.

@jonpaul
jonpaul / registrations_controller.rb
Created July 24, 2012 19:08 — forked from jwo/registrations_controller.rb
API JSON authentication with Devise
class Api::RegistrationsController < Api::BaseController
respond_to :json
def create
user = User.new(params[:user])
if user.save
render :json=> user.as_json(:auth_token=>user.authentication_token, :email=>user.email), :status=>201
return
else
def update
@project = Project.find(params[:id])
@project.update_attributes!(params[:project])
respond_to do |format|
format.html do
if request.xhr?
# *** repond with the new value ***
render :text => params[:project].values.first
else
def create
@karretlink = Karretlink.new(params[:karretlink])
respond_to do |format|
if @karretlink.save
format.html { redirect_to @karretlink, notice: 'Your Karret was successfully created.' }
format.json { render json: @karretlink, status: :created, location: @karretlink }
else
flash.now.alert = "Something went wrong, Make sure to fill up everything. ( Make sure Karet's name have no space or special symbol)"
current_uri = request.env['PATH_INFO']
@jonpaul
jonpaul / rspec-syntax-cheat-sheet.rb
Created January 23, 2012 03:51 — forked from dnagir/rspec-syntax-cheat-sheet.rb
RSpec 2 syntax cheat sheet by example
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")
@jonpaul
jonpaul / public_controller.rb
Created October 28, 2011 18:25 — forked from bsodmike/public_controller.rb
Render 404 via catch-all route in Rails 3.1
class CmsPageNotFound < StandardError; end
class PublicController < ApplicationController
rescue_from CmsPageNotFound do
render 'not_found', :status => :not_found and return false
end
def index
end
@jonpaul
jonpaul / Capistrano-Deployment-Recipe.rb
Created October 19, 2011 13:22 — forked from mrrooijen/Capistrano-Deployment-Recipe.rb
a "base" Capistrano Rails Deployment Recipe. Use it to deploy your Rails application. It is also easily expandable. So feel free to grab this Recipe and add your own tasks/customization!
# Guide
# Configure the essential configurations below and do the following:
#
# Repository Creation:
# cap deploy:repository:create
# git add .
# git commit -am "initial commit"
# git push origin master
#
# Initial Deployment:
@jonpaul
jonpaul / application.rb
Created September 21, 2011 18:03 — forked from bsodmike/application.rb
Modified config/application.rb to print out initialization points for Rails 3.1
require File.expand_path('../boot', __FILE__)
require 'rails/application'
require 'rails/engine'
module Rails
class Application < Engine
def initializers
init = Bootstrap.initializers_for(self) #BOOTSTRAP
init += super #RAILTIES
init += Finisher.initializers_for(self) #FINISHER
@jonpaul
jonpaul / sanitize_helper.rb
Created August 10, 2011 14:24 — forked from workmad3/sanitize_helper.rb
Sanitize class macro
class ActiveRecord::Base
def self.sanitize_attribute(*attr_names)
attr_names.each do |attr_name|
define_method attr_name do
self[attr_name].try(:html_safe)
end
define_method "#{attr_name}=" do |attr_value|
self[attr_name] = attr_value.try(:sanitize)
end
end
@jonpaul
jonpaul / autotest.watchr.rb
Created June 14, 2011 02:12
Auto test + spork via watchr
#!/usr/bin/env watchr
#RUN_ALL_TESTS||=true
#watchr runs through this file a few times
#ensure it only gets set once
unless defined?(GROWL)
$stderr.sync=true
$stdout.sync=true
ENV["WATCHR"] = "1"
GROWL=`which growlnotify`.chomp
@jonpaul
jonpaul / watchr script
Created June 13, 2011 15:16 — forked from markbates/watchr script
A Watchr script for rails, rspec, and cucumber
ENV["WATCHR"] = "1"
system 'clear'
def growl(message)
growlnotify = `which growlnotify`.chomp
title = "Watchr Test Results"
puts message
image = message.match(/\s0\s(errors|failures)/) ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
system %(#{growlnotify} #{options} &)