Skip to content

Instantly share code, notes, and snippets.

@gamov
gamov / gist:1350701
Created November 9, 2011 07:23
Trouble with assert_template with Partial
assert_template(:partial => 'shared/_item_variant_table_title')
#ArgumentError: assertion message must be String or Proc:
#<expecting partial <"shared/_item_variant_table_title"> but action rendered
# <["shared/_item_variant_table_title",
# "_item_variant_table_title",
# "shared/_flash_div",
# "_flash_div"]>>(<Test::Unit::Assertions::AssertionMessage>)
assert_template(:partial => 'shared/_item_variant_table_title', :count => 1)
#ArgumentError: assertion message must be String or Proc:
@gamov
gamov / gist:2213902
Created March 27, 2012 08:09
Delegate fails via a :through relationship until model is saved
class ReturnedItem < AR:Base
belongs_to :sold_item
delegate :unit_price, :to => :sold_item
has_one :item_variant, :through => :sold_item
delegate :full_name, :sku, :packaging, :uom_string, :to => :item_variant
end
#item_variant is another AR model
##
#My problem is
ReturnedItem.new(:sold_item => an_saved_sold_item).full_name =>
@gamov
gamov / gist:3606615
Created September 3, 2012 03:48
Rails nested builds
class Shipment < ActiveRecord::Base
has_many :shipping_items
end
class ShippingItem < ActiveRecord::Base
belongs_to :shipment
has_many :container_allocations
end
class ContainerAllocation < ActiveRecord::Base
belongs_to shipping_item
end
@gamov
gamov / gist:3897180
Created October 16, 2012 04:02
encapsulation?
has_many :own_use_contents, :dependent => :destroy, :inverse_of => :own_use_package do
def total_quantity
loaded? ? to_a.sum(&:quantity) : sum(:quantity)
end
end
@gamov
gamov / gist:4159841
Created November 28, 2012 08:22
Functional testing a controller with a scoped path?!?
#routes
scope :path => 'shipping_reports', :controller => 'ShippingReports', :as => 'shipping_reports' do
get 'index'
end
#test
class ShippingReportsControllerTest < ActionController::TestCase
test "routing" do
assert_routing '/shipping_reports/index', { :controller => "ShippingReports", :action => "index" }
@gamov
gamov / gist:4592607
Last active December 11, 2015 11:18
@search= ShippingItem.joins(:shipment => {:shipment_booking => {:dest_place => :address}}).
departed.
merge(Shipment.with_check_price_done).
merge(Shipment.eta_in_prev_months 12).
where(:unit_check_price.gt => 0).
select('MAX(shipment_bookings.eta) AS latest_eta, shipping_items.item_variant_id AS latest_iv_id, addresses.country AS latest_country').
group('shipping_items.item_variant_id, addresses.country').
search(params[:search])
@subquery = @search.relation.to_sql
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'
@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'
@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: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
..