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,
require 'spec_helper'
describe 'thread safe tenants' do
let(:db_names) { [db1, db2] }
before do
Apartment.configure do |config|
config.excluded_models = ["Company"]
config.tenant_names = lambda{ Company.pluck(:database) }
config.use_schemas = false
@apneadiving
apneadiving / 00-setup.sql
Created October 16, 2015 07:20 — forked from michaeldelorenzo/00-setup.sql
Tree structure query with PostgreSQL
-- Create tables
CREATE TABLE groups (id serial NOT NULL, name character varying NOT NULL);
CREATE TABLE network_group_members (pid integer, cid integer NOT NULL);
-- Insert the groups
INSERT INTO groups (name) VALUES ('GROUP 1'),('GROUP 2'),('GROUP 3'),('GROUP 4'),('GROUP 5'),('GROUP 6'),('GROUP 7'),('GROUP 8'),('GROUP 9'),('GROUP 10'),('GROUP 11'),('GROUP 12'),('GROUP 13');
-- Build the "Network"
INSERT INTO network_group_members(pid,cid) VALUES (1,2),(1,3),(1,4),(2,5),(5,6),(5,7),(7,8),(3,9),(4,10),(4,11),(11,12),(12,13);
@apneadiving
apneadiving / migration.rb
Last active May 1, 2020 09:50
incremental invoice numbers without gap in rails + postgresql
class CreateInvoiceNumbers < ActiveRecord::Migration
def up
create_table :invoice_numbers do |t|
t.integer :year, null: false, unique: true
t.integer :next_number_within_year, null: false, default: 1
end
add_index :invoice_numbers, :year, unique: true
(2016..2045).each do |year|
require 'rubygems'
require 'monadic'
class MonadicService
def initialize(user_id)
@user_id = user_id
end
def call
response = HTTParty.get("https://jsonplaceholder.typicode.com/users/#{@user_id}")