Skip to content

Instantly share code, notes, and snippets.

View schovi's full-sized avatar
👁️

David Schovanec schovi

👁️
View GitHub Profile
@schovi
schovi / gist:626293
Created October 14, 2010 14:51
Rails 3 scope for select model depend on tags. Include selecting more tags ( && or || for them)
scope :tagged_with, lambda { |tags, match_all = false|
tag_array = tags.is_a?(Array) ? TagList.new(tags) : TagList.from(tags)
where_operator = match_all ? :& : :|
where_conditions = tag_array.collect {|name| {:tags => :name =~ name} }
where_sql = where_conditions.inject(&where_operator)
select("DISTINCT prints.*").joins(:tags).where(where_sql)
}
@schovi
schovi / gist:665326
Created November 6, 2010 10:19
Rail3 friendly pagination
# If you are using ActiveRecord and want to paginate it you have to write this:
# PeacefulPaginate.use_for_active_record!
# to end of your config/environment.rb
# If you want to override default .paginate method from WillPaginate gem you have to insert this
# PeacefulPaginate.use_compatible_mod_with_will_paginate!
# to end of your config/environment.rb
# With this you dont have to rewrite nothing else in your application
# In other way how to use it
@schovi
schovi / active_record_mass_assign_attr_for_sti.rb
Created November 25, 2010 19:13
How to mass assign attribute 'type' for STI with validation
class Order < ActiveRecord::Base
validates_inclusion_of :type, :in => %w{SpecialOrder}
def self.new(params = {})
_type = params.delete(:type)
_new = super(params)
_new.type = _type.to_s.camelize if _type
return _new
end
@schovi
schovi / gist:734113
Created December 8, 2010 23:34
idea of chaining error validations
class UltraModel < ActiveRecord::Base
validates_presence_of :super_id
validates_format_of :super_id, :dependency => :super_id_presence
validates_uniqueness_of :super_id, :dependency => :super_id_format
validates :super_object_exist, :dependency => :super_id_uniqueness
private
def super_object_exist
@schovi
schovi / gist:763475
Created January 3, 2011 13:44
Mechanize in action
require 'rubygems'
require 'mechanize'
require 'fileutils'
def start
a = Mechanize.new
last_file = Dir.glob('wulf*.*')[-1]
if last_file and /.*?_(?<year>\d{4})\-(?<month>\d{2})-(?<day>\d{2})/ =~ last_file
strip = "#{year}/#{month}/#{day}"
@schovi
schovi / ActiveRecord ISBN Validation.rb
Created February 5, 2011 13:17
This peace of code provide validation of isbn (lenght, format, sum of number).
class Book < ActiveRecord::Base
attr_accessor :isbn
validate :format_of_isbn
private
def format_of_isbn
normalized_isbn = self.isbn.to_s.gsub(/_|-/, '')
if normalized_isbn.length == 10
@schovi
schovi / WillPaginateCustomUrlFix.rb
Created February 10, 2011 18:57
Quick WillPaginate fix, after that will_paginate method will accept param :url => my_route_path
class WillPaginate::ViewHelpers::LinkRenderer
def url(page)
@base_url_params ||= begin
url_params = base_url_params
merge_optional_params(url_params)
url_params
end
url_params = @base_url_params.dup
@schovi
schovi / gist:1025808
Created June 14, 2011 20:35
Ruby 1.9.x String#to_slug
module String
def to_slug
value = self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').to_s
value.gsub!(/[']+/, '')
value.gsub!(/\W+/, ' ')
value.strip!
value.downcase!
value.gsub!(' ', '-')
value
end
@schovi
schovi / jasmine.settimeout.hax.js
Created July 14, 2011 17:55
Hax of setTimeout with Jasmine test framework. Works only if setTimeout doesn't affect correct work of program.
describe("My amazing lib", function() {
beforeEach(function() {
this.oldSetTimeout = window.setTimeout;
window.setTimeout = function(cb) { cb() };
})
it("should correct process method with setTimeout", function() {
var instance = new MyLib();
instance.processMethodWithSetTimeout();
expect(instance.resultOfThatMethod).toBe("done");
schovi@vps:~$ sudo aptitude install nginx
The following NEW packages will be installed:
libgd2-noxpm{a} libjpeg62{a} nginx nginx-common{a} nginx-full{a}
0 packages upgraded, 5 newly installed, 0 to remove and 28 not upgraded.
Need to get 661 kB of archives. After unpacking 2126 kB will be used.
Do you want to continue? [Y/n/?] y
Get: 1 http://cz.archive.ubuntu.com/ubuntu/ oneiric/main libjpeg62 amd64 6b1-1ubuntu2 [88.3 kB]
Get: 2 http://cz.archive.ubuntu.com/ubuntu/ oneiric/main libgd2-noxpm amd64 2.0.36~rc1~dfsg-5.1ubuntu1 [198 kB]
Get: 3 http://cz.archive.ubuntu.com/ubuntu/ oneiric/universe nginx-common all 1.0.5-1 [14.6 kB]
Get: 4 http://cz.archive.ubuntu.com/ubuntu/ oneiric/universe nginx-full amd64 1.0.5-1 [354 kB]