Skip to content

Instantly share code, notes, and snippets.

@senny
senny / gist:1579301
Created January 8, 2012 18:54
example virtus integration spec
require 'integration/spec_helper'
class Article
include Virtus
attribute :title, String
def title
super || '<unknown>'
end
@senny
senny / gist:1590866
Created January 10, 2012 20:02
const_missing and Class.new
class ThisWorks
def self.const_missing(*args)
p "yay!"
end
NonExistingConstant
end
this_does_not_work = Class.new do
def self.const_missing(*args)
@senny
senny / basic_decorator.rb
Created October 2, 2012 12:05
Simple Decorator implementation for our projects
require 'delegate'
class BasicDecorator < SimpleDelegator
def self.decorate(objects)
case objects
when Array
objects.map { |object| new(object) }
when ActiveRecord::Relation, DecoratedListProxy, ActiveRecord::Associations::CollectionProxy
DecoratedListProxy.new(self, objects)
else
@senny
senny / github_branch_clenaup.rb
Created October 17, 2012 13:42
Github Branch cleanup
#!/usr/bin/env ruby
current_branch = `git symbolic-ref --short HEAD`.chomp
if current_branch != "master"
if $?.exitstatus == 0
puts "WARNING: You are on branch #{current_branch}, NOT master."
else
puts "WARNING: You are not on a branch"
end
puts
@senny
senny / output (PostgreSQL)
Created October 27, 2012 15:04
Performance of ActiveRecord-Setters
user system total real
variable 1.590000 0.000000 1.590000 ( 1.599274)
AR 69.490000 2.440000 71.930000 ( 71.920999)
AR is 44.97 times slower.
@senny
senny / full
Created November 20, 2012 09:58
Rails Plugin Generator
railties :: (refactor_plugin_new_generator) » ./bin/rails plugin new ~/Projects/playground/rails/full_engine --full
create
create README.rdoc
create Rakefile
create full_engine.gemspec
create MIT-LICENSE
create .gitignore
create Gemfile
create app/models
create app/models/.keep
class User < ActiveRecord::Base
validates :account_id, :presence => true
end
# in the View (using simple_form):
#
# f.select :account, :collection => Account.all
@senny
senny / _form.html.erb
Created December 31, 2012 14:45
Simple Example how to use `accepts_nested_attributes_for`
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
diff --git a/lib/spring.rb b/lib/spring.rb
index 38a22bf..20669c6 100644
--- a/lib/spring.rb
+++ b/lib/spring.rb
@@ -50,6 +50,7 @@ class Spring
end
def run(args)
+ puts "#run"
boot_server unless server_running?
@senny
senny / gist:4446037
Last active December 10, 2015 14:08
class Article < ActiveRecord::Base
end
module Posts
end
Posts::Builder # => uninitialized constant Posts::Builder
Article::Builder # => ActiveRecord::Associations::Builder
Post.const_get('Builder') # => ActiveRecord::Associations::Builder