Skip to content

Instantly share code, notes, and snippets.

View semanticart's full-sized avatar

Jeffrey Chupp semanticart

View GitHub Profile
def self.decode_content(c)
if c.header_str.match(/Content-Encoding: gzip/)
begin
gz = Zlib::GzipReader.new(StringIO.new(c.body_str))
xml = gz.read
gz.close
rescue Zlib::GzipFile::Error
xml = c.body_str
end
elsif c.header_str.match(/Content-Encoding: deflate/)
class Trade
def users_with_destroyed
User.find_with_destroyed(:all, :conditions => {:id => self.user_id})
end
def user_with_destroyed
User.find_with_destroyed(self.user_id)
end
end
require 'rubygems'
gem 'activerecord', '=2.3.2'
require 'activerecord'
load 'active_record/associations.rb'
load 'active_record/associations/has_many_through_association.rb'
ActiveRecord::Associations::HasManyThroughAssociation
class SomeClass < ActiveRecord::Associations::HasManyThroughAssociation
end
# thanks to @judofyr for explaining this to me like thus:
# when ruby sees "bar =" (even thought it's not eval'd) it creates a local variable and assigns it to nil.
# so the first "bar" is a method call, while the second is a local variable. it should work if you replace
# it with "self.bar".
class Foo
attr_accessor :bar, :id
def initialize id
@id = id
diff --git a/lib/is_paranoid.rb b/lib/is_paranoid.rb
index 3c11248..c74adf1 100644
--- a/lib/is_paranoid.rb
+++ b/lib/is_paranoid.rb
@@ -30,21 +30,33 @@ module IsParanoid
# NOTE: is_paranoid declaration must follow assocation declarations.
[:has_many, :has_one].each do |macro|
self.reflect_on_all_associations(macro).each do |assoc|
- if (a = assoc.klass.reflect_on_all_associations(:belongs_to).detect{ |a| a.class_name == self.class_name })
- assoc.klass.send(
diff --git a/lib/is_paranoid.rb b/lib/is_paranoid.rb
index cad76db..26624e0 100644
--- a/lib/is_paranoid.rb
+++ b/lib/is_paranoid.rb
@@ -38,13 +38,22 @@ module IsParanoid
# Use update_all with an exclusive scope to restore undo the soft-delete.
# This bypasses update-related callbacks.
#
- # By default, restores cascade through associations that are
+ # By default, restores cascade through associations that are belongs_to
>> project = Project.first
=> #<Project id: 1, name: "some project", created_at: "2009-05-16 16:37:41", updated_at: "2009-05-16 16:37:41">
>> project.tasks
=> []
>> project.tasks_attributes = [{:name => 'something'}]
=> [{:name=>"something"}]
>> project.save
=> true
>> project.tasks
=> [#<Task id: 3, name: "something", project_id: 1, deleted_at: nil, created_at: "2009-05-16 16:49:11", updated_at: "2009-05-16 16:49:11">]
# I'm interested in improvements or variations on doing this in rspec
# assuming we have an instance of ActiveSupport::Cache::MemCacheStore
# as the constant Mem and we want to check to see if a method changes
# the value for a particular key
# this comes into play in the first spec below...
class MemcachedValueFor
def self.method_missing name, *args
Mem.fetch(name)
class ActiveRecord::Base
def self.find_values opts
sql = self.send(:construct_finder_sql, opts)
self.connection.select_values(sql)
end
end
>> Tagging.find_values(:select => :taggable_id, :limit => 30, :order => 'tagged_at asc')
=> ["1", "3", "6", "6", "6", "10", "10", "10", "11", "11", "11", "10", "10", "10", "10", "10", "13", "13", "13", "13", "13", "13", "15", "15", "15", "15", "15", "15", "15", "15"]
def data
data = {
:success => true,
:site => Site.find(params[:id]).site
}
render :json => data.to_json
# Returns {
# 'success': true,
# 'site': {
# 'site': {