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 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 |
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 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 |
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 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 |
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 'fileutils' | |
require 'ostruct' | |
require 'pony' | |
require 'active_support' | |
module MakeEmailable | |
extend ActiveSupport::Concern | |
included do | |
attr_reader :sender, :receiver |
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 Concerns | |
module Indexable | |
class ResultList | |
include Enumerable | |
attr_accessor :results | |
attr_accessor :total | |
attr_accessor :from |
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
# 使用したいモデルでインクルード | |
# ex) include Concerns::ActivityTracker | |
module Concerns::PublicActivityTracker | |
extend ActiveSupport::Concern | |
module ClassMethods | |
# create, update, destroy を監視します。 | |
# 引数に only か except を渡すことで update を監視したいカラムを限定できます。 | |
# |
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 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 |
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 'time' | |
module EasterCalculations | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def easter(year = nil) | |
y = year ||= now.year | |
c = y / 100 | |
n = y - 19 * ( y / 19 ) |
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
# 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 |
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 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( |