Skip to content
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
99 changes: 52 additions & 47 deletions source/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,35 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
builder (3.2.2)
builder (3.2.3)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
coffee-script (2.3.0)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.8.0)
diff-lcs (1.2.5)
coffee-script-source (1.12.2)
diff-lcs (1.3)
erubis (2.7.0)
execjs (2.2.1)
execjs (2.7.0)
hike (1.2.3)
i18n (0.6.11)
jbuilder (2.2.2)
activesupport (>= 3.0.0, < 5)
i18n (0.7.0)
jbuilder (2.6.1)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
jquery-rails (3.1.2)
jquery-rails (3.1.4)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
mail (2.6.1)
mime-types (>= 1.16, < 3)
mime-types (2.4.1)
minitest (5.4.2)
multi_json (1.10.1)
rack (1.5.2)
rack-test (0.6.2)
json (1.8.6)
mail (2.6.4)
mime-types (>= 1.16, < 4)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
minitest (5.10.1)
multi_json (1.12.1)
rack (1.5.5)
rack-test (0.6.3)
rack (>= 1.0)
rails (4.1.6)
actionmailer (= 4.1.6)
Expand All @@ -71,55 +73,55 @@ GEM
activesupport (= 4.1.6)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.3.2)
rdoc (4.1.2)
json (~> 1.4)
rspec-core (3.1.6)
rspec-support (~> 3.1.0)
rspec-expectations (3.1.2)
rake (12.0.0)
rdoc (4.3.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.1.0)
rspec-mocks (3.1.3)
rspec-support (~> 3.1.0)
rspec-rails (3.1.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-rails (3.5.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.1.0)
rspec-expectations (~> 3.1.0)
rspec-mocks (~> 3.1.0)
rspec-support (~> 3.1.0)
rspec-support (3.1.2)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
sass (3.2.19)
sass-rails (4.0.3)
sass-rails (4.0.5)
railties (>= 4.0.0, < 5.0)
sass (~> 3.2.0)
sprockets (~> 2.8, <= 2.11.0)
sass (~> 3.2.2)
sprockets (~> 2.8, < 3.0)
sprockets-rails (~> 2.0)
sdoc (0.4.1)
sdoc (0.4.2)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
spring (1.1.3)
sprockets (2.11.0)
spring (1.7.2)
sprockets (2.12.4)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets-rails (2.2.0)
sprockets-rails (2.3.3)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
sqlite3 (1.3.9)
thor (0.19.1)
thread_safe (0.3.4)
sqlite3 (1.3.13)
thor (0.19.4)
thread_safe (0.3.5)
tilt (1.4.1)
turbolinks (2.4.0)
coffee-rails
turbolinks (5.0.1)
turbolinks-source (~> 5)
turbolinks-source (5.0.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (2.5.3)
execjs (>= 0.3.0)
json (>= 1.8.0)
uglifier (3.0.4)
execjs (>= 0.3.0, < 3)

PLATFORMS
ruby
Expand All @@ -136,3 +138,6 @@ DEPENDENCIES
sqlite3
turbolinks
uglifier (>= 1.3.0)

BUNDLED WITH
1.13.7
12 changes: 12 additions & 0 deletions source/app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
class UrlsController < ApplicationController
def index
@urls = Url.all
end

def new
@url = Url.new
end

def show
@url = Url.find(params[:id])
end

end
14 changes: 14 additions & 0 deletions source/app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Url < ActiveRecord::Base

before_save :shorten_url
validates_uniqueness_of :unique_key

private
#generate 6 digit unique identifier
def shorten_url(record)
if self.unique_key.nil?
self.unique_key = 6.times.map { [*'0'..'9', *'A'..'Z'].sample }.join
Copy link
Member

Choose a reason for hiding this comment

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

This array of characters you're calling sample on can be extracted to a constant. That way ruby doesn't have to reconstruct it 6 times for every call to this method.

end
end

end
19 changes: 19 additions & 0 deletions source/app/views/urls/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1>All Urls</h1>
<table>
<thead>
</thead>
<tbody>
<% @urls.each do |url| %>
<tr>
<td><% link_to root_url+url.unique_key, url.unique_key %></td>
<td><% url.address %></td>
</tr>
<% end %>
<%if @urls.empty? %>
<p> No urls to show </p>
<% end %>
</tbody>
</table>

<br>
<%= link_to "New Url",new_url_path %>
12 changes: 12 additions & 0 deletions source/app/views/urls/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1>Create a new link</h1>
<%= form_for @url do |f| %>
<p>
<%= f.label :address %><br>
<%= f.text_field :address %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>

<%= link_to 'Back', urls_path %>
56 changes: 2 additions & 54 deletions source/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,4 @@
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
resources :urls
root 'urls#index'
end
9 changes: 9 additions & 0 deletions source/db/migrate/20170120134736_create_urls.rb
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 :address
t.string :unique_key
t.timestamps
end
end
end
23 changes: 23 additions & 0 deletions source/db/schema.rb
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: 20170120134736) do

create_table "urls", force: true do |t|
t.string "address"
t.string "unique_key"
t.datetime "created_at"
t.datetime "updated_at"
end

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