Skip to content

Instantly share code, notes, and snippets.

@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'
# 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 / 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"}
@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 / 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 / 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`