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 OneConnectionBeforeConfirm
extend ActiveSupport::Concern
included do
before_create :generate_confirmation_token
after_create :send_on_create_confirmation_instructions
end
# This actions are not properly trigerred because of the rest of our modifications.
before_create :generate_confirmation_token
module Concerns
module CreatedAtScopes
extend ActiveSupport::Concern
included do
%w(created_at updated_at).each do |prop|
verb = prop.gsub('_at', '')
scope "#{verb}_last_h", lambda { where("#{prop} >= ?", (Time.now-1.hour).to_s(:db)) }
scope "#{verb}_last_6h", lambda { where("#{prop} >= ?", (Time.now-6.hour).to_s(:db)) }
module ActiveRecord
module PgJsonStore
extend ActiveSupport::Concern
module ClassMethods
#
# Store (JSON) using Postgres
# JSON data type.
#
module SamyRoad
module FeedsEntryStatus
extend ActiveSupport::Concern
STATES = {
ignored: 1,
pending: 0,
published: 2,
erased: 3
}
@kivanio
kivanio / states.rb
Created June 22, 2014 21:57 — forked from Porta/states.rb
module States
extend ActiveSupport::Concern
included do
state_machine :status, :initial => :new do
event :confirm do
transition :new => :confirmed #, :if => lambda {|order| order.email.present?}
end
# /app/models/item.rb
class Item < ActiveRecord::Base
include ItemSearch
...
end
# /app/models/concerns/item_search.rb
module ItemSearch
extend ActiveSupport::Concern
module RenderError
extend ActiveSupport::Concern
included do
rescue_from HTTPError do |e|
render_error e.status_code, e.message
end
end
class HTTPError < StandardError
require 'active_support/concern'
module ActiveModel
module DatetimeAttributes
extend ActiveSupport::Concern
included do
def self.attr_datetime(datetime_attribute, options = {})
time_attr = options[:time_attr] || :"#{datetime_attribute}_time"
date_attr = options[:date_attr] || :"#{datetime_attribute}_date"
# app/models/item.rb
require 'concerns/sortable'
class Item < ActiveRecord::Base
include Sortable
attr_accessible :title
end
# app/models/concerns/followable.rb
module Followable
extend ActiveSupport::Concern
included do
has_many :reverse_relationships, :class_name => 'Relationship',
:as => :following,
:dependent => :destroy
has_many :followers, :through => :reverse_relationships,