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
3 changes: 2 additions & 1 deletion rails/cropper/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use bootstrap
gem 'bootstrap-sass', '3.3.7'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
Expand Down
6 changes: 6 additions & 0 deletions rails/cropper/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ GEM
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
arel (8.0.0)
autoprefixer-rails (8.1.0)
execjs
bindex (0.5.0)
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
builder (3.2.3)
byebug (10.0.0)
capybara (2.18.0)
Expand Down Expand Up @@ -175,6 +180,7 @@ PLATFORMS
ruby

DEPENDENCIES
bootstrap-sass (= 3.3.7)
byebug
capybara (~> 2.13)
coffee-rails (~> 4.2)
Expand Down
3 changes: 3 additions & 0 deletions rails/cropper/app/assets/javascripts/url_crops.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
55 changes: 55 additions & 0 deletions rails/cropper/app/assets/stylesheets/my_styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@import "bootstrap-sprockets";
@import "bootstrap";

@mixin box_sizing {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}

@mixin for_form_100_width{
margin-bottom: 15px;
width: 100%;
}

header {
a {
color: white;
&:hover {
color: blue;
}
}
small {
color: yellow;
float: right;
padding-top: 15px;
}

#logo {
float: left;
padding-top: 9px;
font-size: 1.7em;
font-weight: bold;
text-transform: uppercase;
letter-spacing: -1px;
color: white;

&:hover {
text-decoration: none;
color: white;
}
}
}


input, button {
border: 1px solid #bbb;
border-radius: 30px;
@include for_form_100_width;
@include box_sizing;
}

label {
@include for_form_100_width;
@include box_sizing;
}
3 changes: 3 additions & 0 deletions rails/cropper/app/assets/stylesheets/url_crops.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the UrlCrops controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
57 changes: 57 additions & 0 deletions rails/cropper/app/controllers/url_crops_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class UrlCropsController < ApplicationController

def new
@url = UrlCrop.new
end

def create

@url = UrlCrop.find_by(base_url: params[:url_crop][:base_url].downcase)
unless @url
@url = UrlCrop.new(url_params)
if @url.save
@url.short_url = url_generator
@url.save
end
end

render 'new'
#render plain: @url.short_url
#render plain: request.original_url
end

def must_be_redirected
url = request.original_url
if url.split('/').last.length == 5
url_r = UrlCrop.find_by(short_url: url.split('/').last)
redirect_to url_r.base_url
else
redirect_to root_path
end
end


def update
@url = UrlCrop.find_by(base_url: params[:url_crop][:base_url].downcase)
if @url.update_attributes(url_params)
render 'new'
end
end


private

def url_params
params.require(:url_crop).permit(:base_url)
end

def url_generator
symbols = ('a'..'z').to_a + ('A'..'Z').to_a
shorts = ''
5.times do
shorts << symbols[rand(symbols.length - 1)]
end
shorts
end

end
2 changes: 2 additions & 0 deletions rails/cropper/app/helpers/url_crops_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UrlCropsHelper
end
22 changes: 22 additions & 0 deletions rails/cropper/app/models/url_crop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class UrlCrop < ApplicationRecord
# started like web-url
# before_create :check
before_save :downcase_url, :find_obj
HTTP_REG_EXP = /\A(http|https):\/\/([a-zA-Z0-9]+[\.\-]{1})+[a-zA-Z0-9]{2,5}\//

validates :base_url, presence: true, length: { maximum: 255 },
format: { with: HTTP_REG_EXP }

private

def downcase_url
self.base_url = base_url.downcase
end

def find_obj
if UrlCrop.find_by(base_url: :base_url)
render plain: "exists"
end
end

end
11 changes: 11 additions & 0 deletions rails/cropper/app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<header class="navbar navbar-inverse">
<div class="container">
<%= link_to "Url cropper", root_path, id: 'logo'%>

<small>
Url cropper by
<a href = "https://github.com/AlexeySotskcov/" title="link to my github">Aleksei Sotskov</a>
</small>
</div>

</header>
7 changes: 5 additions & 2 deletions rails/cropper/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Cropper</title>
<title><%= yield(:title) %></title>
<%= csrf_meta_tags %>

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

<body>
<%= yield %>
<%= render "layouts/header" %>
<div class="container">
<%= yield %>
</div>
</body>
</html>
14 changes: 14 additions & 0 deletions rails/cropper/app/views/url_crops/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%= form_for @url do |f| %>

<%= f.label :base_url %>
<%= f.text_field :base_url%>

<%= f.label "Short link" %>
<%= f.text_field :short_url, readonly: true %>

<%= f.submit "Crop"%>

<%= button_tag "Reset", type: :reset %>


<% end %>
18 changes: 10 additions & 8 deletions rails/cropper/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
# For details on connection pooling, see Rails configuration guide<%= ENV['CROPPER_DATABASE_PASSWORD'] %>
# http://guides.rubyonrails.org/configuring.html#database-pooling<%= ENV['CROPPER_DATABASE_PASSWORD'] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: pguser
password: cropper
host: localhost
port: 5432

development:
<<: *default
database: cropper_development
database: pgcropper_development

# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
Expand Down Expand Up @@ -57,7 +61,7 @@ development:
# Do not set this db to the same as development or production.
test:
<<: *default
database: cropper_test
database: pgcropper_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
Expand All @@ -77,9 +81,7 @@ test:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
#<%= ENV['CROPPER_DATABASE_PASSWORD'] %>
production:
<<: *default
database: cropper_production
username: cropper
password: <%= ENV['CROPPER_DATABASE_PASSWORD'] %>
database: pgcropper_production
6 changes: 6 additions & 0 deletions rails/cropper/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root "url_crops#new"

resources :url_crops

# redirect if unknown url
get '*path', to: 'url_crops#must_be_redirected'
end
10 changes: 10 additions & 0 deletions rails/cropper/db/migrate/20180304065446_create_url_crops.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateUrlCrops < ActiveRecord::Migration[5.1]
def change
create_table :url_crops do |t|
t.text :base_url
t.string :short_url

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIndexToBaseUrl < ActiveRecord::Migration[5.1]
def change
add_index :url_crops, :base_url
end
end
26 changes: 26 additions & 0 deletions rails/cropper/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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: 20180304145624) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "url_crops", force: :cascade do |t|
t.text "base_url"
t.string "short_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["base_url"], name: "index_url_crops_on_base_url", unique: true
end

end
7 changes: 7 additions & 0 deletions rails/cropper/test/controllers/url_crops_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class UrlCropsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
9 changes: 9 additions & 0 deletions rails/cropper/test/fixtures/url_crops.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
base_url: MyText
short_url: MyString

two:
base_url: MyText
short_url: MyString
7 changes: 7 additions & 0 deletions rails/cropper/test/models/url_crop_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class UrlCropTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end