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
9 changes: 7 additions & 2 deletions source/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
gem 'rails'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
Expand All @@ -26,6 +26,12 @@ gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development

gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'

gem 'bcrypt'

gem 'devise'

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

Expand All @@ -38,4 +44,3 @@ gem 'spring', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'rspec-rails', group: [:development, :test]

58 changes: 57 additions & 1 deletion source/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
GIT
remote: git://github.com/sferik/rails_admin.git
revision: 2c8fa4a656c7f5b18c4d9b66c55079fd5ee30b77
specs:
rails_admin (0.8.1)
builder (~> 3.1)
coffee-rails (~> 4.0)
font-awesome-rails (>= 3.0, < 5)
haml (~> 4.0)
jquery-rails (>= 3.0, < 5)
jquery-ui-rails (~> 5.0)
kaminari (~> 0.14)
nested_form (~> 0.3)
rack-pjax (~> 0.7)
rails (>= 4.0, < 6)
remotipart (~> 1.0)
safe_yaml (~> 1.0)
sass-rails (>= 4.0, < 6)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -28,6 +47,7 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
bcrypt (3.1.10)
builder (3.2.2)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
Expand All @@ -36,9 +56,20 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.8.0)
devise (3.5.3)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
responders
thread_safe (~> 0.1)
warden (~> 1.2.3)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.2.1)
font-awesome-rails (4.5.0.0)
railties (>= 3.2, < 5.0)
haml (4.0.7)
tilt
hike (1.2.3)
i18n (0.6.11)
jbuilder (2.2.2)
Expand All @@ -47,13 +78,26 @@ GEM
jquery-rails (3.1.2)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
jquery-ui-rails (5.0.5)
railties (>= 3.2.16)
json (1.8.1)
kaminari (0.16.3)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
mail (2.6.1)
mime-types (>= 1.16, < 3)
mime-types (2.4.1)
mini_portile2 (2.0.0)
minitest (5.4.2)
multi_json (1.10.1)
nested_form (0.3.2)
nokogiri (1.6.7.1)
mini_portile2 (~> 2.0.0.rc2)
orm_adapter (0.5.0)
rack (1.5.2)
rack-pjax (0.8.0)
nokogiri (~> 1.5)
rack (~> 1.1)
rack-test (0.6.2)
rack (>= 1.0)
rails (4.1.6)
Expand All @@ -74,6 +118,9 @@ GEM
rake (10.3.2)
rdoc (4.1.2)
json (~> 1.4)
remotipart (1.2.1)
responders (1.1.2)
railties (>= 3.2, < 4.2)
rspec-core (3.1.6)
rspec-support (~> 3.1.0)
rspec-expectations (3.1.2)
Expand All @@ -90,6 +137,7 @@ GEM
rspec-mocks (~> 3.1.0)
rspec-support (~> 3.1.0)
rspec-support (3.1.2)
safe_yaml (1.0.4)
sass (3.2.19)
sass-rails (4.0.3)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -120,19 +168,27 @@ GEM
uglifier (2.5.3)
execjs (>= 0.3.0)
json (>= 1.8.0)
warden (1.2.4)
rack (>= 1.0)

PLATFORMS
ruby

DEPENDENCIES
bcrypt
coffee-rails (~> 4.0.0)
devise
jbuilder (~> 2.0)
jquery-rails
rails (= 4.1.6)
rails
rails_admin!
rspec-rails
sass-rails (~> 4.0.3)
sdoc (~> 0.4.0)
spring
sqlite3
turbolinks
uglifier (>= 1.3.0)

BUNDLED WITH
1.11.2
4 changes: 4 additions & 0 deletions source/app/assets/stylesheets/urls.css.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// 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/

.error {
background-color: red;
}
25 changes: 25 additions & 0 deletions source/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,29 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :authenticate_user!

# from http://stackoverflow.com/questions/2385799/how-to-redirect-to-a-404-in-rails
def render_404
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end

def s
puts request.original_url
@url = Url.find_by(shortened: params[:shortened])
if [email protected]?
redirect_to @url.unshortened
@url.increment_count
else
render_404
end
end

