Skip to content

Instantly share code, notes, and snippets.

@romansklenar
romansklenar / 20131118172653_create_transactional_items_view.rb
Last active July 3, 2017 09:15
Using PostgreSQL's materialized views as background for ActiveRecord models for flexible statistics
# db/migrate/20131118172653_create_transactional_items_view.rb
class CreateTransactionalItemsView < ActiveRecord::Migration
def up
select_sql = File.open("#{Rails.root}/db/migrate/20131118172653_create_transactional_items_view.sql", 'r') { |f| f.read }
# for materialized view:
view_sql = "CREATE MATERIALIZED VIEW transactional_items AS (#{select_sql})"
# for normal view:
view_sql = "CREATE VIEW transactional_items AS (#{select_sql})"
@romansklenar
romansklenar / rails_proficiency.txt
Created December 4, 2013 20:11
Proficiency requirements for Rails developer
Senior (enterprise)
Analyse and profile an application for performance and memory issues
Analyses and profile an application for security issues
Understand database modeling and query analysis
Tune a production deployment (Passenger, Thin, Apache etc)
Understand and use Ruby metaprogramming
Mentoring skills
Communication skills
Planning and Estimation
@romansklenar
romansklenar / REAME.md
Last active June 9, 2025 07:28
How to set up your VPS with Chef Solo

How to set up your VPS with Chef Solo

1. What is it?

There are many different provisioning tools out there, the most popular of which are Chef and Puppet. Chef uses Ruby, Puppet uses a DSL (Domain Specific Language), there are others that use simple bash too, but today we're going to focus on Chef Solo.

2. Dependencies

To get Chef working properly on your local machine you need a few things.

Make sure you use Ruby 1.9.x and not Ruby 2.x as you will get errors with the json 1.6.1 gem on 2.x. Use rbenv or RVM to manage several different Rubies on the one machine.

@romansklenar
romansklenar / README.md
Last active July 11, 2023 23:12
Rails concerns

What is it?

Collection of concerns for your Rails application

Installation

Copy to your app/models/concerns directory

@romansklenar
romansklenar / devise_resolver.rb
Created January 30, 2013 23:50
Devise template inheritance resolver
# Use only in devise overriden controllers to ensure template inheritance.
# Appending resolver will try to find template in app/views/users/passwords/new
# and it doesn't then it will fallback to app/views/devise/passwords/new
#
# ==== Usage
#
# class Users::PasswordsController < ::Devise::PasswordsController
# append_view_path DeviseResolver.new
# end
#
@romansklenar
romansklenar / devise_mailer.rb
Last active December 11, 2015 23:38
custom Devise mailer
class DeviseMailer < ApplicationMailer
include Devise::Mailers::Helpers
def confirmation_instructions(record)
devise_mail(record, :confirmation_instructions)
end
def reset_password_instructions(record)
devise_mail(record, :reset_password_instructions)
end
@romansklenar
romansklenar / seeds_generator.rb
Created January 29, 2013 21:49
Auditster: sample users CSV import generator
# company_names = (1..15).map { Faker::Company.name }
company_names = %w[Design Marketing Branding Stores Mobile Desktop Software Hardware] # departments
data = (1..500).map do
[ company_names.sample,
name = Faker::Name.name,
Faker::Internet.free_email(name.parameterize),
Faker::Internet.user_name(name.parameterize),
SecureRandom.urlsafe_base64(10).gsub('-', '') ]
end
@romansklenar
romansklenar / seeds.rb
Created January 10, 2013 17:45
Apple stocks import
require 'open-uri'
require 'roo'
apple = Company.create! do |company|
company.name = "Apple Inc."
company.code = "AAPL"
company.messages_url = "http://finance.yahoo.com/rss/headline?s=#{company.code}"
company.stocks_url = "http://ichart.finance.yahoo.com/table.csv?s=#{company.code}"
end
@romansklenar
romansklenar / README.md
Created January 8, 2013 16:00
Killing Rails observers

Killing Rails observers

  • decreases startup time by not loading all your models
  • makes it possible to preload config/environment.rb and still test models -> spin/zeus
  • makes dependencies obvious
  • replaces ActiveRecord magic with ruby
  • makes your app Rails 4 ready

Before

@romansklenar
romansklenar / foo.rb
Created July 19, 2012 12:38
Rails ActiveRecord Model protected against overwrite by another user updating same instance
class Foo < ActiveRecord::Base
attr_accessor :timestamp_control
after_initialize { self.timestamp_control ||= Time.zone.now }
before_validation :overwrite_check
def overwrite_check
errors[:base] << I18n.t('errors.messages.overwritten') if updated_at > timestamp_control
end