Skip to content

Instantly share code, notes, and snippets.

View tbuehlmann's full-sized avatar

Tobias Bühlmann tbuehlmann

View GitHub Profile
class State < ActiveRecord::Base
def self.default
find_by(default: true)
end
def make_default!
PgLock.new(name: 'set_state_default').lock do
State.update_all(default: false)
update(default: true)
end
# app/drops/invoice_drop.rb
class InvoiceDrop < Liquid::Drop
delegate :days_late, :priceinbtw, :print_date, :id, :late, :unpayed, to: :invoice
attr_reader :invoice
def initialize(invoice)
@invoice = invoice
end
@tbuehlmann
tbuehlmann / login_check.rb
Last active March 6, 2016 12:46
login error message/alert
def authenticate
if params[:username].present? && params[:password].present?
inn = Inn.find_by(username: params[:username])
if inn
authenticated = inn.authenticate(params[:password])
if authenticated
redirect_to inns_url, notice: 'You are now logged in.'
return
resource :report do
resources :subjects, only: :index
end
Rails.application.routes.draw do
resources :categories
devise_for :admins
devise_for :users
resources :users
namespace :admin do
resources :categories
end
if params[:friend_id]
friend = User.find(params[:friend_id])
else
# ...
end
@tbuehlmann
tbuehlmann / url_stuff.rb
Last active September 12, 2015 07:58 — forked from anonymous/untitled
catch open uri
urls.each do |url|
begin
open(url)
# do some nokogiri things
rescue OpenURI::HTTPError
# log the error
end
end
class CreateGroupLocations < ActiveRecord::Migration
def change
create_table :group_locations do |t|
t.belongs_to :address, index: true, foreign_key: true
t.belongs_to :group, index: true, foreign_key: true
t.timestamps null: false
end
end
end
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name
t.timestamps
end
end
end
class NotePolicy < ApplicationPolicy
# For index action
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end