Skip to content

Instantly share code, notes, and snippets.

View shamil614's full-sized avatar

scott hamilton shamil614

View GitHub Profile
@shamil614
shamil614 / scopes_on_joins.rb
Created April 28, 2014 16:15
Using Scopes on Arel Join Queries
# Let's say you have a join between Address and Job
# You want to use a scope on Job to limit the jobs that have to be joined otherwise you would join all jobs.
# So you try this
Address.joins('INNER JOIN jobs ON jobs.id = addresses.addressable_id AND addresses.addressable_type = "Job" AND ' \
'addresses.address_type = "physical"').where(Job.in_the_future)
# What you get is an error like so
# Job Load (1.1ms) SELECT `jobs`.* FROM `jobs` WHERE (start_datetime > '2014-04-28 16:06:12')
# (pry) output error: #<TypeError: Cannot visit ActiveRecord::Relation::ActiveRecord_Relation_Job>
# find any jobs that chronologically overlap an array of job_assignments
# ex: find any jobs that overlap an interpreter's existing jobs via the job_assignments
def self.overlapping(job_assignments)
jobs_table = Job.arel_table
query = Job.scoped
job_assignments.each do |ja|
query = query.where((jobs_table[:start_datetime].in(ja.scheduled_start..ja.estimated_end)).
or(jobs_table[:end_datetime].in(ja.scheduled_start..ja.estimated_end)))
end
query
if Rails.env.staging? || Rails.env.production?
begin
redis_config = YAML.load_file(Rails.root + 'config/redis.yml')[Rails.env]
Sidekiq.configure_server do |config|
config.redis = { :url => "redis://#{redis_config['host']}:#{redis_config['port']}", :namespace => 'nin' }
end
# When in Unicorn, this block needs to go in unicorn's `after_fork` callback:
Sidekiq.configure_client do |config|
@shamil614
shamil614 / attachment.rb
Created November 2, 2012 16:12
Carrierwave Attachment Uploader with AWS and Zencoder.
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
mount_uploader :item, AttachmentUploader
# background the storage of files to AWS and processing
# makes for fast uploads!
store_in_background :item
attr_accessible :item
before_save :update_attachment_attributes
@shamil614
shamil614 / string_chunk_by_words.rb
Created October 12, 2012 17:34
Split up a string by words using a character limit.
class String
def chunk_by_words(char_count)
words = self.split
s1_len = 0
word_count = 0
i = 0
while (i <= words.count-1)
s1_len += words[i].length
word_count += 1
i+=1
@shamil614
shamil614 / find_all_between_to_dates
Created April 6, 2012 17:59
Find between two dates
Object.find(:all, :conditions => ["created_at >= ? and created_at < ?", "#{DateTime.now.strftime("%Y-%m-%d")}", "#{(DateTime.now + 1).strftime("%Y-%m-%d")}"])
@shamil614
shamil614 / load_all_models_look_for_user.rb
Created April 5, 2012 19:12
Load all Models and iterate to see if user_id column exists
# Applies to Rails 2.3
# Since Rails doesn't load classes unless it needs them, you must read the models from the folder. Here is the code
Dir[Rails.root.to_s + '/app/models/**/*.rb'].each do |file|
begin
require file
rescue
end
end
Object.subclasses_of(ActiveRecord::Base).each do |model|
@shamil614
shamil614 / paginate_linkedin_results.rb
Created April 2, 2012 22:48
Paginate LinkedIn Gem Search Results
def paginate_linkedin_results(object,per_page=20)
# start = if params[:start] then params[:start] else count end
@total = object.total
@count = if object._count then object._count else per_page end
@start = if object._start then object._start else 0 end
@total_pages = (@total/per_page.to_f).ceil
url = if params[:start] then request.url else request.url + '&start=0' end
if @start <= 0
html_prev = content_tag("span", " << Prev ", :class => 'prev_page disabled')
@shamil614
shamil614 / bootstrap_form_builder.rb
Created February 20, 2012 22:15 — forked from cbmeeks/bootstrap_form_builder.rb
Twitter Bootstrap 2.0 Form Builder & Devise
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
delegate :capture, :content_tag, :tag, to: :@template
%w[text_field text_area password_field collection_select email_field].each do |method_name|
define_method(method_name) do |name, *args|
errors = object.errors[name].any?? " error" : ""
error_msg = object.errors[name].any?? content_tag(:span, object.errors[name].join(","), class: "help-inline") : ""
content_tag :div, class: "control-group#{errors}" do
@shamil614
shamil614 / escape_single_quotes.rb
Created December 15, 2011 19:10
Escape single quotes
class String
def escape_single_quotes
self.gsub(/[']/, '\\\\\'')
end
end