Skip to content

Instantly share code, notes, and snippets.


Carlos Ramirez III carlosramireziii

View GitHub Profile
carlosramireziii / allow_content_type.rb
Last active Oct 31, 2021
A validator and RSpec matcher for restricting an attachment’s content type using Active Storage
View allow_content_type.rb
require "rspec/expectations"
RSpec::Matchers.define :allow_content_type do |*content_types|
match do |record|
matcher.matches?(record, content_types)
chain :for do |attr_name|
carlosramireziii / attached_validator.rb
Last active Dec 21, 2020
A validator and RSpec matcher for requiring an attachment using Active Storage
View attached_validator.rb
class AttachedValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors.add(attribute, :attached, options) unless value.attached?
carlosramireziii / clean_post.rb
Last active Apr 12, 2018
An example of using the Decorator pattern for filtering out profanity
View clean_post.rb
class CleanPost < SimpleDelegator
def title
# calling `super` here will return the value of the #title from the original object
# usage
unclean_post = "foo BAD WORD bar")
unclean_post.title # => "foo BAD WORD bar"
carlosramireziii / main.rb
Last active Sep 30, 2017
Maintaining different formats of an attribute for an ActiveRecord model
View main.rb
# 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
# Option 1: calculate HTML version of the changelog on-the-fly
class ProductVersion < ActiveRecord::Base
def changelog_as_html
carlosramireziii / person.rb
Created Sep 27, 2017
Illustrating the need for using "self" when using a setter method within an instance
View person.rb
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 = new_name #
carlosramireziii / admin.html.erb
Last active May 19, 2021
"Best Practices For Building A Rails Admin Interface From Scratch" sample setup
View admin.html.erb
<% # app/views/layouts/admin.html.erb %>
<!DOCTYPE html>
<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 /
Created Mar 7, 2016 — forked from joshuaclayton/
RSpec custom matchers for tracking
carlosramireziii / pow_setup
Created Feb 13, 2012
Shows the steps to use 37signals's Pow rack server with the Powder gem
View pow_setup
# 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
View html5-reset-sprite-issue.css
/* 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. */
embed {max-width: 100%;}
carlosramireziii / analytics_cookies.html.slim
Created Jan 3, 2012
Adding a cookie for Google Analytics to filter internal traffic in a Rails application
View analytics_cookies.html.slim
doctype 5
title Internal Traffic Cookie
= csrf_meta_tags
meta[name='robots' content='noindex']
body onLoad="javascript:_gaq.push(['_setVar', 'do_not_track_internal_traffic']);"
= yield