Skip to content

Instantly share code, notes, and snippets.

@carlosramireziii
carlosramireziii / analytics_cookies.html.slim
Created January 3, 2012 20:59
Adding a cookie for Google Analytics to filter internal traffic in a Rails application
doctype 5
html
head
title Internal Traffic Cookie
= csrf_meta_tags
meta[name='robots' content='noindex']
body onLoad="javascript:_gaq.push(['_setVar', 'do_not_track_internal_traffic']);"
= yield
@carlosramireziii
carlosramireziii / html5-reset-sprite-issue.css
Created February 7, 2012 23:23
html5-reset sprite issue
/* Responsive images and other embedded objects
Note: keeping IMG here will cause problems if you're using foreground images as sprites.
If this default setting for images is causing issues, you might want to replace it with a .responsive class instead. */
img,
object,
embed {max-width: 100%;}
@carlosramireziii
carlosramireziii / pow_setup
Created February 13, 2012 15:16
Shows the steps to use 37signals's Pow rack server with the Powder gem
# Install the powder gem
gem install powder
# Use the gem to install Pow itself
powder install
# Add a project to Pow
cd <app-directory>
powder link
@carlosramireziii
carlosramireziii / README.md
Created March 7, 2016 17:33 — forked from joshuaclayton/README.md
RSpec custom matchers for Segment.io tracking
@carlosramireziii
carlosramireziii / admin.html.erb
Last active May 19, 2021 17:53
"Best Practices For Building A Rails Admin Interface From Scratch" sample setup
<% # app/views/layouts/admin.html.erb %>
<!DOCTYPE html>
<html>
<head>
<title>Admin Interface</title>
<%= csrf_meta_tags %>
<% # Optionally use admin-specific assets here instead of the normal application assets %>
<%= stylesheet_link_tag 'admin', media: 'all', 'data-turbolinks-track': 'reload' %>
@carlosramireziii
carlosramireziii / person.rb
Created September 27, 2017 01:13
Illustrating the need for using "self" when using a setter method within an instance
class Person
attr_accessor :name
def rename(new_name)
# creates a new local variable called `name` and sets its value to the variable `new_name`
name = new_name
# calls the `name=` method of the instance
self.name = new_name #
end
@carlosramireziii
carlosramireziii / main.rb
Last active September 30, 2017 14:49
Maintaining different formats of an attribute for an ActiveRecord model
# db/schema
create_table "product_versions", force: true do |t|
t.string "string"
t.text "changelog_as_markdown"
t.text "changelog_as_html" # OPTIONAL - used for caching in Options 2 & 3 below
end
# Option 1: calculate HTML version of the changelog on-the-fly
class ProductVersion < ActiveRecord::Base
def changelog_as_html
@carlosramireziii
carlosramireziii / clean_post.rb
Last active April 12, 2018 00:59
An example of using the Decorator pattern for filtering out profanity
class CleanPost < SimpleDelegator
def title
# calling `super` here will return the value of the #title from the original object
Profanity.filter(super)
end
end
# usage
unclean_post = Post.new(title: "foo BAD WORD bar")
unclean_post.title # => "foo BAD WORD bar"
@carlosramireziii
carlosramireziii / attached_validator.rb
Last active April 10, 2024 11:11
A validator and RSpec matcher for requiring an attachment using Active Storage
class AttachedValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors.add(attribute, :attached, options) unless value.attached?
end
end
@carlosramireziii
carlosramireziii / allow_content_type.rb
Last active September 22, 2023 21:39
A validator and RSpec matcher for restricting an attachment’s content type using Active Storage
require "rspec/expectations"
RSpec::Matchers.define :allow_content_type do |*content_types|
match do |record|
matcher.matches?(record, content_types)
end
chain :for do |attr_name|
matcher.for(attr_name)
end