From 76d81c302838dd4ea1560449fce99c7941b89b16 Mon Sep 17 00:00:00 2001 From: Robert Lude Date: Mon, 26 Jan 2015 15:09:27 -0500 Subject: [PATCH 1/5] Created URL resource --- .../app/assets/stylesheets/scaffolds.css.scss | 69 ++++++++ source/app/assets/stylesheets/urls.css.scss | 13 +- source/app/controllers/urls_controller.rb | 72 ++++++++ source/app/models/.url.rb.swp | Bin 0 -> 12288 bytes source/app/models/url.rb | 2 + source/app/views/layouts/application.html.erb | 2 +- source/app/views/urls/_form.html.erb | 25 +++ source/app/views/urls/edit.html.erb | 6 + source/app/views/urls/index.html.erb | 27 +++ source/app/views/urls/index.json.jbuilder | 4 + source/app/views/urls/new.html.erb | 5 + source/app/views/urls/show.html.erb | 14 ++ source/app/views/urls/show.json.jbuilder | 1 + source/config/routes.rb | 2 + .../db/migrate/20150126180838_create_urls.rb | 10 ++ source/db/schema.rb | 23 +++ source/db/seeds.rb | 6 + .../spec/controllers/urls_controller_spec.rb | 159 ++++++++++++++++++ source/spec/helpers/urls_helper_spec.rb | 15 ++ source/spec/models/url_spec.rb | 5 + source/spec/requests/urls_spec.rb | 10 ++ source/spec/routing/urls_routing_spec.rb | 35 ++++ source/spec/views/urls/edit.html.erb_spec.rb | 21 +++ source/spec/views/urls/index.html.erb_spec.rb | 22 +++ source/spec/views/urls/new.html.erb_spec.rb | 21 +++ source/spec/views/urls/show.html.erb_spec.rb | 16 ++ 26 files changed, 583 insertions(+), 2 deletions(-) create mode 100644 source/app/assets/stylesheets/scaffolds.css.scss create mode 100644 source/app/models/.url.rb.swp create mode 100644 source/app/models/url.rb create mode 100644 source/app/views/urls/_form.html.erb create mode 100644 source/app/views/urls/edit.html.erb create mode 100644 source/app/views/urls/index.html.erb create mode 100644 source/app/views/urls/index.json.jbuilder create mode 100644 source/app/views/urls/new.html.erb create mode 100644 source/app/views/urls/show.html.erb create mode 100644 source/app/views/urls/show.json.jbuilder create mode 100644 source/db/migrate/20150126180838_create_urls.rb create mode 100644 source/db/schema.rb create mode 100644 source/spec/controllers/urls_controller_spec.rb create mode 100644 source/spec/helpers/urls_helper_spec.rb create mode 100644 source/spec/models/url_spec.rb create mode 100644 source/spec/requests/urls_spec.rb create mode 100644 source/spec/routing/urls_routing_spec.rb create mode 100644 source/spec/views/urls/edit.html.erb_spec.rb create mode 100644 source/spec/views/urls/index.html.erb_spec.rb create mode 100644 source/spec/views/urls/new.html.erb_spec.rb create mode 100644 source/spec/views/urls/show.html.erb_spec.rb diff --git a/source/app/assets/stylesheets/scaffolds.css.scss b/source/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 0000000..6ec6a8f --- /dev/null +++ b/source/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,69 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + &:visited { + color: #666; + } + &:hover { + color: #fff; + background-color: #000; + } +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; + } + ul li { + font-size: 12px; + list-style: square; + } +} diff --git a/source/app/assets/stylesheets/urls.css.scss b/source/app/assets/stylesheets/urls.css.scss index a4281ec..7abea80 100644 --- a/source/app/assets/stylesheets/urls.css.scss +++ b/source/app/assets/stylesheets/urls.css.scss @@ -1,3 +1,14 @@ -// Place all the styles related to the Urls controller here. +// Place all the styles related to the urls controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ + +.urls { + table { + border-collapse: collapse; + tr { + td { + padding: 1em; + } + } + } +} diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index ef26710..a71c040 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -1,2 +1,74 @@ class UrlsController < ApplicationController + before_action :set_url, only: [:show, :edit, :update, :destroy] + + # GET /urls + # GET /urls.json + def index + @urls = Url.all + end + + # GET /urls/1 + # GET /urls/1.json + def show + end + + # GET /urls/new + def new + @url = Url.new + end + + # GET /urls/1/edit + def edit + end + + # POST /urls + # POST /urls.json + def create + @url = Url.new(url_params) + + respond_to do |format| + if @url.save + format.html { redirect_to @url, notice: 'Url was successfully created.' } + format.json { render :show, status: :created, location: @url } + else + format.html { render :new } + format.json { render json: @url.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /urls/1 + # PATCH/PUT /urls/1.json + def update + respond_to do |format| + if @url.update(url_params) + format.html { redirect_to @url, notice: 'Url was successfully updated.' } + format.json { render :show, status: :ok, location: @url } + else + format.html { render :edit } + format.json { render json: @url.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /urls/1 + # DELETE /urls/1.json + def destroy + @url.destroy + respond_to do |format| + format.html { redirect_to urls_url, notice: 'Url was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_url + @url = Url.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def url_params + params.require(:url).permit(:shortcode, :destination) + end end diff --git a/source/app/models/.url.rb.swp b/source/app/models/.url.rb.swp new file mode 100644 index 0000000000000000000000000000000000000000..26d0a162bf608eb70ac19692cf028bfed54ecdf9 GIT binary patch literal 12288 zcmeI%%}N6?5C`z7M?sYO0#Waq^@~FBV%Lk{QqZ<|O_QL7akC`5f)@oJ#G~)v3-}az z@a#*tTXt^+d$jl;NJ555V1BodX*@YkMK3y{Dz=DTpWjC>-!X}GBG09qtdVz$|h4RSCm_#t7JSs7gEwUsK*o?x9jS6iVh$E0SG_<0uX=z1Rwwb2tWV=|5rd6qPI<=&uY~F|Lyz#tI2)Tc?1L? w009U<00Izz00bZa0SG_<0)JP)W`;DY5X9KCI1#71nBCh;tDQIf{y+swKTPR5NdN!< literal 0 HcmV?d00001 diff --git a/source/app/models/url.rb b/source/app/models/url.rb new file mode 100644 index 0000000..e4834ff --- /dev/null +++ b/source/app/models/url.rb @@ -0,0 +1,2 @@ +class Url < ActiveRecord::Base +end diff --git a/source/app/views/layouts/application.html.erb b/source/app/views/layouts/application.html.erb index f946432..69c74e8 100644 --- a/source/app/views/layouts/application.html.erb +++ b/source/app/views/layouts/application.html.erb @@ -6,7 +6,7 @@ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> <%= csrf_meta_tags %> - + <%= yield %> diff --git a/source/app/views/urls/_form.html.erb b/source/app/views/urls/_form.html.erb new file mode 100644 index 0000000..e07b728 --- /dev/null +++ b/source/app/views/urls/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for(@url) do |f| %> + <% if @url.errors.any? %> +
+

<%= pluralize(@url.errors.count, "error") %> prohibited this url from being saved:

+ +
    + <% @url.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= f.label :shortcode %>
+ <%= f.text_field :shortcode %> +
+
+ <%= f.label :destination %>
+ <%= f.text_field :destination %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/source/app/views/urls/edit.html.erb b/source/app/views/urls/edit.html.erb new file mode 100644 index 0000000..e52fdf4 --- /dev/null +++ b/source/app/views/urls/edit.html.erb @@ -0,0 +1,6 @@ +

Editing url

+ +<%= render 'form' %> + +<%= link_to 'Show', @url %> | +<%= link_to 'Back', urls_path %> diff --git a/source/app/views/urls/index.html.erb b/source/app/views/urls/index.html.erb new file mode 100644 index 0000000..ab02afe --- /dev/null +++ b/source/app/views/urls/index.html.erb @@ -0,0 +1,27 @@ +

Listing urls

+ + + + + + + + + + + + <% @urls.each do |url| %> + + + + + + + + <% end %> + +
ShortcodeDestination
<%= url.shortcode %><%= url.destination %><%= link_to 'Show', url %><%= link_to 'Edit', edit_url_path(url) %><%= link_to 'Destroy', url, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Url', new_url_path %> diff --git a/source/app/views/urls/index.json.jbuilder b/source/app/views/urls/index.json.jbuilder new file mode 100644 index 0000000..cc2fe35 --- /dev/null +++ b/source/app/views/urls/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@urls) do |url| + json.extract! url, :id, :shortcode, :destination + json.url url_url(url, format: :json) +end diff --git a/source/app/views/urls/new.html.erb b/source/app/views/urls/new.html.erb new file mode 100644 index 0000000..7e711d5 --- /dev/null +++ b/source/app/views/urls/new.html.erb @@ -0,0 +1,5 @@ +

New url

+ +<%= render 'form' %> + +<%= link_to 'Back', urls_path %> diff --git a/source/app/views/urls/show.html.erb b/source/app/views/urls/show.html.erb new file mode 100644 index 0000000..9aa7107 --- /dev/null +++ b/source/app/views/urls/show.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

+ Shortcode: + <%= @url.shortcode %> +

+ +

+ Destination: + <%= @url.destination %> +

+ +<%= link_to 'Edit', edit_url_path(@url) %> | +<%= link_to 'Back', urls_path %> diff --git a/source/app/views/urls/show.json.jbuilder b/source/app/views/urls/show.json.jbuilder new file mode 100644 index 0000000..bf7cf02 --- /dev/null +++ b/source/app/views/urls/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @url, :id, :shortcode, :destination, :created_at, :updated_at diff --git a/source/config/routes.rb b/source/config/routes.rb index 3f66539..ff8326b 100644 --- a/source/config/routes.rb +++ b/source/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + resources :urls + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/source/db/migrate/20150126180838_create_urls.rb b/source/db/migrate/20150126180838_create_urls.rb new file mode 100644 index 0000000..d132adf --- /dev/null +++ b/source/db/migrate/20150126180838_create_urls.rb @@ -0,0 +1,10 @@ +class CreateUrls < ActiveRecord::Migration + def change + create_table :urls do |t| + t.string :shortcode + t.string :destination + + t.timestamps + end + end +end diff --git a/source/db/schema.rb b/source/db/schema.rb new file mode 100644 index 0000000..7d89426 --- /dev/null +++ b/source/db/schema.rb @@ -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: 20150126180838) do + + create_table "urls", force: true do |t| + t.string "shortcode" + t.string "destination" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/source/db/seeds.rb b/source/db/seeds.rb index 4edb1e8..be5b308 100644 --- a/source/db/seeds.rb +++ b/source/db/seeds.rb @@ -5,3 +5,9 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) + +Url.delete_all + +Url.create! shortcode: "xxx", destination: "https://www.google.com/" +Url.create! shortcode: "yyy", destination: "https://www.stackoverflow.com/" +Url.create! shortcode: "zzz", destination: "https://www.mint.com/" diff --git a/source/spec/controllers/urls_controller_spec.rb b/source/spec/controllers/urls_controller_spec.rb new file mode 100644 index 0000000..7014ddd --- /dev/null +++ b/source/spec/controllers/urls_controller_spec.rb @@ -0,0 +1,159 @@ +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +RSpec.describe UrlsController, :type => :controller do + + # This should return the minimal set of attributes required to create a valid + # Url. As you add validations to Url, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # UrlsController. Be sure to keep this updated too. + let(:valid_session) { {} } + + describe "GET index" do + it "assigns all urls as @urls" do + url = Url.create! valid_attributes + get :index, {}, valid_session + expect(assigns(:urls)).to eq([url]) + end + end + + describe "GET show" do + it "assigns the requested url as @url" do + url = Url.create! valid_attributes + get :show, {:id => url.to_param}, valid_session + expect(assigns(:url)).to eq(url) + end + end + + describe "GET new" do + it "assigns a new url as @url" do + get :new, {}, valid_session + expect(assigns(:url)).to be_a_new(Url) + end + end + + describe "GET edit" do + it "assigns the requested url as @url" do + url = Url.create! valid_attributes + get :edit, {:id => url.to_param}, valid_session + expect(assigns(:url)).to eq(url) + end + end + + describe "POST create" do + describe "with valid params" do + it "creates a new Url" do + expect { + post :create, {:url => valid_attributes}, valid_session + }.to change(Url, :count).by(1) + end + + it "assigns a newly created url as @url" do + post :create, {:url => valid_attributes}, valid_session + expect(assigns(:url)).to be_a(Url) + expect(assigns(:url)).to be_persisted + end + + it "redirects to the created url" do + post :create, {:url => valid_attributes}, valid_session + expect(response).to redirect_to(Url.last) + end + end + + describe "with invalid params" do + it "assigns a newly created but unsaved url as @url" do + post :create, {:url => invalid_attributes}, valid_session + expect(assigns(:url)).to be_a_new(Url) + end + + it "re-renders the 'new' template" do + post :create, {:url => invalid_attributes}, valid_session + expect(response).to render_template("new") + end + end + end + + describe "PUT update" do + describe "with valid params" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested url" do + url = Url.create! valid_attributes + put :update, {:id => url.to_param, :url => new_attributes}, valid_session + url.reload + skip("Add assertions for updated state") + end + + it "assigns the requested url as @url" do + url = Url.create! valid_attributes + put :update, {:id => url.to_param, :url => valid_attributes}, valid_session + expect(assigns(:url)).to eq(url) + end + + it "redirects to the url" do + url = Url.create! valid_attributes + put :update, {:id => url.to_param, :url => valid_attributes}, valid_session + expect(response).to redirect_to(url) + end + end + + describe "with invalid params" do + it "assigns the url as @url" do + url = Url.create! valid_attributes + put :update, {:id => url.to_param, :url => invalid_attributes}, valid_session + expect(assigns(:url)).to eq(url) + end + + it "re-renders the 'edit' template" do + url = Url.create! valid_attributes + put :update, {:id => url.to_param, :url => invalid_attributes}, valid_session + expect(response).to render_template("edit") + end + end + end + + describe "DELETE destroy" do + it "destroys the requested url" do + url = Url.create! valid_attributes + expect { + delete :destroy, {:id => url.to_param}, valid_session + }.to change(Url, :count).by(-1) + end + + it "redirects to the urls list" do + url = Url.create! valid_attributes + delete :destroy, {:id => url.to_param}, valid_session + expect(response).to redirect_to(urls_url) + end + end + +end diff --git a/source/spec/helpers/urls_helper_spec.rb b/source/spec/helpers/urls_helper_spec.rb new file mode 100644 index 0000000..bbafaf3 --- /dev/null +++ b/source/spec/helpers/urls_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the UrlsHelper. For example: +# +# describe UrlsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe UrlsHelper, :type => :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/source/spec/models/url_spec.rb b/source/spec/models/url_spec.rb new file mode 100644 index 0000000..209ca4c --- /dev/null +++ b/source/spec/models/url_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Url, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/source/spec/requests/urls_spec.rb b/source/spec/requests/urls_spec.rb new file mode 100644 index 0000000..cd1976c --- /dev/null +++ b/source/spec/requests/urls_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +RSpec.describe "Urls", :type => :request do + describe "GET /urls" do + it "works! (now write some real specs)" do + get urls_path + expect(response).to have_http_status(200) + end + end +end diff --git a/source/spec/routing/urls_routing_spec.rb b/source/spec/routing/urls_routing_spec.rb new file mode 100644 index 0000000..d4ec06f --- /dev/null +++ b/source/spec/routing/urls_routing_spec.rb @@ -0,0 +1,35 @@ +require "rails_helper" + +RSpec.describe UrlsController, :type => :routing do + describe "routing" do + + it "routes to #index" do + expect(:get => "/urls").to route_to("urls#index") + end + + it "routes to #new" do + expect(:get => "/urls/new").to route_to("urls#new") + end + + it "routes to #show" do + expect(:get => "/urls/1").to route_to("urls#show", :id => "1") + end + + it "routes to #edit" do + expect(:get => "/urls/1/edit").to route_to("urls#edit", :id => "1") + end + + it "routes to #create" do + expect(:post => "/urls").to route_to("urls#create") + end + + it "routes to #update" do + expect(:put => "/urls/1").to route_to("urls#update", :id => "1") + end + + it "routes to #destroy" do + expect(:delete => "/urls/1").to route_to("urls#destroy", :id => "1") + end + + end +end diff --git a/source/spec/views/urls/edit.html.erb_spec.rb b/source/spec/views/urls/edit.html.erb_spec.rb new file mode 100644 index 0000000..3003a53 --- /dev/null +++ b/source/spec/views/urls/edit.html.erb_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe "urls/edit", :type => :view do + before(:each) do + @url = assign(:url, Url.create!( + :shortcode => "MyString", + :destination => "MyString" + )) + end + + it "renders the edit url form" do + render + + assert_select "form[action=?][method=?]", url_path(@url), "post" do + + assert_select "input#url_shortcode[name=?]", "url[shortcode]" + + assert_select "input#url_destination[name=?]", "url[destination]" + end + end +end diff --git a/source/spec/views/urls/index.html.erb_spec.rb b/source/spec/views/urls/index.html.erb_spec.rb new file mode 100644 index 0000000..1fbc0ce --- /dev/null +++ b/source/spec/views/urls/index.html.erb_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe "urls/index", :type => :view do + before(:each) do + assign(:urls, [ + Url.create!( + :shortcode => "Shortcode", + :destination => "Destination" + ), + Url.create!( + :shortcode => "Shortcode", + :destination => "Destination" + ) + ]) + end + + it "renders a list of urls" do + render + assert_select "tr>td", :text => "Shortcode".to_s, :count => 2 + assert_select "tr>td", :text => "Destination".to_s, :count => 2 + end +end diff --git a/source/spec/views/urls/new.html.erb_spec.rb b/source/spec/views/urls/new.html.erb_spec.rb new file mode 100644 index 0000000..f04b5f7 --- /dev/null +++ b/source/spec/views/urls/new.html.erb_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe "urls/new", :type => :view do + before(:each) do + assign(:url, Url.new( + :shortcode => "MyString", + :destination => "MyString" + )) + end + + it "renders new url form" do + render + + assert_select "form[action=?][method=?]", urls_path, "post" do + + assert_select "input#url_shortcode[name=?]", "url[shortcode]" + + assert_select "input#url_destination[name=?]", "url[destination]" + end + end +end diff --git a/source/spec/views/urls/show.html.erb_spec.rb b/source/spec/views/urls/show.html.erb_spec.rb new file mode 100644 index 0000000..4b0727d --- /dev/null +++ b/source/spec/views/urls/show.html.erb_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +RSpec.describe "urls/show", :type => :view do + before(:each) do + @url = assign(:url, Url.create!( + :shortcode => "Shortcode", + :destination => "Destination" + )) + end + + it "renders attributes in

