From 42d9db526d3aef2e08848d6bc587feaf3700db42 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 7 Jul 2018 16:23:04 -0400 Subject: Added tags Blogs and streams can now be tagged. Records now show the appropriate tags for an entry. Updates work oddly, because their records show the stream's tags, since updates do not have tags themselves. refs #2 --- app/assets/javascripts/admin/records.coffee | 2 ++ app/assets/javascripts/application.js | 1 + app/assets/stylesheets/admin.css.scss | 1 + app/assets/stylesheets/admin/layout.scss | 8 ++++++++ app/assets/stylesheets/main/records.scss | 2 ++ app/controllers/admin/blogs_controller.rb | 2 +- app/controllers/admin/streams_controller.rb | 2 +- app/models/blog.rb | 6 ++++++ app/models/stream.rb | 6 ++++++ app/models/update.rb | 4 ++++ app/views/admin/blogs/_form.html.haml | 4 ++++ app/views/admin/streams/_form.html.haml | 4 ++++ app/views/records/_record.html.haml | 7 +++++++ app/views/records/index.html.haml | 8 +------- 14 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 app/views/records/_record.html.haml (limited to 'app') diff --git a/app/assets/javascripts/admin/records.coffee b/app/assets/javascripts/admin/records.coffee index 3a1ed51..7a11ce1 100644 --- a/app/assets/javascripts/admin/records.coffee +++ b/app/assets/javascripts/admin/records.coffee @@ -23,3 +23,5 @@ $(document).on "turbolinks:load", -> create_record_toggle($(this).prop("checked")) $(".published-field input[type=checkbox]").change -> published_field_toggle($(this).prop("checked")) + $("input[type=tags]").each -> + tagsInput(this) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index f902906..a81b3b2 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -14,4 +14,5 @@ //= require jquery_ujs //= require turbolinks //= require ckeditor/init +//= require tags-input //= require_tree ./admin diff --git a/app/assets/stylesheets/admin.css.scss b/app/assets/stylesheets/admin.css.scss index 5235c4b..95d1ed0 100644 --- a/app/assets/stylesheets/admin.css.scss +++ b/app/assets/stylesheets/admin.css.scss @@ -1,4 +1,5 @@ /* *= require normalize-rails + *= require tags-input *= require_tree ./admin */ diff --git a/app/assets/stylesheets/admin/layout.scss b/app/assets/stylesheets/admin/layout.scss index b825c25..220dcd5 100644 --- a/app/assets/stylesheets/admin/layout.scss +++ b/app/assets/stylesheets/admin/layout.scss @@ -196,6 +196,14 @@ body { } } +.tags-field { + label { + font-size: .75em; + display: block; + margin-bottom: 0.5em; + } +} + .record-description-field { display: none; margin-top: 1em; diff --git a/app/assets/stylesheets/main/records.scss b/app/assets/stylesheets/main/records.scss index f57dded..350fb16 100644 --- a/app/assets/stylesheets/main/records.scss +++ b/app/assets/stylesheets/main/records.scss @@ -31,6 +31,7 @@ .tags { margin: .25em; display: flex; + flex-wrap: wrap; padding-left: 0; li { @@ -58,6 +59,7 @@ } } + &.entry-tag { & + li { margin-left: 1em; } diff --git a/app/controllers/admin/blogs_controller.rb b/app/controllers/admin/blogs_controller.rb index 1035c12..e79dd81 100644 --- a/app/controllers/admin/blogs_controller.rb +++ b/app/controllers/admin/blogs_controller.rb @@ -56,7 +56,7 @@ class Admin::BlogsController < Admin::AdminController private def blog_params - params.require(:blog).permit(:title, :body, :slug, :published, records_attributes: [:description, :_destroy]) + params.require(:blog).permit(:title, :body, :slug, :published, :tag_list, records_attributes: [:description, :_destroy]) end def set_section diff --git a/app/controllers/admin/streams_controller.rb b/app/controllers/admin/streams_controller.rb index 86dec06..252ebfa 100644 --- a/app/controllers/admin/streams_controller.rb +++ b/app/controllers/admin/streams_controller.rb @@ -42,7 +42,7 @@ class Admin::StreamsController < Admin::AdminController private def stream_params - params.require(:stream).permit(:title, :body, :slug, records_attributes: [:description, :_destroy]) + params.require(:stream).permit(:title, :body, :slug, :tag_list, records_attributes: [:description, :_destroy]) end def set_section diff --git a/app/models/blog.rb b/app/models/blog.rb index 5742879..18f63f1 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -1,6 +1,8 @@ class Blog < ApplicationRecord include Recordable + acts_as_taggable + validates :title, presence: true validates :body, presence: true, if: :published validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published @@ -12,6 +14,10 @@ class Blog < ApplicationRecord "/says/#{slug}" end + def taggable + self + end + private def set_draft_title if self.title.blank? and not self.published diff --git a/app/models/stream.rb b/app/models/stream.rb index 1398b75..0773143 100644 --- a/app/models/stream.rb +++ b/app/models/stream.rb @@ -1,6 +1,8 @@ class Stream < ApplicationRecord include Recordable + acts_as_taggable + has_many :updates validates :title, presence: true @@ -9,4 +11,8 @@ class Stream < ApplicationRecord def path "/thinks/#{slug}" end + + def taggable + self + end end diff --git a/app/models/update.rb b/app/models/update.rb index 73c4911..01907d8 100644 --- a/app/models/update.rb +++ b/app/models/update.rb @@ -8,4 +8,8 @@ class Update < ApplicationRecord def path "/thinks/#{stream.slug}\#update-#{id}" end + + def taggable + stream + end end diff --git a/app/views/admin/blogs/_form.html.haml b/app/views/admin/blogs/_form.html.haml index 12f7a82..36925af 100644 --- a/app/views/admin/blogs/_form.html.haml +++ b/app/views/admin/blogs/_form.html.haml @@ -21,6 +21,10 @@ = link_to "View post", blog_url(f.object.slug_was), target: "entry-preview" - else = link_to "Preview post", admin_blog_url(f.object), target: "entry-preview" + .details-module + .tags-field + = f.label :tag_list, "Tags" + = f.text_field :tag_list, type: :tags, value: f.object.tag_list.join(",") .details-module .published-field = f.check_box :published diff --git a/app/views/admin/streams/_form.html.haml b/app/views/admin/streams/_form.html.haml index ce457cb..e04a3fb 100644 --- a/app/views/admin/streams/_form.html.haml +++ b/app/views/admin/streams/_form.html.haml @@ -15,6 +15,10 @@ %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 diff --git a/app/views/records/_record.html.haml b/app/views/records/_record.html.haml new file mode 100644 index 0000000..80a365c --- /dev/null +++ b/app/views/records/_record.html.haml @@ -0,0 +1,7 @@ +%li + %span.description= link_to record.description, record.recordable.path + %ul.tags + %li.record-date= record.created_at.strftime("%m.%d.%y") + %li.entry-type{ class: "entry-type-#{record.recordable_type.downcase}" }= record.recordable_type + - record.recordable.taggable.tag_list.each do |tag| + %li.entry-tag= tag diff --git a/app/views/records/index.html.haml b/app/views/records/index.html.haml index 200321e..4ceed2c 100644 --- a/app/views/records/index.html.haml +++ b/app/views/records/index.html.haml @@ -1,7 +1 @@ -%ul#records - - @records.each do |record| - %li - %span.description= link_to record.description, record.recordable.path - %ul.tags - %li.record-date= record.created_at.strftime("%m.%d.%y") - %li.entry-type{ class: "entry-type-#{record.recordable_type.downcase}" }= record.recordable_type +%ul#records= render @records -- cgit 1.4.1