Skip to content

Instantly share code, notes, and snippets.

View jcasimir's full-sized avatar

Jeff Casimir jcasimir

View GitHub Profile
class Bootstrap
def self.setup
ISerialize.impl = SerializeToCSV
end
end
class Client
def foobar
ISerialize.serialize(nil)
end
class User < ActiveRecord::Base
has_many :companies
def self.find_or_create_by_auth(auth)
raise ArgumentError.new("You mush supply parameters") unless auth
user = User.find_or_initialize_by_provider_and_uid(auth["provider"], auth["uid"])
if user.new_record?
user.name = auth["user_info"]["name"]
ActionController::Dispatcher.to_prepare do
models = Dir.glob(File.join(Rails.root, '/app/models/*.rb')).map do |file|
file.match(/(\w*).rb/)[1].split('_').map do |class_part|
class_part.capitalize
end.join('')
end
models.each do |model|
begin
decorator = (model + "Decorator").constantize

I want to implement a decoration pattern such that a Rails object has presentation-related methods encapsulated in one place.

Given a model named Article and a presentation method named to_json

Approach 1: Magic-ness

  • Create an ArticleDecorator module in /app/decorators/
  • Automatically include that module into the matching model at load time
  • Use the methods like Article.find(1).to_json or Article.find(1).formatted_title
  • Pros: Transparency
class ArticlesController < ApplicationController
expose(:article)
expose(:articles){ Article.order :created_at }
expose(:article_presenter){ @ad ||= ArticleDecorator.new(article) }
def show
end
end

Using RedCarpet from the repo I'm seeing this...

>> Redcarpet.new("```ruby\nclass Hello\nend\n```\n", :fenced_code).to_html
=> "<pre><code class=\"ruby\">class Hello\nend\n</code></pre>\n"

But I think it should be...

>> Redcarpet.new("```ruby\nclass Hello\nend\n```\n", :fenced_code).to_html
=> "<pre class=\"ruby\"><code>class Hello\nend\n</code></pre>\n"

Development and Release Strategy with Git

The Git Version Control System has taken over most of the Ruby world. Simply put, if you're going to develop with Ruby you have to learn Git.

Getting Started

A full Git tutorial is beyond scope here, but there are many great resources out there already:

@jcasimir
jcasimir / routes.rb
Created June 24, 2011 02:45
Solution for "RouterTester" Example
RouterTester::Application.routes.draw do
resources :employees do
resources :evaluations do
resources :scores
end
member do
put :promote
end
end
@jcasimir
jcasimir / gist:1045631
Created June 24, 2011 20:35
second try
$ bundle exec irb
ruby-1.9.2-p180 :001 > Bundler
=> Bundler
ruby-1.9.2-p180 :002 > Bundler.require
=> [<Bundler::Dependency type=:runtime name="rails_decorators" requirements=">= 0">, <Bundler::Dependency type=:runtime name="rspec" requirements="~> 2.0.1">, <Bundler::Dependency type=:runtime name="activesupport" requirements="~> 3.0.9">, <Bundler::Dependency type=:runtime name="actionpack" requirements="~> 3.0.9">]
ruby-1.9.2-p180 :003 > ActiveSupport
NameError: uninitialized constant Object::ActiveSupport
from (irb):3
from /Users/jcasimir/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `<main>'
ruby-1.9.2-p180 :004 > require 'activesupport'
@jcasimir
jcasimir / Gemfile
Created June 24, 2011 21:02
gemspec
source :rubygems
gem 'actionpack', "~> 3.0.9", :require => 'action_view'
gemspec