diff --git a/source/.DS_Store b/source/.DS_Store new file mode 100644 index 0000000..abf4292 Binary files /dev/null and b/source/.DS_Store differ diff --git a/source/Gemfile b/source/Gemfile index 9627b8b..fdd141d 100644 --- a/source/Gemfile +++ b/source/Gemfile @@ -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. diff --git a/source/app/.DS_Store b/source/app/.DS_Store new file mode 100644 index 0000000..6323a05 Binary files /dev/null and b/source/app/.DS_Store differ diff --git a/source/app/assets/javascripts/application.js b/source/app/assets/javascripts/application.js index d6925fa..1ca0e3b 100644 --- a/source/app/assets/javascripts/application.js +++ b/source/app/assets/javascripts/application.js @@ -12,5 +12,4 @@ // //= require jquery //= require jquery_ujs -//= require turbolinks //= require_tree . diff --git a/source/app/controllers/urls_controller.rb b/source/app/controllers/urls_controller.rb index ef26710..a7f8d33 100644 --- a/source/app/controllers/urls_controller.rb +++ b/source/app/controllers/urls_controller.rb @@ -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 + redirect_to @url.read_attribute('original') + end + + private + def url_params + params.require(:url).permit(:original) + end end diff --git a/source/app/models/.DS_Store b/source/app/models/.DS_Store new file mode 100644 index 0000000..eafdcad Binary files /dev/null and b/source/app/models/.DS_Store differ diff --git a/source/app/models/ur_lindex.rb b/source/app/models/ur_lindex.rb new file mode 100644 index 0000000..69f1f51 --- /dev/null +++ b/source/app/models/ur_lindex.rb @@ -0,0 +1,2 @@ +class UrLindex < ActiveRecord::Base +end diff --git a/source/app/models/url.rb b/source/app/models/url.rb new file mode 100644 index 0000000..c02e9da --- /dev/null +++ b/source/app/models/url.rb @@ -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/} + + + before_save :shortify + + private + def shortify + if self.shortened == nil + self.click_count = 0 + begin + self.shortened = "https://bit.ly/" + SecureRandom.hex(3) + end while Url.where(shortened: self.shortened).exists? + end + end +end diff --git a/source/app/views/layouts/application.html.erb b/source/app/views/layouts/application.html.erb index f946432..d5124e5 100644 --- a/source/app/views/layouts/application.html.erb +++ b/source/app/views/layouts/application.html.erb @@ -2,8 +2,8 @@ Source - <%= 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 %> diff --git a/source/app/views/urls/index.html.erb b/source/app/views/urls/index.html.erb new file mode 100644 index 0000000..4f62629 --- /dev/null +++ b/source/app/views/urls/index.html.erb @@ -0,0 +1,34 @@ +

Bitly Clone

+ + + + + + + + + <% @urls.each do |url| %> + + + + + + + <% end %> +
Destination:Short Url:Clicks:
<%= url.original %><%= link_to url.shortened, urls_click_link_path(url) %><%= url.click_count %><%= link_to 'Delete', url_path(url), method: :delete %>
+ +<%= form_for :url do |f| %> +

+ <%= f.label :'Enter Url:' %>
+ <%= f.text_field :original %> + <%= f.submit %> +

+<% end %> + +
+ <% if @url %> +

+ <%= "Please enter a valid URL starting with 'http://' or 'https://'" if @url.errors.any? %> +

+ <% end %> +
diff --git a/source/config/routes.rb b/source/config/routes.rb index 3f66539..733d8ab 100644 --- a/source/config/routes.rb +++ b/source/config/routes.rb @@ -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". diff --git a/source/db/.DS_Store b/source/db/.DS_Store new file mode 100644 index 0000000..0cbabab Binary files /dev/null and b/source/db/.DS_Store differ diff --git a/source/db/migrate/.DS_Store b/source/db/migrate/.DS_Store new file mode 100644 index 0000000..cf6553b Binary files /dev/null and b/source/db/migrate/.DS_Store differ diff --git a/source/db/migrate/20170609180109_create_urls.rb b/source/db/migrate/20170609180109_create_urls.rb new file mode 100644 index 0000000..e76c5fa --- /dev/null +++ b/source/db/migrate/20170609180109_create_urls.rb @@ -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 diff --git a/source/db/migrate/20170609192743_create_ur_lindices.rb b/source/db/migrate/20170609192743_create_ur_lindices.rb new file mode 100644 index 0000000..0eff0ae --- /dev/null +++ b/source/db/migrate/20170609192743_create_ur_lindices.rb @@ -0,0 +1,9 @@ +class CreateUrLindices < ActiveRecord::Migration + def change + create_table :ur_lindices do |t| + + add_index :urls, unique: true + + end + end +end diff --git a/source/db/schema.rb b/source/db/schema.rb new file mode 100644 index 0000000..0db76da --- /dev/null +++ b/source/db/schema.rb @@ -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 diff --git a/source/spec/models/original_and_count_spec.rb b/source/spec/models/original_and_count_spec.rb new file mode 100644 index 0000000..84cf38c --- /dev/null +++ b/source/spec/models/original_and_count_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe OriginalAndCount, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/source/spec/models/ur_lindex_spec.rb b/source/spec/models/ur_lindex_spec.rb new file mode 100644 index 0000000..29bbb69 --- /dev/null +++ b/source/spec/models/ur_lindex_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe UrLindex, :type => :model 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