Skip to content

Instantly share code, notes, and snippets.

View gabeodess's full-sized avatar

Gabe Odess gabeodess

  • RigUp
  • Austin TX
  • 07:01 (UTC -05:00)
View GitHub Profile
@gabeodess
gabeodess / ransackable_scopes.rb
Created January 14, 2016 16:51 — forked from frahugo/gist:8ceb262e929d4c0a9575
Ransack with JSONB query
class Registration < ActiveRecord::Base
scope :nhb_consent_eq, -> (enum = "yes") {
where("document @> ?", { nhb_consent: { value: enum } }.to_json )
}
private
def self.ransackable_scopes(auth_object = nil)
%i(nhb_consent_eq)
end
module Arel
def self.add_predicate(predicate, operator)
$method_name = predicate
$operator = operator
$class_name = $method_name.to_s.camelcase
module Arel::Predications
define_method($method_name) do |right|
@gabeodess
gabeodess / custom_arel.rb
Last active September 20, 2020 23:16
An example of how to add a custom predicate to Arel
module Arel::Predications
def has_key(right)
Arel::Nodes::HasKey.new(self, quoted_node(right))
end
end
class Arel::Nodes::HasKey < Arel::Nodes::Binary
def operator; :"?" end
end
class WholesaleOrdersController < ApplicationController
PERMITTED_PARAMS = [:address_id, :payment_method, :pickup, :shipping_method, :wholesale_order_items_attributes => [:ounces, :quantity, :great_coffee_id]]
before_filter :activated_check!
before_filter :load_wholesale_order, :only => [:show, :pay, :send_check]
before_filter :new_wholesale_order_from_params, :only => [:create, :shipping]
skip_before_filter :authenticate_user!
class ImportCropsterDataJobTest < ActiveJob::TestCase
test "create blend batches" do
@blend = FactoryGirl.create(:great_coffee, :blend => true)
@c1 = @blend.blend_coffees.first
@c2 = @blend.blend_coffees.last
@c1.update_attribute(:cropster_id, 1)
@c2.update_attribute(:cropster_id, 2)
@lot_id = 1654109
ActiveAdmin.register User do
scope("all", :default => true)
scope("setup a subscription"){ |scope| scope.where("(SELECT COUNT(*) FROM subscriptions WHERE subscriptions.user_id = users.id LIMIT 1) = 1") }
scope("canceled"){ |scope| scope.where("
(SELECT COUNT(*) FROM subscriptions WHERE subscriptions.user_id = users.id LIMIT 1) = 1 AND
(SELECT COUNT(*) FROM subscriptions WHERE subscriptions.user_id = users.id AND subscriptions.archived_at IS NULL) = 0
") }
scope("active"){ |scope| scope.where(:id => Subscription.find_active.select(:user_id)) }
# scope("30 days +"){ |scope| scope.where(:id => Subscription.find_active.where("subscriptions.created_at < ?", 30.days.ago).select(:user_id)) }
Subscription Load (0.9ms) SELECT "subscriptions".* FROM "subscriptions" WHERE (subscriptions.away @> 'away_action => NULL')
=> 0
Subscription Load (1.2ms) SELECT "subscriptions".* FROM "subscriptions" WHERE (subscriptions.away @> 'away_action => NULL' OR away ? 'away_action')
=> 34
Subscription Load (2.4ms) SELECT "subscriptions".* FROM "subscriptions" WHERE ((away -> 'away_action') IS NULL)
=> 140
@gabeodess
gabeodess / test.rb
Last active September 27, 2015 21:08
class User < ActiveRecord::Base
after_initialize do
def addresses.<<(val)
push(val)
user.save!
end
end
def addresses
$bundle exec rake
Run options: --seed 62924
# Running:
................autoprefixer: /home/rof/src/bitbucket.org/sharecoffee/share/app/assets/stylesheets/application.scss:6859:5: Gradient has outdated direction syntax. New syntax is like "to left" instead of "right".
autoprefixer: /home/rof/src/bitbucket.org/sharecoffee/share/app/assets/stylesheets/application.scss:6868:5: Gradient has outdated direction syntax. New syntax is like "to left" instead of "right".
........................................autoprefixer: /home/rof/src/bitbucket.org/sharecoffee/share/app/assets/stylesheets/coming_soon.scss:6859:5: Gradient has outdated direction syntax. New syntax is like "to left" instead of "right".
autoprefixer: /home/rof/src/bitbucket.org/sharecoffee/share/app/assets/stylesheets/coming_soon.scss:6868:5: Gradient has outdated direction syntax. New syntax is like "to left" instead of "right".
#<HelloSign::Resource::SignatureRequest:0x007fe1d729c640
@data=
{"signature_request_id"=>"051eb2733453faf3fb0c2703ffd9aaf7237c8df8",
"title"=>"test4",
"original_title"=>"test4",
"subject"=>"test4",
"message"=>nil,
"test_mode"=>true,
"metadata"=>#<HelloSign::Resource::BaseResource:0x007fe1d729c438 @data={}, @raw_data={}>,
"is_complete"=>false,