Skip to content

Commit

Permalink
Added tags
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hatkirby committed Jul 7, 2018
1 parent dd231a3 commit 42d9db5
Show file tree
Hide file tree
Showing 26 changed files with 218 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/tmux*.log
*.swp
tags
!tags/

.byebug_history
.DS_Store
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ gem 'ckeditor'
gem 'paperclip'
gem 'jquery-rails'
gem 'pokeviewer', github: "hatkirby/pokeviewer"
gem 'acts-as-taggable-on'
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
acts-as-taggable-on (6.0.0)
activerecord (~> 5.0)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
airbrussh (1.3.0)
Expand Down Expand Up @@ -269,6 +271,7 @@ PLATFORMS
ruby

DEPENDENCIES
acts-as-taggable-on
byebug
capistrano (~> 3.0)
capistrano-bundler
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/admin/records.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
//= require jquery_ujs
//= require turbolinks
//= require ckeditor/init
//= require tags-input
//= require_tree ./admin
1 change: 1 addition & 0 deletions app/assets/stylesheets/admin.css.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
*= require normalize-rails
*= require tags-input
*= require_tree ./admin
*/
8 changes: 8 additions & 0 deletions app/assets/stylesheets/admin/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions app/assets/stylesheets/main/records.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
.tags {
margin: .25em;
display: flex;
flex-wrap: wrap;
padding-left: 0;

li {
Expand Down Expand Up @@ -58,6 +59,7 @@
}
}

&.entry-tag {
& + li {
margin-left: 1em;
}
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/blogs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def update
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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/streams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def update
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
Expand Down
6 changes: 6 additions & 0 deletions app/models/blog.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,6 +14,10 @@ def path
"/says/#{slug}"
end

def taggable
self
end

private
def set_draft_title
if self.title.blank? and not self.published
Expand Down
6 changes: 6 additions & 0 deletions app/models/stream.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Stream < ApplicationRecord
include Recordable

acts_as_taggable

has_many :updates

validates :title, presence: true
Expand All @@ -9,4 +11,8 @@ class Stream < ApplicationRecord
def path
"/thinks/#{slug}"
end

def taggable
self
end
end
4 changes: 4 additions & 0 deletions app/models/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class Update < ApplicationRecord
def path
"/thinks/#{stream.slug}\#update-#{id}"
end

def taggable
stream
end
end
4 changes: 4 additions & 0 deletions app/views/admin/blogs/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/streams/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions app/views/records/_record.html.haml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 1 addition & 7 deletions app/views/records/index.html.haml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This migration comes from acts_as_taggable_on_engine (originally 1)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]; end
else
class ActsAsTaggableOnMigration < ActiveRecord::Migration; end
end
ActsAsTaggableOnMigration.class_eval do
def self.up
create_table :tags do |t|
t.string :name
end

create_table :taggings do |t|
t.references :tag

# You should make sure that the column created is
# long enough to store the required class names.
t.references :taggable, polymorphic: true
t.references :tagger, polymorphic: true

# Limit is created to prevent MySQL error on index
# length for MyISAM table type: http://bit.ly/vgW2Ql
t.string :context, limit: 128

t.datetime :created_at
end

add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
end

def self.down
drop_table :taggings
drop_table :tags
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This migration comes from acts_as_taggable_on_engine (originally 2)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingUniqueIndices < ActiveRecord::Migration[4.2]; end
else
class AddMissingUniqueIndices < ActiveRecord::Migration; end
end
AddMissingUniqueIndices.class_eval do
def self.up
add_index :tags, :name, unique: true

remove_index :taggings, :tag_id if index_exists?(:taggings, :tag_id)
remove_index :taggings, [:taggable_id, :taggable_type, :context]
add_index :taggings,
[:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
unique: true, name: 'taggings_idx'
end

def self.down
remove_index :tags, :name

remove_index :taggings, name: 'taggings_idx'

add_index :taggings, :tag_id unless index_exists?(:taggings, :tag_id)
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This migration comes from acts_as_taggable_on_engine (originally 3)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration[4.2]; end
else
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration; end
end
AddTaggingsCounterCacheToTags.class_eval do
def self.up
add_column :tags, :taggings_count, :integer, default: 0

ActsAsTaggableOn::Tag.reset_column_information
ActsAsTaggableOn::Tag.find_each do |tag|
ActsAsTaggableOn::Tag.reset_counters(tag.id, :taggings)
end
end

def self.down
remove_column :tags, :taggings_count
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This migration comes from acts_as_taggable_on_engine (originally 4)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingTaggableIndex < ActiveRecord::Migration[4.2]; end
else
class AddMissingTaggableIndex < ActiveRecord::Migration; end
end
AddMissingTaggableIndex.class_eval do
def self.up
add_index :taggings, [:taggable_id, :taggable_type, :context]
end

def self.down
remove_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This migration comes from acts_as_taggable_on_engine (originally 5)
# This migration is added to circumvent issue #623 and have special characters
# work properly
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class ChangeCollationForTagNames < ActiveRecord::Migration[4.2]; end
else
class ChangeCollationForTagNames < ActiveRecord::Migration; end
end
ChangeCollationForTagNames.class_eval do
def up
if ActsAsTaggableOn::Utils.using_mysql?
execute("ALTER TABLE tags MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This migration comes from acts_as_taggable_on_engine (originally 6)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingIndexesOnTaggings < ActiveRecord::Migration[4.2]; end
else
class AddMissingIndexesOnTaggings < ActiveRecord::Migration; end
end
AddMissingIndexesOnTaggings.class_eval do
def change
add_index :taggings, :tag_id unless index_exists? :taggings, :tag_id
add_index :taggings, :taggable_id unless index_exists? :taggings, :taggable_id
add_index :taggings, :taggable_type unless index_exists? :taggings, :taggable_type
add_index :taggings, :tagger_id unless index_exists? :taggings, :tagger_id
add_index :taggings, :context unless index_exists? :taggings, :context

unless index_exists? :taggings, [:tagger_id, :tagger_type]
add_index :taggings, [:tagger_id, :tagger_type]
end

unless index_exists? :taggings, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
add_index :taggings, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
end
end
end
27 changes: 26 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180704144707) do
ActiveRecord::Schema.define(version: 20180707142420) do

create_table "blogs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" do |t|
t.string "title"
Expand Down Expand Up @@ -238,6 +238,31 @@
t.datetime "updated_at", null: false
end

create_table "taggings", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t|
t.integer "tag_id"
t.string "taggable_type"
t.integer "taggable_id"
t.string "tagger_type"
t.integer "tagger_id"
t.string "context", limit: 128
t.datetime "created_at"
t.index ["context"], name: "index_taggings_on_context"
t.index ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
t.index ["tag_id"], name: "index_taggings_on_tag_id"
t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy"
t.index ["taggable_id"], name: "index_taggings_on_taggable_id"
t.index ["taggable_type"], name: "index_taggings_on_taggable_type"
t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type"
t.index ["tagger_id"], name: "index_taggings_on_tagger_id"
end

create_table "tags", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t|
t.string "name", limit: 255, collation: "utf8_bin"
t.integer "taggings_count", default: 0
t.index ["name"], name: "index_tags_on_name", unique: true
end

create_table "updates", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t|
t.bigint "stream_id"
t.text "body"
Expand Down
2 changes: 2 additions & 0 deletions vendor/assets/javascripts/tags-input.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/assets/stylesheets/tags-input.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 42d9db5

Please sign in to comment.