These two matchers are referenced from the blog post Segment.io and Ruby.
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
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 |
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 AttachedValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
record.errors.add(attribute, :attached, options) unless value.attached? | |
end | |
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
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" |
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
# 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 |
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 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 |
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
<% # 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' %> |
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
# 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 |
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
/* 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%;} |