Skip to content

Instantly share code, notes, and snippets.

@romansklenar
romansklenar / Inflector.php
Created December 11, 2009 21:42
Ruby on Rails Inflector port to PHP
<?php
/**
* The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys.
* This solution is partitionaly based on Ruby on Rails ActiveSupport::Inflector (c) David Heinemeier Hansson. (http://rubyonrails.org), MIT license
* @see http://api.rubyonrails.org/classes/Inflector.html
*
* @author Roman Sklenář
* @copyright Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
@romansklenar
romansklenar / Record.php
Created December 13, 2009 18:42
Record: simple abstraction above database row (DibiRow replacement)
<?php
/**
* Provide very simple abstraction above database row (DibiRow replacement)
*
* @author Roman Sklenář
* @copyright Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
* @license New BSD License
* @version 0.1
@romansklenar
romansklenar / Blob.php
Created August 15, 2010 03:36
Doctrine 2, BLOB datatype implementation
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
@romansklenar
romansklenar / sablona_seminarek.pdf
Created March 10, 2012 14:05
Šablona seminárek v LaTeXu
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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}'"
@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 / 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 / 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 / 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 / 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