Skip to content

Instantly share code, notes, and snippets.

@gma
gma / card_creator.rb
Created July 6, 2012 10:46
Moving model creation/update to a separate class
class CardCreator < Struct.new(:listener)
class Failed < RuntimeError
attr_accessor :model
def initialize(model)
@model = model
end
end
def self.create(iteration, attributes)
# The best approach depends on how you define a blog entry. If you think
# of a blog article the same way I do, that means "all pages with a date".
#
# In Ruby you can re-open a class and redefine its methods. Normally, this
# would be a horrific thing to do (as others could be very confused by
# your code) but in the context of a typical Nesta site I don't think it's
# particularly bad news, and its very convenient.
#
# So, in app.rb, add this (which is very similar to the default):
@gma
gma / gist:1876095
Created February 21, 2012 11:55
Massive ghetto related fail
11:53:07 web.1 | /Users/graham/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/sass-3.1.15/lib/sass/plugin/compiler.rb:29:in `<class:Compiler>'
11:53:07 web.1 | /Users/graham/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/sass-3.1.15/lib/sass/plugin/compiler.rb:28:in `<module:Plugin>'
11:53:07 web.1 | /Users/graham/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/sass-3.1.15/lib/sass/plugin/compiler.rb:9:in `<top (required)>'
11:53:07 web.1 | /Users/graham/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/sass-3.1.15/lib/sass/plugin.rb:4:in `<top (required)>'
11:53:07 web.1 | /Users/graham/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/haml-3.1.4/lib/sass/rails3_shim.rb:15:in `block in <top (required)>'
11:53:07 web.1 | /Users/graham/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
11:53:07 web.1 | /Users/graham/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.2.
@gma
gma / gist:1847073
Created February 16, 2012 19:08
Notes on reproducing association bug
class Card extends Serenade.Model
@localStorage = true
@property 'title', serialize: true
class CardSet extends Serenade.Model
@localStorage = true
@hasMany 'cards', as: (-> Card), constructor: Card, serializeIds: true
# Create one card, and one card set, and save them to the database.
the_set = new CardSet(id: 1)
I said "I'll send you a link..." at least three times last night.
Thought I may as well send 'em to the list...
Daniel Tenner's example videos for validating product ideas without
writing any code are linked from this post:
http://swombat.com/2012/1/27/create-your-own-video
He's also got loads of other ideas on validating things that you can
find with this search: http://swombat.com/search?q=validat
@gma
gma / gist:1476374
Created December 14, 2011 12:18
Noooooooo!
module Mongoid #:nodoc
module Components #:nodoc
extend ActiveSupport::Concern
# All modules that a +Document+ is composed of are defined in this
# module, to keep the document class from getting too cluttered.
included do
extend ActiveModel::Translation
extend Mongoid::Finders
end
@gma
gma / rupy.rb
Created December 8, 2011 19:54
#!/usr/bin/env ruby
require 'open3'
def python
Open3.popen2(%w(python -u)) do |stdin, stdout|
stdin.puts yield
stdin.close
puts stdout.read
end
#!/usr/bin/env ruby
require 'open-uri'
HOSTNAME = "http://localhost:9393"
def retrieve_all_pages
pages = []
Dir.glob('content/pages/**/*.{mdown,textile,haml}').each do |filename|
page = filename.sub(/^content\/pages\//, '')
@gma
gma / gist:1319454
Created October 27, 2011 12:45
Presenter using method_missing that has no hope of working
class Presenter < Struct.new(:object)
include ActiveModel::Serializers::JSON
class << self
def presenter_model
name.to_s.sub(/Presenter$/, "")
end
def method_missing(name, *args, &block)
if ! presenter_model.empty?
@gma
gma / gist:1261109
Created October 4, 2011 08:01
ActiveSupport gives all objects access to helper methods
## No matter how hard I try, I can't think of a good reason to do this.
$ rails console
irb(main):001:0> myobj = 1
=> 1
irb(main):002:0> myobj.send(:helper)
=> #<ActionView::Base:0x007fc22080e3c0 @assigns={}, @helpers=#<Module:0x007fc22080e370>, @_controller=nil, @_config={}, @_content_for={}, @_virtual_path=nil, @output_buffer=nil, @lookup_context=#<ActionView::LookupContext:0x007fc2204e53b0 @details_key=nil, @details={:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml], :formats=>[:html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]}, @skip_default_locale=false, @frozen_formats=false, @view_paths=[]>, @controller=nil>
irb(main):003:0> myobj.instance_variable_get(:@helper)
=> #<ActionView::Base:0x007fc22080e3c0 @assigns={}, @helpers=#<Module:0x007fc22080e370>, @_controller=nil, @_config={}, @_content_for={}, @_virtual_path=nil, @output_buffer=nil, @lookup_context=#<ActionView::LookupContext:0x007fc2204e53b0 @details_key=nil, @deta