Skip to content

Instantly share code, notes, and snippets.

@Systho
Systho / podium.rb
Created June 12, 2015 07:10
Podium class to handle ranking, might be useful in the future
class Podium
def initialize(results)
@results = results
end
def rank_for(key)
score = results[key]
return 0 if score.nil?
index = reverse_results_hash.keys.index(score)
(0...index).to_a.sum{|prev_index| ranked_at(prev_index ).size } + 1
@Systho
Systho / application_decorator.rb
Last active March 28, 2017 09:33
Usual Draper Setup
class ApplicationDecorator < Draper::Decorator
extend Memoist
delegate_all
def method_added(method_name)
super
return if [:[], :initialize, :new].include?(method_name) # those method cannot be memoized
return if respond_to?(:object_class?) && object_class? && (method_name.to_s == object_class.name.underscore) # do not memoize object alias
@Systho
Systho / routes.rb
Last active March 28, 2017 09:31
routing concern on collection
Rails.application.routes.draw do
concern :archivable do
scope module: parent_resource.plural do
resources :archiving, only: [:create, :destroy]
end
end