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
str.gsub(/[\\\`\*\_\{\}\[\]\<\>\(\)\#\+\-\.\!\|]/) { |c| "\\#{c}" } |
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
# abuse capistrano variable validators to | |
# use stage configs with the non-lazy #append | |
# this bit sets a global config before the stage is loaded | |
append :linked_files, 'config/database.yml', 'config/other.yml' | |
# this bit will run after the variable you need is set | |
validate :rails_env do |_, value| | |
append :linked_files, "config/credentials/#{value}.key" | |
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
system('clear') | |
begin | |
runs, score = 0, 0 | |
3.times do | |
add, (i1, i2) = rand(2).zero?, [rand(750), rand(250)].sort | |
sym, answer = if add | |
['+', (i1 + i2)] | |
else | |
['-', (i2 - i1)] |
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 Searchable | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def searchable(*args) | |
class_attribute :searchable_attributes | |
self.searchable_attributes = args | |
end | |
def search(search_string, limit=100) |
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
class ChangeValidator < ActiveModel::EachValidator | |
# Enforce/prevent attribute change | |
# | |
# Example: Make attribute immutable once saved | |
# validates :attribute, change: false, on: :update | |
# | |
# Example: Force attribute change on every save | |
# validates :attribute, change: true | |
def initialize(options) |
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
class ActionController::Base | |
protected | |
def do_using_temp_locale(temp_locale, &block) | |
if I18n.available_locales.include?(temp_locale.to_sym) | |
original_locale = I18n.locale | |
I18n.locale = temp_locale | |
return_value = yield | |
I18n.locale = original_locale | |
else | |
return_value = yield |
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
class ActiveRecord::Base | |
class << self | |
def has_simple_search(*attrs) | |
raise 'has_simple_search expects at least one attribute' if attrs.empty? | |
instance_eval do # because this is ActiveRecord::Base, the class inherits this | |
write_inheritable_attribute(:simple_search_fields, attrs.flatten) | |
def simple_search(search_string) | |
attrs = read_inheritable_attribute(:simple_search_fields) | |
like_s = attrs.map{|f| "#{self.table_name}.#{f.to_s} REGEXP ?"}.join(' OR ') | |
terms = search_string.split(' ').map{|s| Regexp.escape(s.strip)} |