Skip to content

Instantly share code, notes, and snippets.

View jagira's full-sized avatar
🙃

Jigar Patel jagira

🙃
View GitHub Profile
require 'net/http'
require 'uri'
require 'rubygems'
require 'json'
reddit_api_url = "http://www.reddit.com/.json"
resp = Net::HTTP.get_response(URI.parse(reddit_api_url))
data = resp.body
result = JSON.parse(data)
[sudo] gem install json
{"kind": "Listing", "data": {"modhash": "", "children": [{"kind": "t3", "data": {"domain": "imgur.com", "media_embed": {}, "subreddit": "pics", "selftext_html": null, "selftext": "", "likes": null, "saved": false, "id": "cj6xp", "clicked": false, "author": "UnnecessaryKeanushop", "media": null, "score": 551, "over_18": false, "hidden": false, "thumbnail": "http://thumbs.reddit.com/t3_cj6xp.png", "subreddit_id": "t5_2qh0u", "downs": 518, "is_self": false, "permalink": "/r/pics/comments/cj6xp/really_really_unnecessary/", "name": "t3_cj6xp", "created": 1277555941.0, "url": "http://imgur.com/UYX4n.jpg", "title": "Really, really unnecessary", "created_utc": 1277530741.0, "num_comments": 84, "ups": 1069}}....
class User < ActiveRecord::Base
has_many :posts, options, :extend => "some_module"
end
module Associations
....
def has_many(association_id, options = {}, &extension)
reflection = create_has_many_reflection(association_id, options, &extension)
configure_dependency_for_has_many(reflection)
add_association_callbacks(reflection.name, reflection.options)
if options[:through]
collection_accessor_methods(reflection, HasManyThroughAssociation)
else
{:posts=>#<ActiveRecord::Reflection::AssociationReflection:0x1d28268 @name=:posts, @active_record=User(id: integer, name: string, created_at: datetime, updated_at: datetime), @options={:extend=>[]}, @macro=:has_many>}
#<ActiveRecord::Reflection::AssociationReflection:0x1d28268 @name=:posts, @active_record=User(id: integer, name: string, created_at: datetime, updated_at: datetime), @options={:extend=>[]}, @macro=:has_many>
mattr_accessor :valid_keys_for_has_many_association
@@valid_keys_for_has_many_association = [
:class_name, :table_name, :foreign_key, :primary_key,
:dependent,
:select, :conditions, :include, :order, :group, :having, :limit, :offset,
:as, :through, :source, :source_type,
:uniq,
:finder_sql, :counter_sql,
:before_add, :after_add, :before_remove, :after_remove,
:extend, :readonly,
def create_extension_modules(association_id, block_extension, extensions)
if block_extension
extension_module_name = "#{self.to_s.demodulize}#{association_id.to_s.camelize}AssociationExtension"
silence_warnings do
self.parent.const_set(extension_module_name, Module.new(&block_extension))
end
Array.wrap(extensions).push("#{self.parent}::#{extension_module_name}".constantize)
else
Array.wrap(extensions)
def add_association_callbacks(association_name, options)
callbacks = %w(before_add after_add before_remove after_remove)
callbacks.each do |callback_name|
full_callback_name = "#{callback_name}_for_#{association_name}"
defined_callbacks = options[callback_name.to_sym]
if options.has_key?(callback_name.to_sym)
class_inheritable_reader full_callback_name.to_sym
write_inheritable_attribute(full_callback_name.to_sym, [defined_callbacks].flatten)
else
write_inheritable_attribute(full_callback_name.to_sym, [])