-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,4 @@ | |
// | ||
//= require jquery | ||
//= require jquery_ujs | ||
//= require turbolinks | ||
//= require_tree . |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,42 @@ | ||
class UrlsController < ApplicationController | ||
|
||
def index | ||
@urls = Url.all | ||
end | ||
|
||
def new | ||
end | ||
|
||
def create | ||
@url = Url.new(url_params) | ||
|
||
if @url.save | ||
redirect_to '/' | ||
else | ||
@urls = Url.all | ||
render 'index' | ||
end | ||
end | ||
|
||
def destroy | ||
@url = Url.find(params[:id]) | ||
@url.destroy | ||
|
||
redirect_to '/' | ||
end | ||
|
||
def click_link | ||
@url = Url.find(params[:format]) | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Why are you using |
||
redirect_to @url.read_attribute('original') | ||
end | ||
|
||
private | ||
def url_params | ||
params.require(:url).permit(:original) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class UrLindex < ActiveRecord::Base | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this class created accidentally? |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
|
||
before_save :shortify | ||
|
||
private | ||
def shortify | ||
if self.shortened == nil | ||
self.click_count = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
begin | ||
self.shortened = "https://bit.ly/" + SecureRandom.hex(3) | ||
end while Url.where(shortened: self.shortened).exists? | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<h1>Bitly Clone</h1> | ||
|
||
<table> | ||
<tr> | ||
<th>Destination:</th> | ||
<th>Short Url:</th> | ||
<th>Clicks:</th> | ||
</tr> | ||
|
||
<% @urls.each do |url| %> | ||
<tr> | ||
<td><%= url.original %></td> | ||
<td style="text-align: center"><%= link_to url.shortened, urls_click_link_path(url) %></td> | ||
<td style="text-align: center"><%= url.click_count %></td> | ||
<td><%= link_to 'Delete', url_path(url), method: :delete %></td> | ||
</tr> | ||
<% end %> | ||
</table> | ||
|
||
<%= form_for :url do |f| %> | ||
<p> | ||
<%= f.label :'Enter Url:' %><br> | ||
<%= f.text_field :original %> | ||
<%= f.submit %> | ||
</p> | ||
<% end %> | ||
|
||
<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 commentThe 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 |
||
</p> | ||
<% end %> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateUrls < ActiveRecord::Migration | ||
def change | ||
create_table :urls do |t| | ||
t.string :shortened | ||
t.string :original | ||
t.integer :click_count | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateUrLindices < ActiveRecord::Migration | ||
def change | ||
create_table :ur_lindices do |t| | ||
|
||
add_index :urls, unique: true | ||
|
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# encoding: UTF-8 | ||
# This file is auto-generated from the current state of the database. Instead | ||
# of editing this file, please use the migrations feature of Active Record to | ||
# incrementally modify your database, and then regenerate this schema definition. | ||
# | ||
# Note that this schema.rb definition is the authoritative source for your | ||
# database schema. If you need to create the application database on another | ||
# system, you should be using db:schema:load, not running all the migrations | ||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
# you'll amass, the slower it'll run and the greater likelihood for issues). | ||
# | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(version: 20170609192743) do | ||
|
||
create_table "ur_lindices", force: true do |t| | ||
end | ||
|
||
create_table "urls", force: true do |t| | ||
t.string "shortened" | ||
t.string "original" | ||
t.integer "click_count" | ||
t.datetime "created_at" | ||
t.datetime "updated_at" | ||
end | ||
|
||
add_index "urls", [nil], name: "index_urls_on_unique_and_true" | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe OriginalAndCount, :type => :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe UrLindex, :type => :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Url, :type => :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
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:It's unfortunate it doesn't appear to be available as an instance method but I moved it to a
click!
instance method onUrl
.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-incrementThere 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 anincrement!
but isn't theincrement_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.