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
require 'active_support/concern'
require 'csv'
module QueryToCSVConcern
extend ActiveSupport::Concern
# Dump ActiveRecord::Relation to a csv file.
# Data will always order ascending based on first order parameter given
# Ignores order and limit options like find_each
#
module Concerns::Titleizable
extend ActiveSupport::Concern
included do
before_validation :to_titlecase
before_validation :to_downcase
class << self
attr_accessor :to_titleize
attr_accessor :to_be_downcased
class Expert < ActiveRecord::Base
include Notifiable
has_many :trackings do
def recent
where('created_at >= ?', proxy_association.owner.notified_at)
end
end
end
# With thanks to https://github.com/optoro/activerecord-callback_notification for original inspiration
if Rails.env.development?
module ActiveRecord
module CallbackNotifications
extend ActiveSupport::Concern
module ClassMethods
def notify_all_callbacks
# Make sure all models are loaded before trying to enumerate
class Activity < ActiveRecord::Base
# columns:
# * entity_id
# * entity_type
# * action
# * created_at
belongs_to :entity, :polymorphic => true
end
require 'active_support/concern'
module Rankable
extend ActiveSupport::Concern
included do
validates :row_order, :presence => true
scope :next_rank, lambda { |rank| where('row_order > ?',rank).order("row_order asc").limit(1)}
scope :previous_rank, lambda { |rank| where('row_order < ?',rank).order("row_order desc").limit(1)}
scope :bigger_rank, order("row_order desc")
before_validation :assign_default_rank
@kivanio
kivanio / nyan.rb
Created June 23, 2014 00:39 — forked from rummelonp/nyan.rb
class ActiveRecord::Base
def self.indexes
connection.indexes(table_name)
end
def self.indexes_by_column(column_name)
indexes.select { |i| i.columns.include?(column_name.to_s) }
end
end
class Yokkyu < ActiveRecord::Base
extend Enumerize
enumerize :status, scope: true, default: :awaiting in: {
awaiting: 1,
approved: 2,
rejected: 3,
}
state_machine :status do
module HasBulkActions
extend ActiveSupport::Concern
module ClassMethods
def bulk_update(ids, attributes, params = {})
where("#{table_name}.id" => ids).update_all(attributes)
extract_bulk_events(params[:events], attributes).each do |event|
notify(event.to_sym)
end
end
module UniquelyIdentifiable
extend ActiveSupport::Concern
FINAL_BASE = 36
INTERMEDIATE_BASE = 16
SEPARATOR = 'a'
def generate_uid
real_id = self.id.to_s
rl = (6 - real_id.length) / 2