This file contains hidden or 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 Creator | |
extend ActiveSupport::Concern | |
included do | |
before_validation -> { | |
self.creator_id = User.current.id if User.current | |
}, on: :create | |
belongs_to :creator, class_name: 'User', foreign_key: :creator_id | |
end |
This file contains hidden or 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 ActsAsCurrent | |
extend ActiveSupport::Concern | |
included do | |
self._current_human_name = self.name.underscore.to_sym | |
end | |
def make_current | |
Thread.current[self.class._current_human_name] = self | |
end |
This file contains hidden or 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 BelongsToTenant | |
extend ActiveSupport::Concern | |
included do | |
belongs_to :tenant | |
default_scope { | |
Tenant.current ? where(tenant_id: Tenant.current.id) : where(nil) | |
} | |
end |
This file contains hidden or 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 Enumerable | |
def deep_freeze | |
obj = self | |
obj.each do |iterator| | |
case iterator | |
when Enumerable | |
iterator.deep_freeze | |
when Symbol, Fixnum, NilClass | |
iterator | |
else |
This file contains hidden or 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
# memoize db/memcache results in instance variable dynamically | |
def memoize_results(key) | |
return instance_variable_get(key) if instance_variable_defined?(key) | |
instance_variable_set key, yield | |
end | |
# usage | |
MY_CONSTANT = [:active, :inactive] | |
MY_CONSTANT.each { |key| | |
define_method("#{key}_users") do |
This file contains hidden or 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 AccountJoinCondition | |
def join_with_account(table, constraint) | |
s_r = reflection.source_reflection | |
# source reflection can be polymorphic | |
s_r = reflection if s_r && s_r.options[:polymorphic] | |
t_r = reflection.through_reflection | |
if s_r && t_r && has_account?(s_r) && has_account?(t_r) | |
sr_account_column = account_col(s_r) | |
tr_account_column = account_col(t_r) | |
constraint = constraint.and(sr_account_column.eq(tr_account_column)) if direct_through_join?(sr_account_column, tr_account_column, constraint) |
NewerOlder