Skip to content

Instantly share code, notes, and snippets.

@johncox00
johncox00 / offer_rule.rb
Created December 26, 2016 12:30
This is code underlying a system for driving offer/discount qualification in an ecom system. Details here: https://docs.google.com/presentation/d/1beY49QzNJuwD_m_fJLdEo_MxWXCzcMQCHT-I968VIII/edit?usp=sharing
class OfferRule
def initialize(user, field_or_method, field_type, threshold_value)
@user = user
@field_or_method = field_or_method.to_sym
@field_type = field_type
@threshold_value = threshold_value
end
def self.create_from_hash(user, rule_hash)
@johncox00
johncox00 / application.rb
Last active December 26, 2016 13:01
Forensic logging...This will log just about EVERYTHING in any class in which is included. In a blocking/synchronous framework it will negatively impact performance. Do not use in production unless you have a big problem to diagnose and can't figure out a better way. Even then, don't leave it on for long. There is an async option that uses Resque…
class Application < Rails::Application
#...
config.after_initialize do
method_logger_config = YAML::load(ERB.new(File.read(File.join(Rails.root, 'config/constants/method_logger.yaml'))).result)[Rails.env]
if Rails.env != "test"
Rails.application.eager_load!
if Rails.logger.level <= "Logger::Severity::#{method_logger_config["controller_logging_threshold"].upcase}".constantize
ApplicationController.subclasses.each{|c| c.send(:include, MethodLogger) unless method_logger_config["excluded_controllers"].include?(c.to_s)}
end
if Rails.logger.level <= "Logger::Severity::#{method_logger_config["model_logging_threshold"].upcase}".constantize
@johncox00
johncox00 / filtered_ability.rb
Last active December 29, 2016 12:23
What if we could load only the abilities that we want for Cancan? (This is a rough sketch of the concept.)
class FilteredAbility
def initialize(user)
@user = user
end
def ability_for(obj)
class_abilities = organized_abilities[obj.class]
considers = class_abilities.keys.select{|a| @user.send(a)}
considers.each{|c| create_ability(class_abilities[c])
end