-
Notifications
You must be signed in to change notification settings - Fork 40
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an increment
instance method: http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-increment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. 😛
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an increment
instance method: http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-increment
class Url < ActiveRecord::Base | ||
validates :original, presence: true | ||
validates :original, length: { minimum: 1 } | ||
validates :original, format: {with: /\A((http|https):\/\/).*\Z/} |
There was a problem hiding this comment.
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? %> |
There was a problem hiding this comment.
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
.
@jaybobo @matt529