" do + render + expect(rendered).to match(/Shortcode/) + expect(rendered).to match(/Destination/) + end +end From fa517770c7b81a24375f41f55d07b6c41d3497be Mon Sep 17 00:00:00 2001 From: Robert Lude Date: Tue, 27 Jan 2015 12:02:05 -0500 Subject: [PATCH 2/5] Removed shortcode from url table and created custom validator for Urls --- source/app/helpers/urls_helper.rb | 1 + source/app/models/url.rb | 1 + .../.url_validator.rb.swp} | Bin 12288 -> 12288 bytes source/app/validators/url_validator.rb | 25 ++++++++++++++++++ source/app/views/urls/_form.html.erb | 4 --- source/app/views/urls/index.html.erb | 2 -- source/app/views/urls/show.html.erb | 5 ---- source/config/application.rb | 1 + ...150126204532_remove_shortcode_from_urls.rb | 5 ++++ source/db/schema.rb | 3 +-- source/db/seeds.rb | 6 ++--- 11 files changed, 37 insertions(+), 16 deletions(-) rename source/app/{models/.url.rb.swp => validators/.url_validator.rb.swp} (86%) create mode 100644 source/app/validators/url_validator.rb create mode 100644 source/db/migrate/20150126204532_remove_shortcode_from_urls.rb diff --git a/source/app/helpers/urls_helper.rb b/source/app/helpers/urls_helper.rb index 83216b1..4c883ba 100644 --- a/source/app/helpers/urls_helper.rb +++ b/source/app/helpers/urls_helper.rb @@ -1,2 +1,3 @@ module UrlsHelper + end diff --git a/source/app/models/url.rb b/source/app/models/url.rb index e4834ff..1765792 100644 --- a/source/app/models/url.rb +++ b/source/app/models/url.rb @@ -1,2 +1,3 @@ class Url < ActiveRecord::Base + validates :destination, url: true end diff --git a/source/app/models/.url.rb.swp b/source/app/validators/.url_validator.rb.swp similarity index 86% rename from source/app/models/.url.rb.swp rename to source/app/validators/.url_validator.rb.swp index 26d0a162bf608eb70ac19692cf028bfed54ecdf9..1cd3fc3a4b593949fe84b433a733d3ac53c96419 100644 GIT binary patch literal 12288 zcmeI2L2nyH6vroAX(<9BmD|HTBnWrc&{hSD5Va<#krJnhohVWo%y{=DUUfXPotX^; zO8EltT{y$B2Tq*%4is?!ao_}R#_Po(ORczp-b#OuXZF2$Z+`Esl*l{o9Bl2t&E&eE z>qQ}+JpOKY`SLaK%7qnSRh45{f6CR{dH-N%cW}5p?A*ASD3eLG)ZBE%0 ziBt;pDZ2D_;njIh8I7qkmCZ1fWtpByRpine>(a`qQ8P{KcxeD$WCBcJIf2TbTz&hZ zxc1hYonUaK^%}hV(*AN=US$GIfC(@GCcp%k025#WOyGY>z?UoH3p&|z_32)%$CZDK z^^^xDzyz286JP>NfC(@GCcp%k025#WOyECAK;}Z|=Y{y06n_7o|NsBTB_VzyJt2Kd z`iAr+sZV-`^!*D$JSKfbIwaj7{dQ4^A4rF!KI!WVLVQFzBy~x@Q;c6pztr)53^Dm) z0!)AjFaajO1egF5U;<3wX#`Ny-|YZl0Jc_Y*8E)zF@yZ9%0~upqOTZIaPD)rmKw z0^Wt|olczyKw0?(t3taHGvC~uyHlG?yf2dpofOt8TO_6QlLV)wKi_~2Xr(Ra!hHSp z+P{>iIHDp^6>Q?Xv>tK;55rCmmzh)!N6sk<7It#vImHAYDB3^xxLI*2=Y@UAxT!;; z@#E+^G0wMV=@>gZ%@|J$9fLkot4%BCf%1V+b)XgtFmxZO6f_9w$?rj?r!` zBpzeYIVeHu`88_(3TI&AS6YsT>Za+H&*IAG&F<-kNAl{Y$8U7+rT5Yl)*poir{{>a z%<*L2?Klcgz}oD|ZZAG4bu5u7NiDE%)xrEVc`E*`%ok_4L+zlb`)P;frVth%RE0%Y L)#%fBLLvSHK!U6- delta 138 zcmZojXh@JwG6?hZRWR2xVE_UF28Lx#$3o`4HegWTVwfo1#G9L+lA2SjUs{x-SCq7w zk;R31^E+lG9!93kf&!oU<#zHjFx&-VkWMC$2|(}=Dt3o|v!cLme&y7>6o%xS#NuLw d(4rg#8wJPYlFYKypw#62q7*ADr^Mn^1_1s}C3gS- diff --git a/source/app/validators/url_validator.rb b/source/app/validators/url_validator.rb new file mode 100644 index 0000000..e6e62d3 --- /dev/null +++ b/source/app/validators/url_validator.rb @@ -0,0 +1,25 @@ + +require 'net/http' + +class UrlValidator < ActiveModel::EachValidator + + def validate_each(record, attribute, value) + value = "http://#{value}" unless /^[a-z]+:\/\// =~ value + uri = URI(value) + return record.errors.add attribute, "must use HTTP or HTTPS" unless ['http','https'].member? uri.scheme + begin + page_checker = Net::HTTP.new uri.host, uri.port + page_checker.use_ssl = uri.scheme.eql? "https" + page_checker.start do |http| + response = http.head (uri.path.empty? ? "/" : uri.path) + puts response.inspect + return record.errors.add attribute, "is not a valid address according to their server" unless response.code.to_i < 400 + end + rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e + return record.errors.add attribute, "could not be reached for validation" + end + rescue URI::InvalidURIError + record.errors[attribute] = "does not seem to be a valid URL" + end + +end diff --git a/source/app/views/urls/_form.html.erb b/source/app/views/urls/_form.html.erb index e07b728..4dea0a9 100644 --- a/source/app/views/urls/_form.html.erb +++ b/source/app/views/urls/_form.html.erb @@ -11,10 +11,6 @@ <% end %> -

