Skip to content

Instantly share code, notes, and snippets.

@robertomiranda
robertomiranda / data_columns.sql
Created July 29, 2015 15:28
Extract column names based on its types (Postgres)
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_schema='public' AND table_name='eplot' AND data_type similar to '%(date|time)%';
rails g migration AddTokenToUsers token:string
=>
invoke active_record
create db/migrate/20150424010931_add_token_to_users.rb
var interPagosToken = {
create: function(idClient){
}
}
var InterPagos = (function(){
return {
token: interPagosToken
}
})();
class User
include Mongoid::Document
include Tire::Model::Search
include Tire::Model::Callbacks
tire do
mapping do
indexes :nickname
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
private
def record_not_found
render plain: "404 Not Found", status: 404
end
end
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
private
def record_not_found(error)
render :json => {:error => error.message}, :status => :not_found
end
end
@robertomiranda
robertomiranda / request_helper.rb
Last active August 29, 2015 14:14
request_helper.rb
#Controller Helpers for request
# rails/actionpack/lib/action_dispatch/http/request.rb
module RequestHelper
extend ActiveSupport::Concern
def current_path
request.fullpath
end
helper_method :current_path
end
module SchemaPlus
module ActiveRecord
# SchemaPlus adds several methods to the connection adapter (as returned by ActiveRecordBase#connection). See AbstractAdapter for details.
module ConnectionAdapters
#
# SchemaPlus adds several methods to
# ActiveRecord::ConnectionAdapters::AbstractAdapter. In most cases
# you don't call these directly, but rather the methods that define
# things are called by schema statements, and methods that query
conn = PG.connect( dbname: "mydb")
result = conn.exec(
"SELECT date_trunc('week', users.created_at) as regweek, COUNT(*)
FROM users
WHERE (created_at >= '2014-07-14' and created_at < '2014-11-30')
GROUP BY regweek
ORDER BY 1;")
begin
file = File.open("registration_count.csv", "wb")
module HasSecureToken
extend ActiveSupport::Concern
module ClassMethods
def has_secure_token(*attrs)
include InstanceMethodsOnActivation
cattr_accessor :token_columns
self.token_columns = attrs
before_create :_generate_token
end
end