Skip to content

Instantly share code, notes, and snippets.

Cristiano cristianogregnanin

Block or report user

Report or block cristianogregnanin

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@cristianogregnanin
cristianogregnanin / config.ru
Last active Aug 26, 2015 — forked from chooh/config.ru
Simple Rack web server for static files with index.html
View config.ru
# This is the root of our app
@root = File.expand_path(File.join(File.dirname(__FILE__), "www"))
run Proc.new { |env|
# Extract the requested path from the request
req = Rack::Request.new(env)
index_file = File.join(@root, req.path_info, "index.html")
if File.exists?(index_file)
# Rewrite to index
View contactForm.html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/css/bootstrapValidator.min.css"/>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js"></script>
View transactions.markdown

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example

View capybara cheat sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
View db.rake
# lib/tasks/db.rake
namespace :db do
desc "Dumps the database to db/APP_NAME.dump"
task :dump => :environment do
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_dump --host #{host} --username #{user} --verbose --clean --no-owner --no-acl --format=c #{db} > #{Rails.root}/db/#{app}.dump"
end
puts cmd
View creating-nested-resources-in-ruby-on-rails-3-and-updating-scaffolding-links-and-redirection.markdown
View devise.it.yml
# Italian translation for Devise 3.2
# Date: 2013-20-12
# Author: xpepper
# Note: Thanks to fuzziness (https://gist.github.com/fuzziness/5262777)
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
it:
devise:
confirmations:
confirmed: "Il tuo account è stato correttamente confermato. Ora sei collegato."
@cristianogregnanin
cristianogregnanin / gist:78a5bb9c9e686727a0b0
Last active Aug 29, 2015
Extjs. Populate grid with ajax call
View gist:78a5bb9c9e686727a0b0
//click in search button ==> start the ajaxSearch_function() retrive the form data parameters and call the load method. ==>
//load method start an ajax call. Retrive the data from the server and populate the store.
//The grid is not yet populated. ==> The load method lunch the callback. ==> when callback terminate the grid is populated
Ext.onReady(function(){
var store = new Ext.data.JsonStore({
url: "my/json/url",
idProperty: "name",
root: "data",//parsing the json starting from data field. Ex: {"results":1,"errors":"","success":true,"data":[{"msg":"SomeData"}]}
View posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
@cristianogregnanin
cristianogregnanin / Guard-livereload
Last active Aug 29, 2015
Guard config to monitoring php project. Run this commands in your root project path
View Guard-livereload
#setup the Gemfile
$ nano Gemfile
source 'https://rubygems.org'
group :development do
gem 'guard'
gem 'guard-livereload', require: false
end
$ bundle
You can’t perform that action at this time.