From c650bee03937bd0e741e701f1fe0bfe5cf8e040e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 12 Aug 2018 07:10:16 -0400 Subject: Add link entry type --- app/assets/stylesheets/admin/layout.scss | 10 ++++++ app/assets/stylesheets/main/records.scss | 4 +++ app/controllers/admin/links_controller.rb | 52 +++++++++++++++++++++++++++++++ app/models/link.rb | 15 +++++++++ app/views/admin/links/_form.html.haml | 26 ++++++++++++++++ app/views/admin/links/edit.html.haml | 3 ++ app/views/admin/links/index.html.haml | 14 +++++++++ app/views/admin/links/new.html.haml | 3 ++ app/views/layouts/admin.html.haml | 4 +++ config/routes.rb | 2 ++ db/migrate/20180811215146_create_links.rb | 10 ++++++ db/schema.rb | 9 +++++- 12 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 app/controllers/admin/links_controller.rb create mode 100644 app/models/link.rb create mode 100644 app/views/admin/links/_form.html.haml create mode 100644 app/views/admin/links/edit.html.haml create mode 100644 app/views/admin/links/index.html.haml create mode 100644 app/views/admin/links/new.html.haml create mode 100644 db/migrate/20180811215146_create_links.rb diff --git a/app/assets/stylesheets/admin/layout.scss b/app/assets/stylesheets/admin/layout.scss index 220dcd5..b2c561b 100644 --- a/app/assets/stylesheets/admin/layout.scss +++ b/app/assets/stylesheets/admin/layout.scss @@ -111,6 +111,16 @@ body { } } + .url-field { + label { + display: none; + } + + input { + width: 100%; + } + } + .slug-field { display: flex; font-size: 0.75em; diff --git a/app/assets/stylesheets/main/records.scss b/app/assets/stylesheets/main/records.scss index 47fdfa3..a8b7754 100644 --- a/app/assets/stylesheets/main/records.scss +++ b/app/assets/stylesheets/main/records.scss @@ -57,6 +57,10 @@ &.entry-type-stream, &.entry-type-update { background-color: #98FB98; } + + &.entry-type-link { + background-color: #FFCC99; + } } &.entry-tag { diff --git a/app/controllers/admin/links_controller.rb b/app/controllers/admin/links_controller.rb new file mode 100644 index 0000000..bda0d49 --- /dev/null +++ b/app/controllers/admin/links_controller.rb @@ -0,0 +1,52 @@ +class Admin::LinksController < Admin::AdminController + before_action :set_section + + def index + @links = Link.order(created_at: :desc) + end + + def new + @link = Link.new + end + + def create + @link = Link.new(link_params) + + if @link.save + flash.notice = "Link created successfully!" + + render :edit + else + flash.alert = "Error creating link." + + render :new + end + end + + def edit + @link = Link.find(params[:id]) + end + + def update + @link = Link.find(params[:id]) + + if @link.update_attributes(link_params) + flash.notice = "Link updated successfully!" + else + flash.alert = "Error updating link." + end + + render :edit + end + + private + + def link_params + params.require(:link).permit(:title, :url, :tag_list, records_attributes: [:description, :_destroy]) + end + + def set_section + @section = "links" + end + +end diff --git a/app/models/link.rb b/app/models/link.rb new file mode 100644 index 0000000..81ffa2b --- /dev/null +++ b/app/models/link.rb @@ -0,0 +1,15 @@ +class Link < ApplicationRecord + include Recordable + + acts_as_taggable + + validates :title, :url, presence: true + + def path + url + end + + def taggable + self + end +end diff --git a/app/views/admin/links/_form.html.haml b/app/views/admin/links/_form.html.haml new file mode 100644 index 0000000..c624a3d --- /dev/null +++ b/app/views/admin/links/_form.html.haml @@ -0,0 +1,26 @@ +%fieldset#content + .title-field + = f.label :title + = f.text_field :title, placeholder: "Title" + .url-field + = f.label :url + = f.text_field :url, placeholder: "URL" +%fieldset#details + - if f.object.errors.any? + #errors.details-module + %h3 Error! + %ul + - f.object.errors.full_messages.each do |error| + %li= error + .details-module + .tags-field + = f.label :tag_list, "Tags" + = f.text_field :tag_list, type: :tags, value: f.object.tag_list.join(",") + .details-module + = f.fields_for :records, Record.new do |builder| + .should-create-record-field + = builder.check_box :_destroy, {checked: false}, "0", "1" + = builder.label :_destroy, "Create record?" + .record-description-field + = builder.text_area :description, placeholder: "record text" + .details-module= f.submit diff --git a/app/views/admin/links/edit.html.haml b/app/views/admin/links/edit.html.haml new file mode 100644 index 0000000..1c6c529 --- /dev/null +++ b/app/views/admin/links/edit.html.haml @@ -0,0 +1,3 @@ +- title "Editing #{@link.title}" += form_for @link, url: admin_link_url(@link), html: { id: "entry-form" } do |f| + = render partial: "form", locals: { f: f } diff --git a/app/views/admin/links/index.html.haml b/app/views/admin/links/index.html.haml new file mode 100644 index 0000000..e66d9b7 --- /dev/null +++ b/app/views/admin/links/index.html.haml @@ -0,0 +1,14 @@ +- title "Links" +%table#entries + %tr + %th Title + %th Date created + %th + - @links.each do |link| + %tr{ class: cycle("even", "odd") } + %td= link.title + %td= link.created_at.strftime("%B %d, %Y, %l:%M%P") + %td + %ul.admin-actions + %li= link_to "Edit", edit_admin_link_url(link) + %li= link_to "Show", link.url diff --git a/app/views/admin/links/new.html.haml b/app/views/admin/links/new.html.haml new file mode 100644 index 0000000..accdfd9 --- /dev/null +++ b/app/views/admin/links/new.html.haml @@ -0,0 +1,3 @@ +- title "New link" += form_for @link, url: admin_links_url, html: { id: "entry-form" } do |f| + = render partial: "form", locals: { f: f } diff --git a/app/views/layouts/admin.html.haml b/app/views/layouts/admin.html.haml index bc07335..4bfe60a 100644 --- a/app/views/layouts/admin.html.haml +++ b/app/views/layouts/admin.html.haml @@ -29,5 +29,9 @@ = link_to "Streams", admin_streams_url, class: "major-link" %ul.minors %li.minor= link_to "New stream", new_admin_stream_url + %li{major_sidebar_attrs("links")} + = link_to "Links", admin_links_url, class: "major-link" + %ul.minors + %li.minor= link_to "New link", new_admin_link_url #main = yield diff --git a/config/routes.rb b/config/routes.rb index 2d0a4ec..512c319 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,8 @@ Rails.application.routes.draw do resources :streams, except: [:show] do resources :updates, except: [:index, :show] end + + resources :links, except: [:show] end mount Ckeditor::Engine => '/ckeditor' diff --git a/db/migrate/20180811215146_create_links.rb b/db/migrate/20180811215146_create_links.rb new file mode 100644 index 0000000..0e0ca29 --- /dev/null +++ b/db/migrate/20180811215146_create_links.rb @@ -0,0 +1,10 @@ +class CreateLinks < ActiveRecord::Migration[5.1] + def change + create_table :links do |t| + t.string :title, null: false + t.string :url, null: false + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 192c9c1..58891d3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180707142420) do +ActiveRecord::Schema.define(version: 20180811215146) do create_table "blogs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "title" @@ -35,6 +35,13 @@ ActiveRecord::Schema.define(version: 20180707142420) do t.index ["type"], name: "index_ckeditor_assets_on_type" end + create_table "links", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t| + t.string "title", null: false + t.string "url", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "pokeviewer_abilities", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t| t.string "name", null: false t.string "description", null: false -- cgit 1.4.1