Skip to content

Instantly share code, notes, and snippets.

@tlux
tlux / translatable.rb
Last active December 20, 2015 20:19
Model-based Translation Concerns
module Translatable
extend ActiveSupport::Concern
included do
extend Macro
extend Migration
include Association
include Attributes
include Dirty
include Queries
@tlux
tlux / button_helper.rb
Created August 9, 2013 09:18
Bootstrap Button Helper
module ButtonHelper
def active_button_link_to(*args, &block)
extract_button_options!(args)
active_link_to *args, &block
end
def button_link_to(*args, &block)
extract_button_options!(args)
link_to *args, &block
end
@tlux
tlux / job.rb
Last active December 28, 2015 21:39
Job Callbacks
require 'active_support/concern'
module Job
extend ActiveSupport::Concern
included do
%w(before success error after failure).each do |method|
instance_eval %{
def #{method}(method_name = nil, &block)
_add_callback(:#{method}, :push, method_name, &block)
@tlux
tlux / activity.rb
Last active December 31, 2015 05:58
A simple API for form objects (using Virtus)
class Activity
if ActiveModel::VERSION::STRING =~ /^3/
extend ActiveModel::Naming
extend ActiveModel::Translation
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::MassAssignmentSecurity
else
include ActiveModel::Model
end
@tlux
tlux / example.coffee
Last active December 31, 2015 20:19
jQuery.prepare plugin to prepare a page or specific parts by executing a number of defined tasks (e.g. when loading new content to the DOM via AJAX)
# Define what to do when preparing a document or snippet
$.prepare ->
# scope all invocations to `this` (@)
$('.select2', @).select2()
# prepare the whole document
$(document).ready ->
$(document).prepare()
# prepare a snippet of AJAX-loaded content
@tlux
tlux / phantom_js.rb
Last active January 2, 2016 19:29
PhantomJS Wrapper for Ruby
# PhantomJS.configure do |config|
# config.executable_path = "#{Rails.root}/lib/phantomjs"
# end
# PhantomJS.run('my_phantom.js')
# PhantomJS.execute
class PhantomJS
include Singleton
@tlux
tlux / product_factory.rb
Last active January 2, 2016 20:19
ProductDB ProductFactory
# === EXAMPLE ===
# product_type = ProductType.first
# ProductFactory.create_or_update(product_type, properties: { custom_attribute: 'Bla' }) do |p|
# p.property :custom_attribute, 'value'
# p.price :total, 1.55
# end
class ProductFactory
def initialize(product)
@product = product
@tlux
tlux / weeks_count.rb
Last active January 23, 2023 17:19
Ruby/Rails: Calculate the number of Calendar Weeks in a Year
def weeks_count(year)
last_day = Date.new(year).end_of_year
if last_day.cweek == 1
last_day.prev_week.cweek
else
last_day.cweek
end
end
@tlux
tlux / sanitizable.rb
Last active August 29, 2015 13:56
Sanitizable Concern to sanitize ActiveRecord attributes the way you like. This implementation has been converted to a Gem, which is available here: https://github.com/chilian/acts_as_sanitizable
module Sanitizable
extend ActiveSupport::Concern
module ClassMethods
# Usage
# sanitizes :content # strips content by default
# sanitizes :content, with: :squish
# sanitizes :content, with: :presence
# Chain multiple sanitizer methods, they are executed in the order they are defined
module Archivable
extend ActiveSupport::Concern
included do
default_scope -> { where(archived_at: nil) }
scope :archived, -> { where.not(archived_at: nil) }
end
def archived?
!archived_at.nil?