Skip to content

Instantly share code, notes, and snippets.

this is a another test
new_arr = []
arr = [{:a=>"test1", :b=>1, :c=>2}, {:a=>"test1", :b=>4, :c=>6}, {:a=>"test2", :b=>4, :c=>5}]
types = arr.collect{|item| item[:a] }.uniq
types.each do |type|
related_hashes = arr.select {|item| item[:a] == type }
new_arr.push({
b: related_hashes.inject(0) {|sum, hash| sum + hash[:b]},
c: related_hashes.inject(0) {|sum, hash| sum + hash[:c]}
})
@dascgo
dascgo / test.rb
Last active September 9, 2015 12:45
No memoization between associations?
class Group
has_many :things
# external data would look like:
# {
# things: {
# 1: "some data for 1",
# 2: "some data for 2",
# etc...
# }
@dascgo
dascgo / gist:6779867
Last active December 24, 2015 09:49
type casting hstore key values
module HstoreAccessor
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def hstore_accessor(hstore_attribute, type, *keys)
Array(keys).flatten.each do |key|
# Dynamic setter
@dascgo
dascgo / pinterest_fan_ad.css
Created November 13, 2012 21:07
Pinterest Fan Ad CSS
#pin_hidden_container{ display: none; }
#pin_container{
width: 298px;
height: 248px;
background-color: #fff;
position: relative;
font-size: 12px;
font-family: Arial, Helvetica, sans-serif;
padding: 1px;
@dascgo
dascgo / pinterest_fan_ad.js
Created November 13, 2012 21:06
Pinterest Fan Ad JS
var pinterest = {
init: function(){
var ad_container = document.getElementById("ad");
ad_container.innerHTML = pinterest.template();
pinterest.pull_in_stylesheet();
google.setOnLoadCallback(pinterest.get_feed);
},
pull_in_stylesheet: function(){
# provided the query is right....
User.all(:joins => 'LEFT JOIN ad_scripts ON ad_scripts.user_id = users.id', :group => 'users.id', :having => 'COUNT(ad_scripts.id) = 0').each |user| do
user.send_later_slow(:create_ad, "300x250", "Staree", "desktop", 45, 0, 1) unless user.has_desktop_big_ad?
user.send_later_slow(:create_ad, "300x250", "Staree", "desktop", 45, 0, 2) unless user.has_desktop_small_ad?
user.send_later_slow(:create_ad, "320x50", "Mobile", "mobile", 45, 0, 3) unless user.has_mobile_ad?
end
@dascgo
dascgo / test.rb
Created October 29, 2012 14:45
remove duplicate imports
# sql select you were using
medias = Media.find_by_sql("SELECT user_id, name, comment, COUNT(medias.id) FROM medias INNER JOIN users ON medias.user_id = users.id WHERE comment <> "" AND upload_source IN ('Facebook Importer', 'Twitter Importer') GROUP BY comment HAVING COUNT(medias.id) > 1 ORDER BY COUNT(medias.id) DESC;")
# loop through all records found
medias.each do |media|
# get the duplicate records for a particular user
uploads = Media.find(:all, :conditions => {:user_id => media["user_id"], :comment => media["comment"]})
# loop through them
@dascgo
dascgo / gh.js
Created October 26, 2012 18:21
tool to add functionality to JIRA's greenhopper interface. This script is inserted into the page via a bookmarklet
var gh = {
setup: function(){
$(".gh_tag").live("click", gh.tags.cycle);
$(".ghx-corner").live("click", gh.time.update);
gh.subtasks.summarize();
},
@dascgo
dascgo / actions_test.html.erb
Created August 28, 2012 22:08
figuring out best way to show actions
# some view:
# V1:
<% actions.each do |action| %>
<%= render :partial => "actions/#{action.class.to_s.downcase}", :object => action %>
<% end %>
# V2:
<% actions.each do |action| %>
<%= render :partial => action.partial_name, :object => action %>