Skip to content

Instantly share code, notes, and snippets.

@delba
delba / routes.rb
Created September 20, 2013 16:38
rails module scope in routes
Antihero::Application.routes.draw do
scope module: :admin, as: :admin do
root 'dashboard#index'
resources :articles, only: [:new, :create]
end
end
# Prefix Verb URI Pattern Controller#Action
# admin_root GET / admin/dashboard#index
# admin_articles GET /articles(.:format) admin/articles#index
@delba
delba / duration.rb
Last active December 22, 2015 21:39
DateTime#strptime parse datetime
duration = '1h2min3s'
# duration = '2min3s'
# duration = '3s'
# match = duration.match(/(?<hours>.+h)?(?<minutes>.+min)?(?<seconds>.+s)?/)
# hours, minutes, seconds = [match[:hours], match[:minutes], match[:seconds]].map(&:to_i)
# 3600 * hours + 60 * minutes + seconds
match = duration.match(/
( (?<hours>[[:digit:]]+)h ){0,1}
@delba
delba / holes.html.erb
Last active August 2, 2018 07:41
ruby on rails timetable / planner
<%
min_hour, max_hour = @slots.minimum_hour, @slots.maximum_hour
datetimes = (@[email protected]_date).map do |date|
(min_hour..max_hour).map do |hour|
date.to_datetime.change(hour: hour)
end
end.flatten
@slots = @slots.group_by(&:datetime)
@delba
delba / slot.rb
Last active December 21, 2015 22:59
AR group by time or date
Slot.group('TIME(datetime)').count
# => {"06:00:00"=>8, "07:00:00"=>8, "08:00:00"=>8, "09:00:00"=>8, "10:00:00"=>8, "11:00:00"=>8, "12:00:00"=>8, "13:00:00"=>8, "14:00:00"=>8, "15:00:00"=>8, "16:00:00"=>8, "17:00:00"=>8, "18:00:00"=>8, "19:00:00"=>8}
Slot.group('DATE(datetime)').count
# => {"2013-08-29"=>14, "2013-08-30"=>14, "2013-08-31"=>14, "2013-09-01"=>14, "2013-09-02"=>14, "2013-09-03"=>14, "2013-09-04"=>14, "2013-09-05"=>14}
Slot.group('datetime::time').count
Slot.group('datetime::date').count
@delba
delba / club.rb
Last active December 21, 2015 10:59
Map
class Club < ActiveRecord::Base
def self.center
@center ||= south_west.zip(north_east).map do |a|
(a[0] + a[1]) / 2
end
end
def self.south_west
@south_west ||= [minimum(:lat), minimum(:lng)]
end
@delba
delba / projects_controller_test.rb
Last active December 20, 2015 06:59
ActionController::TestCase
# TL;DR
# verb(action, *args)
# verb(action, parameters, session, flash)
require 'test_helper'
class ProjectsControllerTest < ActionController::TestCase
setup do
@delba
delba / .vimrc
Created July 23, 2013 15:20
toggle between number and relative number in vim
set nonumber
set relativenumber
function! ToggleNumber()
if &relativenumber
set number
set norelativenumber
else
set nonumber
set relativenumber
@delba
delba / article_test.rb
Created July 22, 2013 11:29
get tested_class in rails test
require 'test_helper'
class ArticleTest < ActiveSupport::TestCase
test 'tested_class is Article' do
assert_same Article, tested_class
end
private
def tested_class
@delba
delba / application.coffee
Last active December 20, 2015 01:09
jQuery promises
City =
find: (location) ->
promise = $.Deferred()
$.ajax '/cities',
data: q: location
success: (result) ->
promise.resolve result.city
error: ->
promise.reject 'invalid location'
promise
def link_to_next(article)
target = article.next || Article.new(date: Date.today)
link_to "next", target.today? ? root_path : target
end
# do not initialize an Article
def link_to_next(article)
target = article.next
link_to "next", target.try('!today?') ? target : root_path