Skip to content

Instantly share code, notes, and snippets.

@blowmage
blowmage / tasks_controller_refactoring.rb
Created November 12, 2012 16:52 — forked from ryanb/tasks_controller_refactoring.rb
Variation of RubyTapas episode "021 Domain Model Events" without callbacks, using dirty, and keeping the tracker object.
class TasksController < ApplicationController
def update
if @task.update_attributes(params[:task])
tracker = PostSaveTaskTracker.new(@task)
TaskPusher.new(tracker, socket_id).push_changes
TaskMailSender.new(tracker, current_user).deliver_email
# success response
else
# failure respond
end
@blowmage
blowmage / tasks_controller_refactoring.rb
Created November 12, 2012 16:13 — forked from ryanb/tasks_controller_refactoring.rb
Variation of RubyTapas episode "021 Domain Model Events" without using callbacks or reimplementing dirty tracking.
class TasksController < ApplicationController
def update
if @task.update_attributes(params[:task])
TaskPusher.new(@task, socket_id).push_changes
TaskMailSender.new(@task, current_user).deliver_email
# success response
else
# failure respond
end
end
@blowmage
blowmage / _readme.md
Created November 6, 2012 16:54
Presenters in Rails, using modules

Presenters in Rails

I have bemoaned the lack of a ViewModel in Rails many times, and I prefer using Presenters to simulate a ViewModel. But it turns out there is an object that does represent the state of the view in Rails apps. Its an instance of ActionView::Base that the controller calls view_context. We don't have much control of this object, but it seems like a logical place to put our view-specific behavior.

This code is an attempt at creating Presenters using modules. The module will be mixed into the view_context object. This is very similar to how a Decorator module will be mixed into a model object, only instead of being specific to the model is it specific to the view.

This means that testing your presenter is no different than testing any other module. So relying on dependencies such as other methods or instance variables can make testing difficult.

@blowmage
blowmage / History.txt
Created September 26, 2012 03:09
Stoopid example of MiniTest Shouldify
=== 1.0.0 / 2012-09-25
* 1 major enhancement
* Birthday!
@blowmage
blowmage / foo.rb
Created August 3, 2012 11:42
Shouldify MiniTest
class Foo
def bar
"bar"
end
def baz
"baz"
end
end
@blowmage
blowmage / changes.rake
Created June 5, 2012 18:21
Rake tasks for generating changes made by author
# Taken from http://stackoverflow.com/a/9782550
namespace :git do
desc "Analytics of changes made by author"
task :changes do
git_changes_by_author.sort_by { |a,h| -h[:total] }.each do |author, changes|
puts "#{author}: #{changes[:total]} (#{changes[:insertions]} insertions, #{changes[:deletions]} deletions)"
end
end
end
@blowmage
blowmage / lambda.rb
Created December 14, 2011 02:48
Blocks, Procs and Lambda - Oh My!
def test_lambda
puts "About to call lambda"
blck = lambda do
puts " -- Start lambda"
return "lambda"
puts " -- End lambda"
end
val = blck.call
puts "The value is #{val}"
puts "Just called lambda"
@blowmage
blowmage / Rakefile
Created August 23, 2011 03:18
Flog Example
# Note we are requiring Flog before any other rake task
# This fails with "uninitialized constant Rake::TaskLib"
require "flog_task"
FlogTask.new do |t|
t.verbose = true
end
# If we require this before flog_task then it will load fine
require "rake/testtask"
Rake::TestTask.new do |t|
@blowmage
blowmage / edgecase.rb
Created July 27, 2011 02:08
Ruby Koans Hackfest
#!/usr/bin/env ruby
# -*- ruby -*-
require 'test/unit/assertions'
class Object
def method_missing *args
true
end
end
@blowmage
blowmage / list.rb
Created July 14, 2011 15:12
Testing Primer (URUG presentation 7/12/2011)
class List < ActiveRecord::Base
belongs_to :user
validates_presence_of :name
validates_presence_of :user
end