Skip to content

Instantly share code, notes, and snippets.

View igor-alexandrov's full-sized avatar
😀
Ruby, Ruby, Ruby, Ruby... Crystal :)

Igor Aleksandrov igor-alexandrov

😀
Ruby, Ruby, Ruby, Ruby... Crystal :)
View GitHub Profile
require 'rainbow'
def log(level, message)
Rails.logger.send level, message.foreground(color_for_level(level))
if RAILS_ENV == 'development'
puts level.to_s.upcase " " message.foreground(color_for_level(level))
end
end
def color_for_level(level)
@igor-alexandrov
igor-alexandrov / add floor and ceil to Time
Created April 16, 2010 05:33
Add floor() and ceil() methods to Time class
class Time
def ceil(seconds = 60)
Time.at((self.to_f / seconds).ceil * seconds)
end
def floor(seconds = 60)
Time.at((self.to_f / seconds).floor * seconds)
end
end
@igor-alexandrov
igor-alexandrov / application_controller.rb
Created August 6, 2010 12:50
DRY up empty controller actions
class Admin::AdminController < ActionController::Base
def self.simple_action(*actions)
actions.each {|action| class_eval("def #{action}; end")}
end
end
@igor-alexandrov
igor-alexandrov / settings.rb
Created December 11, 2011 18:51
Multiple configuration files in SettingsLogic
class Settings < Settingslogic
source "#{Rails.root}/config/application.yml"
if Rails.env == 'development'
namespace 'development-' + Socket.gethostname.gsub(/\W/, '-').gsub(/[-]{2,}/, '-')
else
namespace Rails.env
end
# load local settings
@igor-alexandrov
igor-alexandrov / gist:1599584
Created January 12, 2012 09:36
AuthLogic controller helpers for multiple sessions
module Application::Controller::Authlogic
def self.included(base)
base.extend Application::Controller::Authlogic::ClassMethods
end
module ClassMethods
end
def current_session(id=nil)
if User.session_ids.include?(id)
@igor-alexandrov
igor-alexandrov / gist:4612075
Created January 23, 2013 19:41
Parameters fix for Rails 2.3.x and Ruby 1.9.x
class ActionController::Base
def force_utf8_params
traverse = lambda do |object, block|
if object.kind_of?(Hash)
object.each_value { |o| traverse.call(o, block) }
elsif object.kind_of?(Array)
object.each { |o| traverse.call(o, block) }
else
block.call(object)
@igor-alexandrov
igor-alexandrov / gist:6942754
Created October 11, 2013 22:05
Complete example of how to map association from Sequel::Model to AR. Eager loading works like a charm!
class Tour < Sequel::Model
many_to_one :vendor_pansion,
:dataset => proc{ Vendor::Pansion.where(:id => self.vendor_pansion_id) },
:reciprocal => nil, :class => 'Vendor::Pansion',
:eager_loader => (proc do |opts|
opts[:rows].each{ |object| object.associations[:vendor_pansion] = nil }
Vendor::Pansion.where(:id => opts[:id_map].keys).each do |vendor_pansion|
if tours = opts[:id_map][vendor_pansion.id]
tours.each do |tour|
@purchased_at = Date.civil(params[:purchased_at]["(1i)"].to_i,
params[:purchased_at]["(2i)"].to_i,
params[:purchased_at]["(3i)"].to_i)
@code = Partner::Code.lookup("Vendor", params[:code]) || build_vendor_code
@not_redeem = nil
@error_text = nil
@to_late = nil
if @code.nil?
@igor-alexandrov
igor-alexandrov / form.rb
Last active March 29, 2017 08:51
reform2 custom predicates with dry-validation
class Fund::CreateCallForm < BaseForm
# your properties are here
# ...
validation do
configure do
config.messages_file = 'config/locales/dry-validation/errors.yml'
# don't forget about this option to be able to access your form object from your predicates
option :form
@igor-alexandrov
igor-alexandrov / form.rb
Last active July 19, 2019 03:13
dry-validation conditional validation
class Fund::CreateCallForm < BaseForm
property :type
property :fund
validation do
required(:type) { filled? & included_in?(TRANSACTION_TYPES['Fund::Call']) }
end
validation if: -> (results) { rebalance? } do
required(:fund).filled