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
# app/models/address.rb
class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: true
end
module Expirable
extend ActiveSupport::Concern
included do
cattr_accessor :default_lifetime
self.default_lifetime = 6.hours
scope :valid, where('expires_at >= ?', Time.now)
before_validation :set_default_expiration, :on => :create
end
module VirtualAttributes
extend ActiveSupport::Concern
class VirtualAttribute
attr_reader :name, :type
def initilaize(name, type = :string)
@name = name
@type = type
end
module Reportable
extend ActiveSupport::Concern
module ClassMethods
# Chain on a scope and specify the fields to extract.
# Example:
# User.enabled.report %{
# :email_opt_in,
# 'created_at as sign_up_date'
# }
class Ping
module Blockable
extend ActiveSupport::Concern
included do
belongs_to :blocker, class_name: 'User'
end
def blocked?
# using active_support
require 'active_support/core_ext'
module Mixpanel
extend ActiveSupport::Concern
# class methods
included do
mixpanel
end
require 'iconv'
module ExportableControllerMethods
BOM = "\377\376" #Byte Order Mark
extend ActiveSupport::Concern
protected
def export_as_csv(collection, column_names, filename)
module ActsAsHistorical
extend ActiveSupport::Concern
included do
end
module ClassMethods
def acts_as_historical(options = {})
has_many :histories, :as => :historical, :dependent => :destroy
include MyInstanceMethods
module SidekiqIndexing
module SearchableOverride
extend ActiveSupport::Concern
module InstanceOverrides
def solr_index
SunspotIndexer.perform_async(self.class.to_s, self.id)
end
end
module Eventable
module Emitter
extend ActiveSupport::Concern
included do
def emit(event_name, payload = {})
full_event = "#{self.class.to_s.underscore}:#{event_name}"
Eventable.emit full_event, self, payload
end
end