Skip to content

Instantly share code, notes, and snippets.

@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
@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: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: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: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: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:1286351
Created October 14, 2011 05:55
Adding setter and getter of attributes of a AR model backed by an Hash
class MyModel < ActiveRecord::Base
serialize :preferences, Hash
PREFS_KEYS = [:hostname, :has_retail]
PREFS_KEYS.each do |key|
define_method(key) do
preferences[key]
end
def update_deliver_partially
@purchase_order = PurchaseOrder.find(params[:id], :include => {:purchased_items => {:item_variant => :item_family}})
# authorize! :update
@purchase_order.attributes = params[:purchase_order]
@stock_receipt = @purchase_order.stock_receipts.to_a.find{|sr| sr.new_record?}
#logger.debug @stock_receipt.inspect
# @purchased_items.each {|pi| p "in controller #{pi.wh_alloc_hash.inspect}"}
@gamov
gamov / gist:1041878
Created June 23, 2011 04:05
Getting a different model from an association
class Shipment
has_many :shipping_items, :inverse_of => :shipment, :dependent => :destroy do
def linked_po_ids
joins(:purchased_item => :purchase_order).select('DISTINCT purchase_orders.id AS po_id').map{|si| si.po_id}
def linked_pos
? #how to return the purchase orders (objects) ?
end
end
def linked_pos
@gamov
gamov / gist:1011864
Created June 7, 2011 08:01
Model Optimisation
def any_missing_specs
if item_variants.loaded?
item_variants.any?{|si| si.m3 == 0 || si.weight == 0}
else
item_variants.where({:m3 => 0 } | {:weight => 0}).size != 0 #metawhere
end
end