- <%= f.label :shortcode %>
- <%= f.text_field :shortcode %> -
<%= f.label :destination %>
<%= f.text_field :destination %> diff --git a/source/app/views/urls/index.html.erb b/source/app/views/urls/index.html.erb index ab02afe..a56da1e 100644 --- a/source/app/views/urls/index.html.erb +++ b/source/app/views/urls/index.html.erb @@ -3,7 +3,6 @@ - @@ -12,7 +11,6 @@ <% @urls.each do |url| %> - diff --git a/source/app/views/urls/show.html.erb b/source/app/views/urls/show.html.erb index 9aa7107..83a137d 100644 --- a/source/app/views/urls/show.html.erb +++ b/source/app/views/urls/show.html.erb @@ -1,10 +1,5 @@

<%= notice %>

-

- Shortcode: - <%= @url.shortcode %> -

-

Destination: <%= @url.destination %> diff --git a/source/config/application.rb b/source/config/application.rb index 9a1de00..d337d0b 100644 --- a/source/config/application.rb +++ b/source/config/application.rb @@ -8,6 +8,7 @@ module Source class Application < Rails::Application + config.autoload_paths += %W["#{config.root}/app/validators/"] # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. diff --git a/source/db/migrate/20150126204532_remove_shortcode_from_urls.rb b/source/db/migrate/20150126204532_remove_shortcode_from_urls.rb new file mode 100644 index 0000000..b73eca2 --- /dev/null +++ b/source/db/migrate/20150126204532_remove_shortcode_from_urls.rb @@ -0,0 +1,5 @@ +class RemoveShortcodeFromUrls < ActiveRecord::Migration + def change + remove_column :urls, :shortcode, :string + end +end diff --git a/source/db/schema.rb b/source/db/schema.rb index 7d89426..f78f212 100644 --- a/source/db/schema.rb +++ b/source/db/schema.rb @@ -11,10 +11,9 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150126180838) do +ActiveRecord::Schema.define(version: 20150126204532) do create_table "urls", force: true do |t| - t.string "shortcode" t.string "destination" t.datetime "created_at" t.datetime "updated_at" diff --git a/source/db/seeds.rb b/source/db/seeds.rb index be5b308..54ef583 100644 --- a/source/db/seeds.rb +++ b/source/db/seeds.rb @@ -8,6 +8,6 @@ Url.delete_all -Url.create! shortcode: "xxx", destination: "https://www.google.com/" -Url.create! shortcode: "yyy", destination: "https://www.stackoverflow.com/" -Url.create! shortcode: "zzz", destination: "https://www.mint.com/" +Url.create! destination: "https://www.google.com/" +Url.create! destination: "https://www.stackoverflow.com/" +Url.create! destination: "https://www.mint.com/" From e50d1ee6aa863d2d5d0183cbef8dda0dccc474c3 Mon Sep 17 00:00:00 2001 From: Robert Lude Date: Tue, 27 Jan 2015 12:49:05 -0500 Subject: [PATCH 3/5] Added shortcodes as a function of id and implemented redirect via goto action on urls controller --- .../app/controllers/.urls_controller.rb.swp | Bin 0 -> 12288 bytes source/app/controllers/urls_controller.rb | 5 +++++ source/app/models/url.rb | 12 ++++++++++++ source/app/validators/.url_validator.rb.swp | Bin 12288 -> 12288 bytes source/app/validators/url_validator.rb | 1 - source/app/views/urls/index.html.erb | 2 ++ source/config/routes.rb | 1 + source/db/seeds.rb | 3 +++ 8 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 source/app/controllers/.urls_controller.rb.swp diff --git a/source/app/controllers/.urls_controller.rb.swp b/source/app/controllers/.urls_controller.rb.swp new file mode 100644 index 0000000000000000000000000000000000000000..522b01b3431058ddde89b4c9ecddf12c7c2953cc GIT binary patch literal 12288 zcmeI2O^6&t6vt~t@N45of}rBz>7v=-rf1hg5}F7mlf?wG&O&yB5|^Q;yJn|NPgh%2 zy_3ag(7R&5V=i6<4;}<_3JPBICPnHn!*Gxmojc>iOqr$M)qd26~fMz$@St@CtYZyaHYU zuYgy;EAW3+Ko=A2ERws=CD?J-iLtA@^%t*zSHLUa74Qmp1-t@Y0k42pz$@St@CtYZ z?m-2jgt50CVC?G$5j_6?|K{KSmmXv69QYJ`1O$jd2OI@Qzqm*6z$fkWT`*bSZrliKtx3U~#)0$u^Hz}>0k8~>XUDij!`-IWm%7Zmc(Hne?OVYAbx^m4m z&L2`=iXrI%ClyC>ozfiEoa-49dA3eC$S6kk@o120PFae#^+1Ypbr3M?xe53+%t^1( zvdn^hnkUoM^e4kKS!v=@Tz4_f1+}a=;aFwXi{ev?v>-KzBurvB6ggpRS&)#p&y|)D zf>YGvdc=87QEU=c0X!8lU5hl1bT;xeQT2r!Mml&$!Qp!};88+hF6@y@o@;vFMndF< z4fHU>Fv$~W9_ubbLbS?+JVUwAY0N{~i4!Toocu zjFUvrnQDqNc0!|7;NF12RIIt1qQmpY=9lKlo(^rz+2Ap1+`e_fP3m=4E_sPZSOexR zBpU&jQb;vJN=Ld>A%$gLNMUk{dKvFRpj5Bd8rA-4m8`!IVl_hYOmU}aJ%022X7P?T z%qdRIkU3pz>RHA04>LA(F$vtMCS{ReVKfyXO8Zsj)st!XaNDF>qPH!X-BZ?^EyUu% zrS_55;_}k1HM-@|-BPBUk7{B6eU)O#jg~TlcG6}})9@&*T6mhcKoV{`bhqx+uBD%B zy7vm)nBZ207R@p-7PF^pu&>Y8Ypi9B8{*rXwDX1Z$6hp+`Yq$!Y{0QG8=B098JpM0 zZX4w?L(6uvA#Z+qv#&I#dq YNGbDmQSJKtJRP8eMUkad^Jwhs21Pru(EtDd literal 0 HcmV?d00001 diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index a71c040..335ece2 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -21,6 +21,11 @@ def new def edit end + def goto + id = Url.decode_shortcode params[:shortcode] + url = Url.find id + redirect_to url.destination + end # POST /urls # POST /urls.json def create diff --git a/source/app/models/url.rb b/source/app/models/url.rb index 1765792..3a9488e 100644 --- a/source/app/models/url.rb +++ b/source/app/models/url.rb @@ -1,3 +1,15 @@ class Url < ActiveRecord::Base validates :destination, url: true + + before_validation do + self.destination = "http://#{self.destination}" unless /^[a-z]+:/ =~ self.destination + end + + def shortcode + id.to_s(36) + end + + def self.decode_shortcode shortcode + shortcode.to_i(36) + end end diff --git a/source/app/validators/.url_validator.rb.swp b/source/app/validators/.url_validator.rb.swp index 1cd3fc3a4b593949fe84b433a733d3ac53c96419..803701b82d4e8765a4ea96f51dacd5ada049ef7e 100644 GIT binary patch delta 156 zcmZojXh;xCG6?hZRWR2xVE_UF1_rJ3$3x~DZ4~>(&nU53P~bB^?*?uL1|1%Vti)tN z1$n=6eg+0^AZ7&O(|imJ(}1`bh>d|*6vXFcVCVtjcR*e9fOr}ZR{*g&5E}xq0T6>s g`~)=lE%#zw$CLTnFL^Aoc^|4Ll4C2|yeH#8yE36KK+B zApQh3^%eJKMixu%$vQgblRs%&ZBEnCV769JC`-&KO;xZ}P|7GNDX`MlSFQ$&)G8^I i=H;Xo7c1z;MJMW3#cEr{=*Q^mE7;aSwM_2TF9iTy>NM5> diff --git a/source/app/validators/url_validator.rb b/source/app/validators/url_validator.rb index e6e62d3..f37c484 100644 --- a/source/app/validators/url_validator.rb +++ b/source/app/validators/url_validator.rb @@ -4,7 +4,6 @@ class UrlValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - value = "http://#{value}" unless /^[a-z]+:\/\// =~ value uri = URI(value) return record.errors.add attribute, "must use HTTP or HTTPS" unless ['http','https'].member? uri.scheme begin diff --git a/source/app/views/urls/index.html.erb b/source/app/views/urls/index.html.erb index a56da1e..ab02afe 100644 --- a/source/app/views/urls/index.html.erb +++ b/source/app/views/urls/index.html.erb @@ -3,6 +3,7 @@

Shortcode Destination
<%= url.shortcode %> <%= url.destination %> <%= link_to 'Show', url %> <%= link_to 'Edit', edit_url_path(url) %>
+ @@ -11,6 +12,7 @@ <% @urls.each do |url| %> + diff --git a/source/config/routes.rb b/source/config/routes.rb index ff8326b..e0c9db1 100644 --- a/source/config/routes.rb +++ b/source/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do resources :urls + get ':shortcode' => 'urls#goto' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/source/db/seeds.rb b/source/db/seeds.rb index 54ef583..f908ccb 100644 --- a/source/db/seeds.rb +++ b/source/db/seeds.rb @@ -8,6 +8,9 @@ Url.delete_all +Url.create! destination: "covermymeds.com" Url.create! destination: "https://www.google.com/" Url.create! destination: "https://www.stackoverflow.com/" Url.create! destination: "https://www.mint.com/" +Url.create! destination: "http://weather.com/" +Url.create! destination: "wikipedia.org" From b86886fe1f9ac96813e413891f94358dd1a0633f Mon Sep 17 00:00:00 2001 From: Robert Lude Date: Tue, 27 Jan 2015 13:02:33 -0500 Subject: [PATCH 4/5] Added click counting --- .../app/controllers/.urls_controller.rb.swp | Bin 12288 -> 12288 bytes source/app/controllers/urls_controller.rb | 2 ++ .../urls/.show.html.erb.swp} | Bin 12288 -> 12288 bytes source/app/views/urls/index.html.erb | 2 ++ source/app/views/urls/show.html.erb | 10 ++++++++++ .../20150127175240_add_count_to_urls.rb | 5 +++++ source/db/schema.rb | 3 ++- 7 files changed, 21 insertions(+), 1 deletion(-) rename source/app/{validators/.url_validator.rb.swp => views/urls/.show.html.erb.swp} (88%) create mode 100644 source/db/migrate/20150127175240_add_count_to_urls.rb diff --git a/source/app/controllers/.urls_controller.rb.swp b/source/app/controllers/.urls_controller.rb.swp index 522b01b3431058ddde89b4c9ecddf12c7c2953cc..3daac31cb8bf3509db936bed72c4303d0d7a2bf2 100644 GIT binary patch delta 322 zcmXBPF-QVo0LAeqRF-wALoN+{Zb!k9P((yEMK}l@M1)J)QBhh#c?~V+)Zoxk6tuKQ zi`>#yL)oSv1TBqjO}RBhf9Syv9=z$*3bjINdwL_AGjq<2NJ0uaZ|kM`M(QUXal>Sy z7q=#(Qho4$buW{q5@biE1&m`11NgS2E}D47J#Mgq-w~;YHUjuK#2$9BhFRFK@HH&m zViP&o_=rg@G-Jx^nev1OT;Uu(^610oko1l=T6n=34zP-4ETPM-4z6*5dg!Jz(Xe4Z j_D?t`#nYX%4DpZ4j#u9IN?TsVKdPFkMUx4O&RygW!z(=j delta 289 zcmXBPu}cC09L4c>;Bk7LsR)A6aElU1QXsfExi!QMN}x0xfx%NZU(vUl;MS$ z^hYY$vZNsXfAuai?oq$KEY+}%JRHoE(mTd@#sLag!KWkLVu&l8;|#~xg@+2Vcv+JA z=)uFUEk&5)4O3eUO(>7J!w@GZA%>p>4-sL88(iQJd+5T!7n7cFk02fzv9@J)7YR?- L9?WpRd~N&zgzq&d diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index 335ece2..783b53f 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -24,6 +24,8 @@ def edit def goto id = Url.decode_shortcode params[:shortcode] url = Url.find id + url.click_count += 1 + url.save! redirect_to url.destination end # POST /urls diff --git a/source/app/validators/.url_validator.rb.swp b/source/app/views/urls/.show.html.erb.swp similarity index 88% rename from source/app/validators/.url_validator.rb.swp rename to source/app/views/urls/.show.html.erb.swp index 803701b82d4e8765a4ea96f51dacd5ada049ef7e..6ad793f15497999ebba83cffd70f9c9f0993a930 100644 GIT binary patch literal 12288 zcmeI&J#W)M7zgkRth|Ye6&;SM7K!=-r3#=lLUkFC8X?gav01Kt)TbOib3PNH68ID_ zz{00s;0v%bGSHzcd<&j4K_$?_hE()-^v~J%@?8Aeta!V#b$7tK;dMsGImWslKWr@B zTx1tcwV2g)CVl^3j%cvDHMqC_U~Qwb)D5*s6Mg84=S@KonM~4`DWg2mS{6I95-ZB- zr;#>=6qTuMDn(M3;)#-@O4L?YqRP!E%-tg$O1ph%b#y`i0!Iqe?%~3X(`@nD)sCOj z4lePT^P5Lb#{dK%009U<00Izz00bcL{|UIV#a_^DotWI(%%pAo(oQbXKmY;|fB*y_ z009U<00Izz00ba#3@Os4dpfE6~$8KDQ!wX zxkx!l`AT)ZQ1&VBC{5jC=z^aN0SG_<0uX=z1Rwwb2tWV=|3IKOyUevJhOskzZY4>F zb62>33p_3pm-E>^o0@PZQ;sG`YGRtzOq#zYTM}DBX)f1Cz)}w_#FAv(gRz9v}<-*b$XHuib2VJ?TTs%@Pk4t0SShRPW z?da8Y>r#5@eH=`0x9$gIL7$9o-e3E}zD=EswpB&qlqsg`tR4*fR-@!9TMi22R4RkM LC-{C&6&jXr97wM? literal 12288 zcmeI2-D*=o5XU#(T0c+(FUNS1D)A(3l}b>H#!3rKTbi^8B4KmRGAd)(bqTPXSh zUiVU8!E1d0FTC(U{J8K1boS)KC;>(ALYaksx4UO%XZAN!XvpbQcUBv)7+e)JP787Q z{oB^d`2}%qrXY;UBJ}&e81=^8oyJD<{#vVgZ81iO6JP?<31n{f%JmasVScXa z4K5Zhz}Yj~(`|W|2`~XBzyz286JP>NfC(^x|B`@93*s5w?AY*j=ZACQr+K*Ki3u

Iys*8sR;17dZI~ggR%^A@B!5J|FZ(?5#6}5vOP87C$4mnmMKI2`48>4qD$0|k zPzGh#!w7b@83dIs)`^cgj@#7XBXoJVGPL48>de(@!!=Ebrq$~D&h51_jF$C{_4Tdg z=5mX+Htr2=*OB~ITCL4HGTK6$>cnCh#=9#hBQ(u}#L4|hk4>YU4zUUVQi?3W)DpfvP5|cGRw0KLV6=RaJn7C4Y*pZ z4if=LGdE!sCpN{kuUPBzNs; z>&k<4v>Du{gWWjEgZ@^l^)u^$@&R9UpceCUbBy&t$tP4Q=aVfx4EosbpjnzoJdZ{9 zpaiMsXQ}x!^k5>~9rY`=eGktL{yfW*tv<9Cp^{X@ZKHNZugO#SZig=3!v+;pQUB5j U

+ @@ -13,6 +14,7 @@ <% @urls.each do |url| %> + diff --git a/source/app/views/urls/show.html.erb b/source/app/views/urls/show.html.erb index 83a137d..0734650 100644 --- a/source/app/views/urls/show.html.erb +++ b/source/app/views/urls/show.html.erb @@ -5,5 +5,15 @@ <%= @url.destination %>

+

+ Shortenened URL: + <%= "#{request.protocol}#{request.host_with_port}/#{@url.shortcode}" %> +

+ +

+ Clicks: + <%= @url.click_count %> +

+ <%= link_to 'Edit', edit_url_path(@url) %> | <%= link_to 'Back', urls_path %> diff --git a/source/db/migrate/20150127175240_add_count_to_urls.rb b/source/db/migrate/20150127175240_add_count_to_urls.rb new file mode 100644 index 0000000..4b3d247 --- /dev/null +++ b/source/db/migrate/20150127175240_add_count_to_urls.rb @@ -0,0 +1,5 @@ +class AddCountToUrls < ActiveRecord::Migration + def change + add_column :urls, :click_count, :integer, default: 0 + end +end diff --git a/source/db/schema.rb b/source/db/schema.rb index f78f212..e2ce234 100644 --- a/source/db/schema.rb +++ b/source/db/schema.rb @@ -11,12 +11,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150126204532) do +ActiveRecord::Schema.define(version: 20150127175240) do create_table "urls", force: true do |t| t.string "destination" t.datetime "created_at" t.datetime "updated_at" + t.integer "click_count", default: 0 end end From cbb0b7138d550646d0d64e9916fc6cdeff62a90d Mon Sep 17 00:00:00 2001 From: Robert Lude Date: Tue, 27 Jan 2015 13:38:02 -0500 Subject: [PATCH 5/5] Moved short link method to URL, took advantage of it in views --- source/.gitignore | 4 ++++ source/app/controllers/.urls_controller.rb.swp | Bin 12288 -> 0 bytes source/app/models/url.rb | 7 +++++++ source/app/views/urls/.show.html.erb.swp | Bin 12288 -> 0 bytes source/app/views/urls/index.html.erb | 2 +- source/app/views/urls/show.html.erb | 4 ++-- 6 files changed, 14 insertions(+), 3 deletions(-) delete mode 100644 source/app/controllers/.urls_controller.rb.swp delete mode 100644 source/app/views/urls/.show.html.erb.swp diff --git a/source/.gitignore b/source/.gitignore index 6a502e9..7b93474 100644 --- a/source/.gitignore +++ b/source/.gitignore @@ -14,3 +14,7 @@ # Ignore all logfiles and tempfiles. /log/*.log /tmp + +# Ignore vim tempfiles +*.swo +*.swp diff --git a/source/app/controllers/.urls_controller.rb.swp b/source/app/controllers/.urls_controller.rb.swp deleted file mode 100644 index 3daac31cb8bf3509db936bed72c4303d0d7a2bf2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&x;&I6vt~t{F(JfBp`TsaSxg3+Oum6iLFuNWJwmX?uP7!U|5Ep?wXy>_H?yX z)jL^@E_m?ZK@>#6|3LAmCl7ki^_D#*4p5yw9wYxRO8;pqxj&F5pCthW-8@}?5G40$uivSuh!Ed|C%)y!q0 z+O@%xogL?JMyG&NUz-{1{amKEKP4E@?2)qww!0)>l`xRUU8z2XL zumZYZ0UQJkFb;mco3RhTSuh70;JdpRy974D7vOX7F?bie0dnvnxE1_(Cu84&%it3D z61)t~gX7>?@C>*L{jPxbz+1rTc@wS&7qU~pDc}@v3OEJ+T?Kd=F~VB2SFI9NzsDf*;)oKzUdb&6A1Q?4gTq{%wrgrXSO$Aez1IVCaP);%fmm7d3}=Q`l$ zFee?MC7F2LIE@;`^ymCITCU?#T(>c<3~EVn!g??BBut{;7b#(Eo{^Ba zPnDJdf>YGtdcb)~L1+?I9z10-UJEqNYS!}AQFVpv2iiNY;P4gd@gO2U74|G8Pc^-2 zBOxk>^>jbMFv%lm9_ltiLbS?+JV~k0amanzhZJcLC{lSCa;3U?0yg0(x~!Rsbu~65UUoFCyJMvR^zwN zZx-)h%ba3$lFYGLQ_mu!EBqJ5n?GIFt=?G+l?_9NTWF*W*bQ5DT05!P#3s7E)m@mx;VPO-j@I(% z5t<%8*wPm%E;@Z2mBxshM&7PkdfEBU)lGgGZcGj8Y~^aYhhUNn~KE#utm(~&V-deN2{>(|KcE>&iR&R))j?*YHC-B+!B<;nQzaP&#u zF4{Z!<@cia9{FTNLG!RZ2bW?SmmfiN-(?mifl}rpq*~Sad3u_TW*JIjdrWO-e*z95 By7>SA diff --git a/source/app/models/url.rb b/source/app/models/url.rb index 3a9488e..37a61a0 100644 --- a/source/app/models/url.rb +++ b/source/app/models/url.rb @@ -1,3 +1,6 @@ + +require 'socket' + class Url < ActiveRecord::Base validates :destination, url: true @@ -12,4 +15,8 @@ def shortcode def self.decode_shortcode shortcode shortcode.to_i(36) end + + def shortlink + "http://localhost:3000/#{shortcode}" + end end diff --git a/source/app/views/urls/.show.html.erb.swp b/source/app/views/urls/.show.html.erb.swp deleted file mode 100644 index 6ad793f15497999ebba83cffd70f9c9f0993a930..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&J#W)M7zgkRth|Ye6&;SM7K!=-r3#=lLUkFC8X?gav01Kt)TbOib3PNH68ID_ zz{00s;0v%bGSHzcd<&j4K_$?_hE()-^v~J%@?8Aeta!V#b$7tK;dMsGImWslKWr@B zTx1tcwV2g)CVl^3j%cvDHMqC_U~Qwb)D5*s6Mg84=S@KonM~4`DWg2mS{6I95-ZB- zr;#>=6qTuMDn(M3;)#-@O4L?YqRP!E%-tg$O1ph%b#y`i0!Iqe?%~3X(`@nD)sCOj z4lePT^P5Lb#{dK%009U<00Izz00bcL{|UIV#a_^DotWI(%%pAo(oQbXKmY;|fB*y_ z009U<00Izz00ba#3@Os4dpfE6~$8KDQ!wX zxkx!l`AT)ZQ1&VBC{5jC=z^aN0SG_<0uX=z1Rwwb2tWV=|3IKOyUevJhOskzZY4>F zb62>33p_3pm-E>^o0@PZQ;sG`YGRtzOq#zYTM}DBX)f1Cz)}w_#FAv(gRz9v}<-*b$XHuib2VJ?TTs%@Pk4t0SShRPW z?da8Y>r#5@eH=`0x9$gIL7$9o-e3E}zD=EswpB&qlqsg`tR4*fR-@!9TMi22R4RkM LC-{C&6&jXr97wM? diff --git a/source/app/views/urls/index.html.erb b/source/app/views/urls/index.html.erb index f3fa4c7..27415c1 100644 --- a/source/app/views/urls/index.html.erb +++ b/source/app/views/urls/index.html.erb @@ -15,7 +15,7 @@ - + diff --git a/source/app/views/urls/show.html.erb b/source/app/views/urls/show.html.erb index 0734650..2de2cca 100644 --- a/source/app/views/urls/show.html.erb +++ b/source/app/views/urls/show.html.erb @@ -2,12 +2,12 @@

Destination: - <%= @url.destination %> + <%= link_to @url.destination, @url.destination %>

Shortenened URL: - <%= "#{request.protocol}#{request.host_with_port}/#{@url.shortcode}" %> + <%= link_to @url.shortlink, @url.shortlink %>

Shortcode Destination
<%= url.shortcode %> <%= url.destination %> <%= link_to 'Show', url %> <%= link_to 'Edit', edit_url_path(url) %>
ShortcodeClicks Destination
<%= url.shortcode %><%= url.click_count %> <%= url.destination %> <%= link_to 'Show', url %> <%= link_to 'Edit', edit_url_path(url) %>
<%= url.shortcode %> <%= url.click_count %><%= url.destination %><%= link_to url.destination, url.destination %> <%= link_to 'Show', url %> <%= link_to 'Edit', edit_url_path(url) %> <%= link_to 'Destroy', url, method: :delete, data: { confirm: 'Are you sure?' } %>