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
Open
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
Binary file added source/.DS_Store
Binary file not shown.
2 changes: 0 additions & 2 deletions source/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ gem 'coffee-rails', '~> 4.0.0'

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
Expand Down
Binary file added source/app/.DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion source/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
40 changes: 40 additions & 0 deletions source/app/controllers/urls_controller.rb
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)

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.


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?

redirect_to @url.read_attribute('original')
end

private
def url_params
params.require(:url).permit(:original)
end
end
Binary file added source/app/models/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions source/app/models/ur_lindex.rb
Original file line number Diff line number Diff line change
@@ -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?

end
18 changes: 18 additions & 0 deletions source/app/models/url.rb
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/}
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.



before_save :shortify

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.

begin
self.shortened = "https://bit.ly/" + SecureRandom.hex(3)
end while Url.where(shortened: self.shortened).exists?
end
end
end
4 changes: 2 additions & 2 deletions source/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<html>
<head>
<title>Source</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body>
Expand Down
34 changes: 34 additions & 0 deletions source/app/views/urls/index.html.erb
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? %>
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.

</p>
<% end %>
</div>
5 changes: 5 additions & 0 deletions source/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
Rails.application.routes.draw do
get 'urls/click_link' => 'urls#click_link'

resources :urls

root 'urls#index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
Binary file added source/db/.DS_Store
Binary file not shown.
Binary file added source/db/migrate/.DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions source/db/migrate/20170609180109_create_urls.rb
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
9 changes: 9 additions & 0 deletions source/db/migrate/20170609192743_create_ur_lindices.rb
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
29 changes: 29 additions & 0 deletions source/db/schema.rb
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
5 changes: 5 additions & 0 deletions source/spec/models/original_and_count_spec.rb
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
5 changes: 5 additions & 0 deletions source/spec/models/ur_lindex_spec.rb
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
5 changes: 5 additions & 0 deletions source/spec/models/url_spec.rb
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