Skip to content

Instantly share code, notes, and snippets.

View kivanio's full-sized avatar
🏠
Working from home

Kivanio Barbosa kivanio

🏠
Working from home
View GitHub Profile
module RangeScopes
extend ActiveSupport::Concern
module ClassMethods
# Order.between(1..5) => pulls all orders who's ids are between 1 and 4
# Order.between(:customer_id, 1..4) => pulls all orders who's orders.customer_id is between 1 and 4
def between(*args)
column = args.first.is_a?(Symbol) ? args.shift : :id
range = args.first
module Accessible
extend ActiveSupport::Concern
included do
has_many :accessibilities, as: :accessible, dependent: :destroy
has_many :roles, -> { uniq }, through: :accessibilities
scope :visible_to, ->(role_names) {
role_names = [role_names] if [String, Symbol].include?(role_names.class)
if role_names.include?(Role::ROOT_ROLE)
all
module Avatarable
extend ActiveSupport::Concern
GRAVATAR_IMAGE_BASE_URL = 'http://www.gravatar.com/avatar/'
GRAVATAR_IMAGE_DEFAULT_SIZE = '32'
DEFAULT_URL = 'http://your-awesome-domain.com/images/your-awesome-default-image.png'
# Avatarable assumes the class (or other module) that includes this module has an email attribute
# if the email attribute is named something other than email, use alias_attribute to map it to email
# alias_attribute :email, :your_email_attribute
require 'fileutils'
require 'ostruct'
require 'pony'
require 'active_support'
module MakeEmailable
extend ActiveSupport::Concern
included do
attr_reader :sender, :receiver
require 'active_support/concern'
module Concerns
module Indexable
class ResultList
include Enumerable
attr_accessor :results
attr_accessor :total
attr_accessor :from
# 使用したいモデルでインクルード
# ex) include Concerns::ActivityTracker
module Concerns::PublicActivityTracker
extend ActiveSupport::Concern
module ClassMethods
# create, update, destroy を監視します。
# 引数に only か except を渡すことで update を監視したいカラムを限定できます。
#
class ExternalService
include RateLimiter
def self.work_with_users
rate_limit 20, 2.0, User.find_each do |user|
# Do stuff with each user.
# Pause 2 seconds every 20 iterations
end
end
end
require 'time'
module EasterCalculations
extend ActiveSupport::Concern
module ClassMethods
def easter(year = nil)
y = year ||= now.year
c = y / 100
n = y - 19 * ( y / 19 )
# VAlidate Part Of Resource
module Vapor
extend ActiveSupport::Concern
included do
class_attribute :target_name, :attribute_names
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::Naming
end
class ClassificationQuery
Tag = ActsAsTaggableOn::Tag
Tagging = ActsAsTaggableOn::Tagging
def initialize(bill, tagger = nil)
@bill, @tagger = bill, tagger
end
def count
relation = Tag.select(Tag[:name], Arel.star.count).where(