Skip to content

Instantly share code, notes, and snippets.

@romansklenar
romansklenar / REAME.md
Last active February 19, 2020 14:14
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
@romansklenar
romansklenar / dependent_select.js.coffee
Last active June 25, 2020 00:05
Unobtrusive RESTful dynamic/dependent select menus for Ruby on Rails 3 and jQuery
# Unobtrusive RESTful dynamic/dependent select menus for Ruby on Rails 3 and jQuery
#
# USAGE (with Formtastic):
#
# match = form.object
# seasons = Season.all
# rounds = match.season.nil? ? Array.new : match.season.rounds
#
# form.input :season, :as => :select, :collection => seasons, :include_blank => false, :prompt => true, :input_html => { :id => :season_id }
# form.input :round, :as => :select, :collection => rounds, :include_blank => false, :prompt => true, :input_html => { :id => :round_id,
@romansklenar
romansklenar / string.rb
Last active March 8, 2017 01:20
Ruby string to boolean type casting
class String
def to_bool
case
when self == true || self =~ /^(true|t|yes|y|1)$/i
true
when self == false || self.blank? || self =~ /^(false|f|no|n|0)$/i
false
else
raise ArgumentError.new "invalid value for Boolean: '#{self}'"