Skip to content

Remove with_username completely #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/achievements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def award
render_404
else
if @api_access.can_award?(award_params[:badge])
user = User.with_username(award_params[provider], provider)
user = User.find_by_provider_username(award_params[provider], provider)
badge = badge_class_factory(award_params[:badge].to_s).new(user, Date.strptime(award_params[:date], '%m/%d/%Y'))
badge.generate_fact!(award_params[:badge], award_params[provider], provider)
unless user.nil?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def autocomplete

def refresh
refresh_params = params.permit(:username)
user = User.with_username(refresh_params[:username])
user = User.find_by_username(refresh_params[:username])
RefreshUserJob.perform_async(user.id, true)
flash[:notice] = "Queued #{refresh_params[:username]} for a refresh"
redirect_to :back
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ApplicationHelper
include TweetButton
include SchemaOrgHelper

def link_twitter_path
'/auth/twitter'
end
Expand Down Expand Up @@ -161,7 +161,7 @@ def user_endorsements
# https://twitter.com/#!/kennethkalmer/status/86392260555587584
endorsements << [User.find_by_username('kennethkalmer'), "@coderwall really dishes out some neat achievements, hope this helps motivate even more folks to contribute to FOSS"]

# endorsements << [User.with_username('jeffhogan'), 'I really dig @coderwall...I see great potential in utilizing @coderwall for portfolio/linkedin/professional ref. for developers!']
# endorsements << [User.find_by_username('jeffhogan'), 'I really dig @coderwall...I see great potential in utilizing @coderwall for portfolio/linkedin/professional ref. for developers!']

endorsements
end
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/award_user_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AwardUserJob
sidekiq_options queue: :user

def perform(username, badges)
user = User.with_username(username)
user = User.find_by_username(username)

if badges.first.is_a?(String)
badges.map!(&:constantize)
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/build_activity_stream_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class BuildActivityStreamJob
sidekiq_options queue: :timeline

def perform(username)
user = User.with_username(username)
user = User.find_by_username(username)
user.build_repo_followed_activity!
end
end
2 changes: 1 addition & 1 deletion app/jobs/github_badge_org_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class GithubBadgeOrgJob
sidekiq_options queue: :github

def perform(username, action)
user = User.with_username(username)
user = User.find_by_username(username)
unless user.nil? or user.github.nil?
if action.to_sym == :add
GithubBadge.new.add_all(user.badges, user.github)
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/seed_github_protips_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SeedGithubProtipsJob
sidekiq_options queue: :github

def perform(username)
user = User.with_username(username)
user = User.find_by_username(username)
user.build_github_proptips_fast
end
end
2 changes: 1 addition & 1 deletion app/mailers/mail_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MailPreview < MailView
def popular_protips
from = 60.days.ago
to = 0.days.ago
user = User.with_username(USERNAME)
user = User.find_by_username(USERNAME)
REDIS.srem(ProtipMailer::CAMPAIGN_ID, user.id.to_s)
protips = ProtipMailer::Queries.popular_protips(from, to)
ProtipMailer.popular_protips(user, protips, from, to).deliver
Expand Down
2 changes: 1 addition & 1 deletion app/models/fact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def tagged?(*required_tags)

def user
service, username = self.owner.split(":")
User.with_username(username, service)
User.find_by_provider_username(username, service)
end
end

Expand Down
22 changes: 6 additions & 16 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,13 @@ def near
#TODO Kill
scope :username_in, ->(usernames) { where(["UPPER(username) in (?)", usernames.collect(&:upcase)]) }

#TODO Kill
def self.with_username(username, provider = :username)
def self.find_by_provider_username(username, provider)
return nil if username.nil?
sql_injection_safe_where_clause = case provider.to_s
when 'username', ''
'username'
when 'linkedin'
'linkedin'
when 'twitter'
'twitter'
when 'github'
'github'
else
#A user could malicously pass in a provider, thats why we do the string matching above
raise "Unkown provider type specified, unable to find user by username"
end
where(["UPPER(#{sql_injection_safe_where_clause}) = UPPER(?)", username]).first
return self.find_by_username(username) if provider == ''
unless %w{twitter linkedin github}.include?(provider)
raise "Unkown provider type specified, unable to find user by username"
end
where(["UPPER(#{provider}) = UPPER(?)", username]).first
end

# Todo State machine
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/mailers.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace :mailers do
task popular_protips: :environment do
from = 60.days.ago
to = 0.days.ago
user = User.with_username('mcansky')
user = User.find_by_username('mcansky')
protips = ProtipMailer::Queries.popular_protips(from, to)
ProtipMailer.popular_protips(user, protips, from, to).deliver
end
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@
session[:referred_by] = 'asdfasdf'
session['oauth.data'] = github_response
post :create, user: { location: 'SF', username: 'testingReferredBy' }
user = User.with_username('testingReferredBy')
user = User.find_by_username('testingReferredBy')
expect(user.referred_by).to eq('asdfasdf')
end

it 'should not add referred by if not present' do
session['oauth.data'] = github_response
post :create, user: { location: 'SF', username: 'testingReferredBy' }
user = User.with_username('testingReferredBy')
user = User.find_by_username('testingReferredBy')
expect(user.referred_by).to be_nil
end
end
Expand All @@ -96,7 +96,7 @@
session[:utm_campaign] = 'asdfasdf'
session['oauth.data'] = github_response
post :create, user: { location: 'SF', username: 'testingUTM_campaign' }
user = User.with_username('testingUTM_campaign')
user = User.find_by_username('testingUTM_campaign')
expect(user.utm_campaign).to eq('asdfasdf')
end

Expand Down