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

completed onboarding activity #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 0 additions & 1 deletion source/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,46 @@
*= require_tree .
*= require_self
*/
$cmm_orange: #F59222;
$cmm_pink: #B31944;
$cmm_blue: #F8AD59;

html, body, h1, .container, table{
padding:0;
margin:0 auto;
border-style:none;
text-align:center;
}
h3{
color: $cmm_blue;
text-align: center;
background-color: $cmm_pink;
border-radius:5em;
word-wrap: break-word;
}

header, footer{
width:100%;
text-align:center;
line-height:3em;
color:white;
font-family: monospace;
}

table, td{
border-style:groove;
}

a{
text-decoration:none;
color:$cmm_pink;
}

header{
background-color: $cmm_orange;
}
footer{
background-color: $cmm_pink;
position:fixed;
top:75vh;
}
31 changes: 31 additions & 0 deletions source/app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
class UrlsController < ApplicationController
#GET urls
#def index
#@urls = Url.all #rake db:reset db:migrate
#end

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

#POST urls
def create
check = Url.find_by(:address => params[:url][:address])
if !check
@url = Url.create(:address => params[:url][:address])
end
flash[:error] = @url.errors.full_messages.to_sentence
redirect_to root_path
end

def move
@url = Url.where :bitly_clone => params[:short]
Copy link
Member

Choose a reason for hiding this comment

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

Url.find_by(:bitly_clone => params[:short]) gives you either the found record or nil. That simplifies the later code a little, since it isn't wrapped in an array.

if @url[0]
@url[0].increment!(:click_count)
redirect_to @url[0].address
else
redirect_to urls_path
end

end
end
18 changes: 18 additions & 0 deletions source/app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'uri'
class Url < ActiveRecord::Base
before_save :bitly_generate, :unless => :bitly_clone
validates :address, :if => :validate_url, presence: true, uniqueness: true, length: {minimum: 1}, :format => /\Ahttp[s]?:\/\/\S+/
validates :bitly_clone, uniqueness: true

def bitly_generate
rando_arr = ('a'..'z').to_a + ('A'..'Z').to_a
Copy link
Member

Choose a reason for hiding this comment

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

This could be a constant.

#bitly_link = "https://cmm.ly/"+rando_arr.sample(6).join
bitly_link = rando_arr.sample(8).join
self.bitly_clone = bitly_link
end

def validate_url
!!URI.regexp(['http','https'])
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand how this is being used. Doesn't this always return true?

end

end
23 changes: 15 additions & 8 deletions source/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<title>Source</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<head>
<title>Source</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<body>
<header><h1><%= link_to "CMMly", root_path %></h1></header>
<% if flash[:error] %>
<h3><%= flash[:error] %></h3>
<% end %>
<div class="container">

<%= yield %>
<%= yield %>

</body>
</div>
<footer>&copy; 2017</footer>
</body>
</html>
16 changes: 16 additions & 0 deletions source/app/views/urls/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>CMMly Keys</h2>
<table>
<tr>
<th>URL</th>
<th>CMMly URL</th>
</tr>
<% if @urls %>
<% @urls.each do |u|%>
<tr>
<td><%= u.address %></td>
<td><%= link_to "https://cmm.ly/#{u.bitly_clone}", move_url_path(:short => u.bitly_clone)%></td>
<td>Clicks: <%= u.click_count %></td>
</tr>
<% end %>
</table>
<% end %>
25 changes: 25 additions & 0 deletions source/app/views/urls/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h1>Generate CMMly URL</h1>

<%= form_for @url, url: urls_path do |u| %>
<p><%= u.label :address %></p>
<p><%= u.text_field :address %></p>
<p><%= u.submit %></p>
<% end %>
<br>
<% if @urls %>
<h2>CMMly Keys</h2>
<table>
<tr>
<th>URL</th>
<th>CMMly URL</th>
<th>Clicks</th>
</tr>
<% @urls.each do |u|%>
<tr>
<td><%= u.address %></td>
<td><%= link_to "https://cmm.ly/#{u.bitly_clone}", move_url_path(:short => u.bitly_clone)%></td>
<td>Clicks: <%= u.click_count %></td>
</tr>
<% end %>
</table>
<% end %>
9 changes: 9 additions & 0 deletions source/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
Rails.application.routes.draw do
#get '/urls/new' => 'urls#new'
#post '/urls' => 'urls#create'
#get '/urls' => 'urls#index'
root 'urls#new'
get '/urls/:short' => 'urls#move', :as => "move_url"
resources :urls, :except => [:update, :destroy, :edit, :show]



# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
10 changes: 10 additions & 0 deletions source/db/migrate/20170118215020_create_urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateUrls < ActiveRecord::Migration
def change
create_table :urls do |t|
t.string :address
t.string :bitly_clone

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions source/db/migrate/20170119162045_add_click_count_to_url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddClickCountToUrl < ActiveRecord::Migration
def change
add_column :urls, :click_count, :integer
end
end
5 changes: 5 additions & 0 deletions source/db/migrate/20170119180835_change_click_countin_urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeClickCountinUrls < ActiveRecord::Migration
def change
change_column :urls, :click_count, :integer, :default => 0
end
end
24 changes: 24 additions & 0 deletions source/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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: 20170119180835) do

create_table "urls", force: true do |t|
t.string "address"
t.string "bitly_clone"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "click_count", default: 0
end

end
33 changes: 33 additions & 0 deletions source/spec/controllers/urls_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'rails_helper'

RSpec.describe UrlsController, :type => :controller do

describe "GET index" do
it "returns http success" do
get :index
expect(response).to have_http_status(:success)
end
end

describe "GET new" do
it "returns http success" do
get :new
expect(response).to have_http_status(:success)
end
end

describe "GET create" do
it "returns http success" do
get :create
expect(response).to have_http_status(:success)
end
end

describe "GET move" do
it "returns http success" do
get :move
expect(response).to have_http_status(:success)
end
end

end
15 changes: 15 additions & 0 deletions source/spec/helpers/urls_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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
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
5 changes: 5 additions & 0 deletions source/spec/views/urls/bitly_path.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe "urls/bitly_path.html.erb", :type => :view do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions source/spec/views/urls/create.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe "urls/create.html.erb", :type => :view do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions source/spec/views/urls/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe "urls/index.html.erb", :type => :view do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions source/spec/views/urls/new.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe "urls/new.html.erb", :type => :view do
pending "add some examples to (or delete) #{__FILE__}"
end