This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Expert < ActiveRecord::Base | |
include Notifiable | |
has_many :trackings do | |
def recent | |
where('created_at >= ?', proxy_association.owner.notified_at) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Activity < ActiveRecord::Base | |
# columns: | |
# * entity_id | |
# * entity_type | |
# * action | |
# * created_at | |
belongs_to :entity, :polymorphic => true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Yokkyu < ActiveRecord::Base | |
extend Enumerize | |
enumerize :status, scope: true, default: :awaiting in: { | |
awaiting: 1, | |
approved: 2, | |
rejected: 3, | |
} | |
state_machine :status do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |