Skip to content
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

Jack Chester Solution #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jack-chester
Copy link

@jaybobo @matt529

Copy link

@glossawy glossawy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty cool overall.

@url = Url.find(params[:format])

new_count = @url.click_count + 1
@url.update_attribute(:click_count, new_count)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There actually is a increment_counter class method that updates the database for you as well:

Url.increment_counter :click_count, @url.id

It's unfortunate it doesn't appear to be available as an instance method but I moved it to a click! instance method on Url.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an increment and an increment! but isn't the increment_counter class method the only one that's atomic? Otherwise, I had misread that in the docs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry. I was just responding to your previous comment kinda out of context. It is super important to make this click counting work properly at web-scale when this bad boy gets to the front of Reddit. 😛

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If our clicks are not consistent at scale, how would we ever get to the top of /r/programming and topple bit.ly?

Just wanted to make sure I got that right :P who knows when I will need to know that? Possibly never.

new_count = @url.click_count + 1
@url.update_attribute(:click_count, new_count)

sleep 0.01

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using sleep here?

@@ -0,0 +1,2 @@
class UrLindex < ActiveRecord::Base

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this class created accidentally?

private
def shortify
if self.shortened == nil
self.click_count = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a note, you can specify a default in your migration. I don't know if you can do it with the rails generate cli but you can modify it as add_column :urls, :click_count, :integer, default: 0 in the migration.

@url = Url.find(params[:format])

new_count = @url.click_count + 1
@url.update_attribute(:click_count, new_count)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class Url < ActiveRecord::Base
validates :original, presence: true
validates :original, length: { minimum: 1 }
validates :original, format: {with: /\A((http|https):\/\/).*\Z/}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .*\Z part is unneeded. All strings will have some more content and eventually end. You don't need to specify it.

<div>
<% if @url %>
<p>
<%= "Please enter a valid URL starting with 'http://' or 'https://'" if @url.errors.any? %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can define the error message on the validation in model (if the default isn't good enough), then pull the messages from @url.errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants