Skip to content

Instantly share code, notes, and snippets.

View razorcd's full-sized avatar

Cristian Dugacicu razorcd

View GitHub Profile
@razorcd
razorcd / restore_pg_dump.sh
Last active August 29, 2015 14:17
restore PG db dump
pg_restore -vwc -U postgres -d database_name file.dump
@razorcd
razorcd / rspec_custojm_matcher.rb
Created March 11, 2015 18:38
Rspec custom matcher
#spec/support/model_helpers.rb
# use like this:
# it { is_expected.to have_validation ValidDatePresenceValidator,
# :on => :start_date,
# :options => [:allow_blank, :allow_nil]
# }
RSpec::Matchers.define :have_validation do |validator, params|
match do |model|
my_validators = model.class.validators.select { |v| v.class == validator and v.attributes.include? params[:on] }
my_validators = validators.select { |v| params[:options].all? { |po| v.options.any? { |vo| vo == po } } } if params[:options]
@razorcd
razorcd / custom_model_validator.rb
Created March 11, 2015 18:36
Custom model validators in Rails
#app/models/validators/valid_date_presence_validator.rb
class ValidDatePresenceValidator < ActiveModel::EachValidator
include DateHelpers
def validate_each(record, attribute, value)
unless valid_date? value
record.errors[attribute] << ('is not a valid date')
end
end
bm = ->(counter=1) {
Benchmark.measure do
counter.times do |i|
Sponsorship.joins(:sponsor, :orphan).
where("sponsor_id = ?",1).
select("orphans.name as orphans_name, orphans.date_of_birth as orphans_date_of_birth, orphans.gender as orphans_gender").
to_a
# puts i
end
end
<!--
HTML5 data- attributes using RESTful approach
HTML5 specifies extensible attributes like data-foo=“bar” (or as in Twitter Bootstrap data-toggle=“modal”), which poses two problems for Rails.
First, if you’re using symbol notation in link_to (for instance) to specify attributes, this fails (dash is not a valid symbol character), so:
-->
<% link_to('Edit', '../main.html', :class => "btn", :data-toggle => "modal") %> <!-- INVALID!! -->
<!--
There are two solutions: