Skip to content

Instantly share code, notes, and snippets.

@uberllama
uberllama / gist:6977242
Last active December 25, 2015 12:29
Modified DelayedJob workflow to use the Document's ID rather than serializing the entire Document object.
class Document < ActiveRecord::Base
# Environment-specific direct upload url verifier screens for malicious posted upload locations.
DIRECT_UPLOAD_URL_FORMAT = %r{\Ahttps:\/\/s3\.amazonaws\.com\/myapp#{!Rails.env.production? ? "\\-#{Rails.env}" : ''}\/(?<path>uploads\/.+\/(?<filename>.+))\z}.freeze
belongs_to :user
has_attached_file :upload
validates :direct_upload_url, presence: true, format: { with: DIRECT_UPLOAD_URL_FORMAT }
# app/models/user.rb
class User < ActiveRecord::Base
include UserObserver
has_many :galleries
end
# /app/models/concerns/user_observer.rb
module UserObserver
extend ActiveSupport::Concern
@uberllama
uberllama / gist:7505406
Last active December 28, 2015 13:09
Observer example 1
# config/application.rb
config.autoload_paths << "#{config.root}/app/models/observers"
config.active_record.observers = :user_observer
# app/models/user.rb
class User < ActiveRecord::Base
has_many :galleries
end
# app/models/observers/user_observer.rb
@uberllama
uberllama / gist:7505705
Last active December 28, 2015 13:09
Observer example 2
# config/application.rb
config.autoload_paths << "#{config.root}/app/models/observers"
config.active_record.observers = :trackable_observer
# app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
has_many :activities, as: :trackable
end
# app/models/comment.rb
class Comment < ActiveRecord::Base
include TrackableObserver
belongs_to :user
has_many :activities, as: :trackable
end
# app/models/like.rb
class Like < ActiveRecord::Base
@uberllama
uberllama / gist:1d0323438700d4f23bbb
Last active August 29, 2015 14:09
Erroneous validation on has_many :through with existing record
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.1.7'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
@uberllama
uberllama / gist:8f1f24048f8052ee6725
Created January 11, 2015 21:57
Nested React components
var PostExpanded = React.createClass({
propTypes: {
post: React.PropTypes.object,
comments: React.PropTypes.array,
users: React.PropTypes.array
},
render: function() {
return (
<div>
// WORKING VERSION
var Table = React.createClass({
getDefaultProps: function() {
return {
columns: [
{ propName: 'id', name: 'ID' },
{ propName: 'name', name: 'Name' }
]
};
},
@uberllama
uberllama / gist:a9e17127cdec1b08a2b4
Last active August 29, 2015 14:14
Conditional loading of components
# posts/show.html.erb
<%= react_component("ShowPost", render(template: 'posts/show.json.jbuilder')) %>
# posts/show.json.jbuilder
json.post do
json.extract!(@post, :id, :user_id, :title, :body)
end
json.comments(@comments) do |comment|
json.extract!(comment, :id, :user_id, :body)
@uberllama
uberllama / gist:7226f8e0ea3800a1f517
Created February 8, 2015 22:32
react-router dynamic segment bug with forms
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dynamic Segment Bug</title>
</head>
<body>
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.min.js"></script>