feat: ✨ Links controller
Show method, Home page, redirection validation LinksController show method
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class LinksController < ApplicationController
|
||||
def home
|
||||
render 'links/home'
|
||||
end
|
||||
|
||||
def show
|
||||
@link = Link.find_by_slug(link_params[:slug])
|
||||
|
||||
if @link.nil?
|
||||
render file: "#{Rails.root}/public/404", status: :not_found
|
||||
else
|
||||
@link.update(click_counter: @link.click_counter + 1)
|
||||
redirect_to @link.url
|
||||
end
|
||||
end
|
||||
|
||||
def link_params
|
||||
params.permit(:slug)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,4 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module LinksHelper
|
||||
end
|
||||
@@ -5,7 +5,6 @@ class Link < ApplicationRecord
|
||||
validates_uniqueness_of :slug
|
||||
|
||||
validates :url, format: { with: URI::DEFAULT_PARSER.make_regexp, message: 'invalid format' }
|
||||
|
||||
validates_length_of :url, within: 3..30_000, on: :create, message: 'max length is 30000'
|
||||
|
||||
before_validation :generate_slug
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
<%= content_tag :div, nil, data: {controller: "hello"} do %>
|
||||
<div class="btn btn-primary">Say Hello</div>
|
||||
<% end %>
|
||||
@@ -1,5 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
Rails.application.routes.draw do
|
||||
root 'links#home'
|
||||
|
||||
get '/:slug', to: 'links#show', as: :short
|
||||
end
|
||||
|
||||
+12
-12
@@ -1,5 +1,3 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# 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.
|
||||
@@ -12,16 +10,18 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20_210_613_145_459) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension 'plpgsql'
|
||||
ActiveRecord::Schema.define(version: 2021_06_13_145459) do
|
||||
|
||||
create_table 'links', force: :cascade do |t|
|
||||
t.string 'slug', null: false
|
||||
t.text 'url'
|
||||
t.integer 'click_counter', default: 0
|
||||
t.datetime 'created_at', null: false
|
||||
t.datetime 'updated_at', null: false
|
||||
t.index ['slug'], name: 'index_links_on_slug'
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
create_table "links", force: :cascade do |t|
|
||||
t.string "slug", null: false
|
||||
t.text "url"
|
||||
t.integer "click_counter", default: 0
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["slug"], name: "index_links_on_slug"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
class LinksControllerTest < ActionDispatch::IntegrationTest
|
||||
test 'Should get Home page' do
|
||||
get '/'
|
||||
assert_response :success, 'Not rendered home page'
|
||||
end
|
||||
|
||||
test 'Should return 404 for unavailable slug' do
|
||||
get '/test'
|
||||
assert_response :not_found, 'Not rendered not found'
|
||||
end
|
||||
|
||||
test 'Should redirect from slug to url' do
|
||||
link = links(:one)
|
||||
slug = link.slug
|
||||
get short_url(slug: slug)
|
||||
|
||||
assert_redirected_to link.url, 'Not redirected to link url'
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user