Skip to content

Instantly share code, notes, and snippets.

Avatar

Yuval Kordov uberllama

View GitHub Profile
View gist:9e69529b0d835902f33d
class PostQuery
attr_accessor :relation
ALLOWED_SORT_ATTRS = ["title", "updated_at"]
# @example PostQuery.new.relation.recent.sorted("title", "DESC")
# @example PostQuery.new(user.posts).relation.recent.sorted("title", "DESC")
#
def initialize(relation = nil)
@uberllama
uberllama / gist:7226f8e0ea3800a1f517
Created Feb 8, 2015
react-router dynamic segment bug with forms
View gist:7226f8e0ea3800a1f517
<!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>
@uberllama
uberllama / gist:a9e17127cdec1b08a2b4
Last active Aug 29, 2015
Conditional loading of components
View gist:a9e17127cdec1b08a2b4
# 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)
View gist:487b6f94c615ae75e636
// WORKING VERSION
var Table = React.createClass({
getDefaultProps: function() {
return {
columns: [
{ propName: 'id', name: 'ID' },
{ propName: 'name', name: 'Name' }
]
};
},
View gist:8f1f24048f8052ee6725
var PostExpanded = React.createClass({
propTypes: {
post: React.PropTypes.object,
comments: React.PropTypes.array,
users: React.PropTypes.array
},
render: function() {
return (
<div>
@uberllama
uberllama / gist:1d0323438700d4f23bbb
Last active Aug 29, 2015
Erroneous validation on has_many :through with existing record
View gist:1d0323438700d4f23bbb
# 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.
View gist:7505774
# 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:7505705
Last active Dec 28, 2015
Observer example 2
View gist:7505705
# 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
@uberllama
uberllama / gist:7505406
Last active Dec 28, 2015
Observer example 1
View gist:7505406
# 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
View gist:7505333
# 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
You can’t perform that action at this time.