def login
render 'users/sign_in'
end
end
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 @@
require 'uri'

class UrlsController < ApplicationController

def index
@urls = Url.where(user: current_user)
end

def create
name = params[:name]
unshortened = params[:unshortened]

if name.nil? || unshortened.nil? || name.empty? || unshortened.empty? || !check_url(unshortened) || !user_signed_in?
flash[:create_error] = 'An error occured while creating shortened link. Please make sure link provided is valid and are logged in.'
else
Url.create!(unshortened: unshortened, name: name, user: current_user)
end

redirect_to '/urls'
end

private

def check_url(url)
val = true
val = false if url.empty?
val = false if /^(http\:\/\/|https\:\/\/)/.match(url).nil?
# from http://stackoverflow.com/questions/1805761/check-if-url-is-valid-ruby
val = false if (url =~ /\A#{URI::regexp(['http', 'https'])}\z/).nil?
val
end

end
1 change: 1 addition & 0 deletions source/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class UsersController < ApplicationController
has_many :urls
end
9 changes: 9 additions & 0 deletions source/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
module ApplicationHelper
def count_of_clicks(url)
return 0 if url.click_count.nil?
url.click_count
end

def link_name_helper(url)
return url.unshortened if url.name.nil?
url.name
end
end
31 changes: 31 additions & 0 deletions source/app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Url < ActiveRecord::Base
belongs_to :user

before_create :add_shortened

def increment_count
self.click_count = 0 if self.click_count.nil?
self.click_count += 1
self.save!
end


private

def add_shortened
letters = nil

until already_created(letters)
# from http://stackoverflow.com/questions/88311/how-best-to-generate-a-random-string-in-ruby
letters = (0...5).map { ('a'..'z').to_a[rand(26)] }.join
end

self.shortened = letters
end

def already_created(short_url)
puts short_url
return false if short_url.nil?
Url.find_by(shortened: short_url).nil?
end
end
50 changes: 50 additions & 0 deletions source/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'bcrypt'

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :urls

# attr_accessor :plain_password
# attr_accessor :new_password
#
# before_save :hash_password, :if=>:password_changed?
# before_create :hash_password
#
# def password_changed?
# if self.new_password
# return true
# else
# return false
# end
# end
#
# def changePassword(new_password)
# self.plain_password = new_password
# self.new_password = true
# self.save!
# end
#
# def self.authenticate(email, password)
# # Because we use bcrypt we can't do this query in one part, first
# # we need to fetch the potential user
# if user = find_by_email(email)
# # Then compare the provided password against the hashed one in the db.
# if BCrypt::Password.new(user.hashed_password).is_password? password
# # If they match we return the user
# return user
# end
# end
# # If we get here it means either there's no user with that email, or the wrong
# # password was provided. But we don't want to let an attacker know which.
# return nil
# end
#
# private
#
# def hash_password
# self.hashed_password = BCrypt::Password.create(self.plain_password)
# end
end
16 changes: 16 additions & 0 deletions source/app/views/devise/confirmations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Resend confirmation instructions</h2>

<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
</div>

<div class="actions">
<%= f.submit "Resend confirmation instructions" %>
</div>
<% end %>

<%= render "devise/shared/links" %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
3 changes: 3 additions & 0 deletions source/app/views/devise/mailer/password_change.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>Hello <%= @resource.email %>!</p>

<p>We're contacting you to notify you that your password has been changed.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>Hello <%= @resource.email %>!</p>

<p>Someone has requested a link to change your password. You can do this through the link below.</p>

<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
7 changes: 7 additions & 0 deletions source/app/views/devise/mailer/unlock_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p>Hello <%= @resource.email %>!</p>

<p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p>

<p>Click the link below to unlock your account:</p>

<p><%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %></p>
25 changes: 25 additions & 0 deletions source/app/views/devise/passwords/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>Change your password</h2>

<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>

<div class="field">
<%= f.label :password, "New password" %><br />
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em><br />
<% end %>
<%= f.password_field :password, autofocus: true, autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="actions">
<%= f.submit "Change my password" %>
</div>
<% end %>

<%= render "devise/shared/links" %>
Loading