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

Kenworthy #6

Open
wants to merge 3 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
5 changes: 5 additions & 0 deletions source/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ gem 'rails', '4.1.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'bootstrap-sass', '~> 3.2.0'
gem 'sass-rails', '~> 4.0.3'
gem 'autoprefixer-rails'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

gem 'haml'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

Expand Down
9 changes: 9 additions & 0 deletions source/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
autoprefixer-rails (3.1.2.20141016)
execjs
bootstrap-sass (3.2.0.2)
sass (~> 3.2)
builder (3.2.2)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
Expand All @@ -39,6 +43,8 @@ GEM
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.2.1)
haml (4.0.6)
tilt
hike (1.2.3)
i18n (0.6.11)
jbuilder (2.2.2)
Expand Down Expand Up @@ -125,7 +131,10 @@ PLATFORMS
ruby

DEPENDENCIES
autoprefixer-rails
bootstrap-sass (~> 3.2.0)
coffee-rails (~> 4.0.0)
haml
jbuilder (~> 2.0)
jquery-rails
rails (= 4.1.6)
Expand Down
15 changes: 0 additions & 15 deletions source/app/assets/stylesheets/application.css

This file was deleted.

29 changes: 29 additions & 0 deletions source/app/assets/stylesheets/application.css.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@import "bootstrap-sprockets"
@import "bootstrap"

.primary-body
margin: 0 auto
padding-top: 75px


.flash
border: solid 1px darken(green, 20%)
text-align: center
padding: 5px
color: white


.flash_notice
display: block
@extend .flash
background: green

.flash_alert
@extend .flash
background: red

table, th, td
border: 1px solid grey
text-align: left
padding: 5px
background-color: light-grey
69 changes: 69 additions & 0 deletions source/app/assets/stylesheets/scaffolds.css.scss
Original file line number Diff line number Diff line change
@@ -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;
}
}
84 changes: 84 additions & 0 deletions source/app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,86 @@
class UrlsController < ApplicationController
before_action :set_url, only: [:show, :stats, :edit, :update, :destroy]

# GET /urls
# GET /urls.json
def index
@urls = Url.all
end

# GET /urls/1
# GET /urls/1.json
def show
end

def expand
if @url = Url.find_by(short_url: params[:short_url])
redirect_to @url.long_url
else
redirect_to '/', alert: "URL not found!"
end
end

# GET /urls/new
def new
@url = Url.new
end

def stats
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
flash.now.alert = "Error creating URL!"
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(:long_url, :short_url, :name, :clicks)
end
end
24 changes: 24 additions & 0 deletions source/app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Url < ActiveRecord::Base
validates :long_url, format: { with: URI::regexp(%w(http https)), message: "Invalid URL!" }

before_save :shorten

validates :short_url, uniqueness: true
validates :name, presence: true


def shorten
p "========================Start shorten"
known_sites = { facebook: :fb, google: :goo}
if self.short_url.empty?
p "======================="
/https:\/\/(?:www.)(\w+).\w{3}\/(.*)((?:\?).*)?/ =~ self.long_url
p self.long_url
p $1
p $2
self.short_url = known_sites.has_key?($1) ? known_sites[$1] : $1[0..3]
$2 && self.short_url += $2[0..3]
end
end

end
14 changes: 0 additions & 14 deletions source/app/views/layouts/application.html.erb

This file was deleted.

16 changes: 16 additions & 0 deletions source/app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
!!! 5
%html
%head
%title Source
= stylesheet_link_tag 'application', { media: 'all', 'data-turbolinks-track' => true }
= javascript_include_tag 'application', { 'data-turbolinks-track' => true }
= csrf_meta_tags
%body

= render 'partials/nav'

.container.primary-body
- flash.each do |name, msg|
= content_tag :div, msg, :class => "flash_#{name}"

= yield
20 changes: 20 additions & 0 deletions source/app/views/partials/_nav.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%nav.navbar.navbar-default.navbar-fixed-top{ role: 'navigation' }
.container
.navbar-header
%button{ class: "navbar-toggle", "data-toggle" => "collapse", "data-target" => "#bs-example-navbar-collapse-1" }
%span.sr-only Toggle navigation
%span.icon-bar
%span.icon-bar
%span.icon-bar
%a{ class: "navbar-brand", href: "/" }
Super App
.collapse.navbar-collapse#bs-example-navbar-collapse-1
%ul.nav.navbar-nav.navbar-right
%li=link_to 'Home', root_url
- if @current_user
%li= link_to "Logged in as #{@current_user.email}", user_path(@current_user)

//%li= link_to "Logout", log_out_path
- else
//%li= link_to "Sign up", sign_up_path
//%li= link_to "login", log_in_path
33 changes: 33 additions & 0 deletions source/app/views/urls/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= form_for(@url) do |f| %>
<% if @url.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@url.errors.count, "error") %> prohibited this url from being saved:</h2>

<ul>
<% @url.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :long_url %><br>
<%= f.text_field :long_url %>
</div>
<div class="field">
<%= f.label :short_url %><br>
<%= f.text_field :short_url %>
</div>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :clicks %><br>
<%= f.number_field :clicks %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions source/app/views/urls/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing url</h1>

<%= render 'form' %>

<%= link_to 'Show', @url %> |
<%= link_to 'Back', urls_path %>
24 changes: 24 additions & 0 deletions source/app/views/urls/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%h1 Listing urls

%table
%thead
%tr
%th Name
%th Clicks
%th Short url
%th Long url
%th{colspan: "3"}
%tbody>
- @urls.each do |url|
%tr
//%td= link_to url.name, url_path(url.short_url)
%td= link_to url.clicks, url_stats_path(url)
//%td= link_to url.short_url, expand_url_path(url.short_url)
//%td= link_to url.long_url, expand_url_path(url.short_url)

%td= link_to 'Show', url
%td= link_to 'Edit', edit_url_path(url)
%td= link_to 'Destroy', url, method: :delete, data: { confirm: 'Are you sure?' }
%br

= link_to 'New Url', new_url_path
Loading