Skip to content

Instantly share code, notes, and snippets.

@apneadiving
apneadiving / before.rb
Last active August 29, 2015 14:07
The before is some standard code, `with_waterfall` presents another way to write it (fully chainable)
# In controller
result = TaxCalculator.new(order).call
if result[:success]
render json: { value: result[:value] }
else
render json: { errors: result[:errors] }, status: 422
end
# The service
class TaxCalculator
Wf.new
.chain { CloseProject.new(project) }
.chain { NotifyProjectClosed(project) }
.chain { Log.new(:project_closed, project) }
.chain { render json: project }
.on_dam { |error_pool| render json: {errors: error_pool}, status: 422 }
@apneadiving
apneadiving / base.rb
Last active August 29, 2015 14:08
wf example
def accept_group_terms
if current_entity.user? && notification.group_terms?
if params[:terms_of_service][:checked]
user_group = notification.entity.user_groups.with_user(current_entity).first
if user_group
user_group.terms_accepted_at = Time.now
user_group.save
notification.mark_as_read_by(current_entity).save!
render_notif
else
@apneadiving
apneadiving / base.rb
Last active August 29, 2015 14:10
form objects
class FormObjectBase
include ::ActiveModel::Validations
attr_reader :object, :params
def initialize(object)
@object = object
end
def assign(params)
@apneadiving
apneadiving / model.rb
Last active August 29, 2015 14:13
where_values binding issue?
class Author::Work < AR::Base
scope :with_text, -> { where.not(text: nil) }
scope :started, -> { where(status: 0) }
scope :in_progress, -> { started.with_text }
end
@apneadiving
apneadiving / tracer.rb
Created August 14, 2015 14:27
trace methods
def trace_calls_on
scope = {}
trace = TracePoint.new(:call, :line) do |tp|
case tp.event
when :call then puts "#{tp.path}:#{tp.lineno} #{tp.defined_class}::#{tp.method_id} " \
"called from #{scope[:path]}:#{scope[:lineno]} #{scope[:class]}::#{scope[:method_id]}"
when :line then scope = {
event: :line,
lineno: tp.lineno,
path: tp.path,
@apneadiving
apneadiving / refinery_patch.rb
Created October 8, 2011 19:10
to use Refinery with your custom devise configuration in Rails 3.1
module RefineryPatch
def self.included(base)
base.send :helper_method,
:current_refinery_user,
:refinery_user_signed_in?,
:refinery_user? if base.respond_to? :helper_method
end
def current_refinery_user
@apneadiving
apneadiving / deploy.rb
Created October 31, 2011 17:19
basic capistrano script
require 'yaml'
CAP = YAML.load_file("./config/deploy.yml")
require "bundler/capistrano"
set :bundle_cmd, "/usr/local/rvm/gems/ree-1.8.7-2011.03/bin/bundle"
set :bundle_without, [:development, :test]
default_run_options[:pty] = true # Must be set for the password prompt from git to work
set :repository, CAP["repository"] # Your clone URL
@apneadiving
apneadiving / users_controller.rb
Created January 20, 2012 00:25
gmaps4rails: Don't show map by default and load markers with ajax
class UsersController < ApplicationController
respond_to :html, :json
def index
@json = User.all.to_gmaps4rails
respond_with @json
end
end
@apneadiving
apneadiving / console.rb
Created April 14, 2012 20:15
potential mongoid bug
--------------------------------------------------------------------------------------------
With one object saved, another in memory
--------------------------------------------------------------------------------------------
#setup
subject.copywriter_languages.create Factory.attributes_for(:copywriter_language, status: "deleted")
subject.copywriter_languages.build Factory.attributes_for(:copywriter_language, status: "pending")
#Round 1
#checking default scope
subject.copywriter_languages.criteria