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 +++ 9 files changed, 131 insertions(+) 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 (limited to 'app') 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 -- cgit 1.4.1