Skip to content

Instantly share code, notes, and snippets.

@gamov
gamov / request.sql
Last active June 7, 2016 10:02
ActiveRecord Auto Aliasing
-- shipments is aliased to shipments_shipping_items by AR because it is present in the subquery
-- this particular query fails because we join business_sites on shipments but shipments was aliased shipments_shipping_items
SELECT
`shipping_items`.*
FROM
`shipping_items`
INNER JOIN
`item_variants` ON `item_variants`.`id` = `shipping_items`.`item_variant_id`
INNER JOIN
`shipments` `shipments_shipping_items` ON `shipments_shipping_items`.`id` = `shipping_items`.`shipment_id`
@gamov
gamov / ar.rb
Last active May 5, 2016 09:36
ActiveRecord Queries Aliasing
# Rails 4.0.13
items = ShippingItem.joins(shipment: :booking)
.joins("INNER JOIN (#{
ShippingItem.joins(shipment: :booking).select('max(booking.eta), shipment.dest_site_id').group('shipments.dest_site_id').to_sql
}) AS sub sub.dest_site_id = shipments.dest_site_id AND booking.eta = sub.max_eta")
.order(Shipment.arel_table[:dest_site_id])
# The outer query tables get automagically aliased (`shipments` `shipments_shipping_items`, `shipment_bookings` `shipment_bookings_shipments`)
@gamov
gamov / ruby.rb
Created September 18, 2015 05:27
Default value for an implicit block
def assert_sorted array, &block
block ||= lambda {|i,j| i+1 < j+1}
assert array.each_cons(2).all?(&block), 'Result data not correctly sorted'
end
assert_sorted [1,3,5] {|i,j| i+1 < j+1} #works
assert_sorted [1,3,5] #triggers:
ArgumentError: wrong number of arguments (1 for 2)
ruby.rb:2:in `block in assert_sorted'
@gamov
gamov / partial.html.haml
Created September 7, 2015 08:42
Yield in partial
# Problem: block_given always retruns true, either I call the partial with or without a block...
- if block_given?
#hidden_deletion_element= yield
- else
= link_to(deletion_path, method: :delete, confirm: 'Are you sure?', id: :hidden_deletion_element) { "Delete Me"}
# inspired by http://www.scottraymond.net/2005/9/20/validating-x-html-in-rails/
def assert_valid_markup(markup = @response.body, fragment = false)
str = if fragment
"<!doctype html><head><title>T</title></head><body>#{markup}</body></html>"
else
markup.to_str # .to_str to remove SafeBuffer stuff
end
require 'net/http'
# http://validator.w3.org/docs/api.html
@gamov
gamov / gist:4718054ac76d2742d6b4
Last active August 29, 2015 14:08
Custom setting in config.action_mailer
config.action_mailer.host_from_outside= 'url'
config.action_mailer[:host_from_outside]= 'url'
#both triggers:
/Users/gamov/.rvm/gems/ruby-1.9.3-p545@ector/gems/actionmailer-3.0.20/lib/action_mailer/deprecated_api.rb:74:in `method_missing': undefined method `host_from_outside=' for ActionMailer::Base:Class (NoMethodError)
from /Users/gamov/.rvm/gems/ruby-1.9.3-p545@ector/gems/actionmailer-3.0.20/lib/action_mailer/base.rb:452:in `method_missing'
from /Users/gamov/.rvm/gems/ruby-1.9.3-p545@ector/gems/actionmailer-3.0.20/lib/action_mailer/railtie.rb:26:in `block (3 levels) in <class:Railtie>'
from /Users/gamov/.rvm/gems/ruby-1.9.3-p545@ector/gems/actionmailer-3.0.20/lib/action_mailer/railtie.rb:26:in `each'
from /Users/gamov/.rvm/gems/ruby-1.9.3-p545@ector/gems/actionmailer-3.0.20/lib/action_mailer/railtie.rb:26:in `block (2 levels) in <class:Railtie>'
from /Users/gamov/.rvm/gems/ruby-1.9.3-p545@ector/gems/activesupport-3.0.20/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
@gamov
gamov / gist:8fe38733012931eb3360
Last active December 18, 2015 08:54
Rails: postgres adapter returns string instead of integer
ris = RequestedItem.currently_requested.joins(:order_request).
group(:item_variant_id).select('requested_items.*, order_request.business_site_id').all
ris.first[:business_site_id].class == String #instead of Fixnum...
#with SQLite with can:
if ris.first[:business_site_id] == DEFAULT_SITE
..
#but with PG, we must:
if ris.first[:business_site_id].try(:to_i) == DEFAULT_SITE
..
@gamov
gamov / gist:71111af88b0802e5ec2c
Last active August 29, 2015 14:01
How to fake Date.current?
def adjusted real_value, type, &block
puts @data
key = [real_value, type]
if @data.include? key
@data[key][1]= Date.current # <- calls Date.current internally to keep track of oldest record,
# How can i alter the return of Date.current for it to return eg yesterday
else
trim_data
@data[key] = [yield, Date.current]
@data[key].first
@gamov
gamov / gist:9501394
Last active August 29, 2015 13:57
Paper Trail single file Rails test
# Activate the gem you are reporting the issue against.
gem 'rails', '3.0.20'
gem 'paper_trail', '3.0.0'
require 'rails/all'
require 'action_controller/railtie'
# ENV["RAILS_ENV"] = "test"
require 'rails/test_help'
test 'create a standard po with items' do
po_count = PurchaseOrder.count
visit new_purchase_order_path
page.select po.supplier.name, from: 'purchase_order_supplier_id'
page.select po.company_profile.name, from: 'purchase_order_company_profile_id'
page.select po.delivery_site.name, from: 'purchase_order_delivery_site_id'
page.click_button 'purchase_order_submit'