View mod_machine.rb
This file contains 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
module ModMachine | |
extend ActiveSupport::Concern | |
included do | |
end | |
module ClassMethods | |
# | |
# Builds a module with constants assigned to the index passed in the `options` array. Be sure | |
# to pass all arguments as you would write them manually. In other words `attribute` and any |
View Twitter Gem Geocode Examples
This file contains 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 'twitter' | |
Twitter::Search.new.geocode(39.037, -94.5874, '5mi').fetch().results | |
#=> all tweets within 5 miles of lat/lon | |
Twitter::Search.geocode(39.037, -94.5874, '5mi').fetch().results.first | |
#=> most recent tweet within 5 miles of lat/lon | |
Twitter::Search.new('dinner').geocode(39.037, -94.5874, '5mi').fetch().results | |
#=> all tweets containing the word 'dinner' within 5 miles of lat/lon |
View Geo.js
This file contains 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
var Geo = navigator.geolocation, // {Object|null} a shortcut to the geolocation static class | |
options = { // {Object} options available to calls for position | |
enableHighAccuracy: false, // {Boolean} Whether or not to use more resources to get a more accurate position | |
maximumAge: 0, // {Number|Infinity} The maximum number of milliseconds since the last check. | |
timeout: null // {Number|null} The maximum number of milliseconds to wait for a fix | |
}, | |
watchId; // {Number} an ID to a watch timeout | |
// handles an error finding the user's position | |
function onError(error) { |
View elder.rb
This file contains 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 Elder < ActiveRecord::Base | |
def self.increment_distribution_count(user_id) | |
self.find_by_user_id(user_id).increment!(:distribution_count) | |
end | |
end |
View dontdothis.rb
This file contains 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 UsersController < ApplicationController | |
... | |
def create | |
referral_id = User.find_valid_referral(params[:user][:referral]) | |
@user = User.new(params[:user].merge(:referral_id => referral_id)) | |
... | |
end | |
... | |
end |
View much_better_user.rb
This file contains 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
def self.find_valid_referral(username) | |
case | |
when username.blank? then EarlyAdopter.first(:group => "distribution_count ASC").user_id | |
when !username.blank? && self.exists?(:username => username) then User.find_by_username(username).id | |
else nil | |
end | |
end |
View referrer_must_be_existing_user.rb
This file contains 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
def referrer_must_be_existing_user | |
errors.add(:referral_id, "is not a valid user") if referral_id.blank? | |
end |
View first_bad_solution.rb
This file contains 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
def create | |
referral = User.find_by_username(params[:user][:referral]) | |
@user = User.new(params[:user].merge(:referral_id => referral.id)) | |
end |
View a_better_approach.rb
This file contains 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 User < ActiveRecord::Base | |
... | |
def self.find_valid_referral(username) | |
user = User.find_by_username(username) | |
if user.nil? | |
nil | |
else | |
user.id | |
end | |
end |
View full_solution.rb
This file contains 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 User < ActiveRecord::Base | |
validate_on_create :referrer_must_be_existing_user | |
... | |
def self.find_valid_referral(username) | |
case | |
when username.blank? then Elder.first(:group => "distribution_count ASC").user_id | |
when !username.blank? && self.exists?(:username => username) then User.find_by_username(username).id | |
else nil | |
end | |
end |
OlderNewer