Skip to content

Instantly share code, notes, and snippets.

# app/models/concerns/multiparameter_attribute_assignment.rb
module MultiparameterAttributeAssignment
include ActiveModel::ForbiddenAttributesProtection
def initialize(params = {})
assign_attributes(params)
end
def assign_attributes(new_attributes)
# A list of possible usernames to reserve to avoid
# vanity URL collision with resource paths
# It is a merged list of the recommendations from this Quora discussion:
# http://www.quora.com/How-do-sites-prevent-vanity-URLs-from-colliding-with-future-features
# Country TLDs found here:
# http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains#Country_code_top-level_domains
# Languages found here:
@yctay
yctay / hash.rb
Last active January 1, 2016 16:49
Extension of the `key?` method to check for existence of multiple keys in a hash.
class Hash
# Extension of the `key?` method to check for existence of multiple keys in
# a hash.
#
# Returns true if all keys matches, false if hash has missing keys.
# Returns true if hash has more keys than specified in array.
def keys?(array)
!array.map { |k| self.key?(k) }.include? false
end
end
@yctay
yctay / app.rake
Created November 28, 2013 18:07 — forked from ChuckJHardy/app.rake
------------ From Rake Task
namespace :app do
# Checks and ensures task is not run in production.
task :ensure_development_environment => :environment do
if Rails.env.production?
raise "\nI'm sorry, I can't do that.\n(You're asking me to drop your production database.)"
end
end
@yctay
yctay / seeds.rb
Created November 28, 2013 15:27
Country seed data using data from the 'countries' gem. Table columns: - name - continent - alpha2_code - currency_code - calling_code Adapt for your own use!
# Countries
# `alpha2_code` has unique constraint
countries_seed_file = File.join(Rails.root, 'db', 'seeds', 'countries.yml')
countries = YAML.load_file(countries_seed_file)
countries.each do |country|
country = country[-1]
data = {
name: country['name'],
continent: country['continent'],
@yctay
yctay / nginx.conf
Created June 20, 2013 03:14
Example nginx/puma conf file.
upstream example_name {
server unix:///var/www/example_name/shared/sockets/puma.sock;
}
server {
listen 80;
server_name example_name.com;
root /var/www/example_name/current/public;
location / {
@yctay
yctay / figaro-capistrano.rb
Created April 30, 2013 05:34
Capistrano recipe for deploying figaro's application.yml
namespace :figaro do
desc "SCP transfer figaro configuration to the shared folder"
task :setup do
transfer :up, "config/application.yml", "#{shared_path}/application.yml", :via => :scp
end
desc "Symlink application.yml to the release path"
task :finalize do
run "ln -sf #{shared_path}/application.yml #{release_path}/config/application.yml"
end
@yctay
yctay / knife.rb
Created April 29, 2013 10:22 — forked from jtimberman/knife.rb
# Knife Configuration File.
#
# This is a Ruby DSL to set configuration parameters for Knife's
# general options. The default location for this file is
# ~/.chef/knife.rb. If multiple Chef repositories are used,
# per-repository configuration files can be created. A per repository
# configuration file must be .chef/knife.rb in the base directory of
# the Chef repository. For example,
#
# ~/Development/chef-repo/.chef/knife.rb
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
@yctay
yctay / deploy.rb
Last active August 31, 2017 13:43
Capistrano recipe for deploying to WebFaction
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :user, "<< YOUR WEBFACTION USERNAME >>"
set :application, "<< YOUR APP NAME >>"
set :repository, "<< YOUR REPOSITORY ADDRESS >>"
set :deploy_to, "/home/<< YOUR WEBFACTION USERNAME >>/webapps/<< YOUR WEBAPP DIRECTORY >>"
set :default_stage, "production"