-
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
Complete Release 3 #38
base: master
Are you sure you want to change the base?
Changes from 6 commits
817a193
4eca9cc
94b5e0b
bb19c2a
fb947e0
8ad65a0
0fa1957
0d7bc53
006bf22
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--require spec_helper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,27 @@ | ||
class UrlsController < ApplicationController | ||
def index | ||
@urls = Url.all | ||
end | ||
|
||
def create | ||
url = Url.new(url_params) | ||
url.click_count = 0 | ||
if !url.save | ||
flash[:error] = 'Invalid URL' | ||
end | ||
redirect_to urls_path | ||
end | ||
|
||
def show | ||
url = Url.find(params[:id]) | ||
url.click_count += 1 | ||
url.save | ||
redirect_to url.long_url | ||
end | ||
|
||
private | ||
|
||
def url_params | ||
params.require(:url).permit(:long_url) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Url < ActiveRecord::Base | ||
validates :long_url, presence: true, format: { with: /http(|s):\/\/.+/ } | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<p>Shorten URL:</p> | ||
|
||
<%= flash[:error] if !nil? %> | ||
|
||
<%= form_for :url do |f| %> | ||
<%= f.label :long_url %> | ||
<%= f.text_field :long_url %> | ||
<% end %> | ||
|
||
<ul> | ||
<% @urls.each do |url| %> | ||
<li><%= "0.0.0.0:3000/urls/#{url.id} ====> #{url.long_url} (clicked #{url.click_count} times)" %></li> | ||
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. Your routes file has a I think |
||
<% end %> | ||
</ul> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateUrls < ActiveRecord::Migration | ||
def change | ||
create_table :urls do |t| | ||
t.string :long_url | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddClickCounterToUrls < ActiveRecord::Migration | ||
def change | ||
add_column :urls, :click_count, :integer | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 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: 20180514175827) do | ||
|
||
create_table "urls", force: true do |t| | ||
t.string "long_url" | ||
t.datetime "created_at" | ||
t.datetime "updated_at" | ||
t.integer "click_count" | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require 'rails_helper' | ||
|
||
describe UrlsController do | ||
let(:url) { Url.create(long_url: 'https://www.google.com/', click_count: 0) } | ||
|
||
describe 'GET index' do | ||
it 'responds with a 200 OK' do | ||
get :index | ||
expect(response.status).to eq 200 | ||
end | ||
|
||
it 'assigns @urls' do | ||
get :index | ||
expect(assigns[:urls]).to eq [url] | ||
end | ||
|
||
it 'renders the index template' do | ||
get :index | ||
expect(response).to render_template('index') | ||
end | ||
end | ||
|
||
describe 'POST create' do | ||
context 'with valid params' do | ||
it 'creates a url entry' do | ||
expect do | ||
post :create, url: { long_url: 'https://www.google.com/' } | ||
end.to change { Url.count }.by 1 | ||
end | ||
|
||
it 'redirects to the index page' do | ||
post :create, url: { long_url: 'https://www.google.com/' } | ||
expect(response).to redirect_to urls_path | ||
end | ||
|
||
it 'sets the click counter to 0' do | ||
post :create, url: { long_url: 'https://www.google.com/' } | ||
expect(Url.first.click_count).to eq 0 | ||
end | ||
end | ||
|
||
context 'with invalid params' do | ||
it 'does not create a url entry' do | ||
expect do | ||
post :create, url: { long_url: 'invalid URL' } | ||
end.to_not change { Url.count } | ||
end | ||
|
||
it 'redirects to the index page' do | ||
post :create, url: { long_url: 'invalid URL' } | ||
expect(response).to redirect_to urls_path | ||
end | ||
|
||
it 'displays an error message' do | ||
post :create, url: { long_url: 'invalid URL' } | ||
expect(flash[:error]).to eq 'Invalid URL' | ||
end | ||
end | ||
end | ||
|
||
describe 'GET show' do | ||
it 'redirects to the long url' do | ||
get :show, { id: url.id } | ||
expect(response).to redirect_to 'https://www.google.com/' | ||
end | ||
|
||
it 'increases the click_counter' do | ||
expect do | ||
get :show, { id: url.id } | ||
end.to change { url.reload.click_count }.by 1 | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Url, :type => :model do | ||
let(:url) { Url.new(long_url: 'https://www.google.com/') } | ||
|
||
describe 'long_url' do | ||
it 'is not an empty string' do | ||
url.long_url = '' | ||
expect(url).to_not be_valid | ||
end | ||
|
||
it 'has a prefix http:// or https://' do | ||
url.long_url = 'www.google.com' | ||
expect(url).to_not be_valid | ||
|
||
url.long_url = 'http://www.google.com' | ||
expect(url).to be_valid | ||
|
||
url.long_url = 'https://www.google.com' | ||
expect(url).to be_valid | ||
end | ||
end | ||
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.
I could nitpick this
format
regexp, but I'm not sure if it is needed. Is theURI::regexp
matching sufficient?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're right - this is leftover from before I implemented the URI matching.