Skip to content

Instantly share code, notes, and snippets.

@blowmage
blowmage / lib-tasks-db_fixtures_dump.rake
Created December 6, 2022 11:45
Rails Rake task to dump the current environment's database into fixture files.
namespace :db do
namespace :fixtures do
desc "Dumps the current environment's database into fixtures (uses FIXTURES_PATH)"
task dump: :environment do
Rails.application.eager_load!
models = ApplicationRecord.descendants.reject { |model| model.primary_key.nil? }
fixture_files = ENV["FIXTURES"].to_s.split(",")
if fixture_files.any?
@blowmage
blowmage / minitest-assertify.rb
Created October 21, 2013 14:18
minitest-assertify
module MiniTest
module Assertify
module Lifecycle # :nodoc:
# Hook into Minitest's Lifecycle to alias methods when tests are run.
def before_setup # :nodoc:
self.class.public_instance_methods.grep(/\Arefute_/).each do |method|
new_method = method.to_s.sub("refute_", "assert_not_").to_sym
class_eval do
alias_method new_method, method
end
[vagrant@precise32:/vagrant (master)]$ echo "using search_exec"
using search_exec
[vagrant@precise32:/vagrant (master)]$ rspec spec/models/user_search_spec.rb
/usr/local/rvm/gems/ruby-1.9.3-p374/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
/vagrant/vendor/gems/message_bus/lib/message_bus.rb:130: warning: already initialized constant ENCODE_SITE_TOKEN
== Seed from /vagrant/db/fixtures/post_action_types.rb
- PostActionType {:id=>1, :name_key=>"bookmark", :is_flag=>false, :position=>1}
- PostActionType {:id=>2, :name_key=>"like", :is_flag=>false, :icon=>"heart", :position=>2}
- PostActionType {:id=>3, :name_key=>"off_topic", :is_flag=>true, :position=>3}
@blowmage
blowmage / tasks_controller_refactoring.rb
Created November 12, 2012 20:17 — forked from ryanb/tasks_controller_refactoring.rb
Variation of RubyTapas episode "021 Domain Model Events" without using callbacks
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: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