Skip to content

Commit

Permalink
add tag feature to data sets #13
Browse files Browse the repository at this point in the history
  • Loading branch information
rest515 committed Feb 14, 2020
1 parent 2cc8985 commit 22a4c96
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/controllers/data_sets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def set_data_set

def data_set_params
params.require(:data_set).permit(
:title, :body,
:title, :body, :tag_list,
links_attributes: [:id, :title, :body, :url, :_destroy]
)
end
Expand Down
38 changes: 38 additions & 0 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class TagsController < ApplicationController
before_action :set_tag, only: [:show, :edit, :update, :destroy]

def show
@data_sets = DataSet.tagged_with(@tag)
end

def edit
end

def update
@tag = ActsAsTaggableOn::Tag.find(params[:id])

throw Exception

respond_to do |format|
if @tag.update(tag_params)
format.html { redirect_to tag_path(@tag), notice: t('Tag was successfully updated.') }
format.json { render :show, status: :ok, location: @tag }
else
format.html { render :edit }
format.json { render json: @tag.errors, status: :unprocessable_entity }
end
end

end

private

def set_tag
@tag = ActsAsTaggableOn::Tag.find(params[:id])
end

def tag_params
params.require(:acts_as_taggable_on_tag).permit(:id)
end

end
6 changes: 4 additions & 2 deletions app/models/data_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ class DataSet < ApplicationRecord
belongs_to :user
has_many :links

validates :title, presence: true
validates :body, presence: true
acts_as_taggable

accepts_nested_attributes_for :links, reject_if: :all_blank, allow_destroy: true

validates :title, presence: true
validates :body, presence: true

default_scope { order("updated_at desc") }
end
10 changes: 10 additions & 0 deletions app/views/data_sets/_data_set.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.mb-4.card
.card-body
%h5.card-title= link_to data_set.title, data_set
%h6.card-subtitle.mb-2.text-muted

%p.card-text
%small= strip_tags(data_set.body).truncate(150)

- data_set.tags.each do |tag|
= link_to tag.name, tag_path(tag), class: "btn btn-outline-secondary btn-sm"
3 changes: 2 additions & 1 deletion app/views/data_sets/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
= simple_form_for @data_set do |f|
= f.input :title
= f.input :body, as: :summernote
= f.input :tag_list

#data-set-links.pb-4
%h4.text-muted 관련 항목
%h5.text-muted= t("related_info")
#links-items
= f.simple_fields_for :links do |link|
= render 'link_fields', f: link
Expand Down
10 changes: 1 addition & 9 deletions app/views/data_sets/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,4 @@
.row
- @data_sets.each do |data_set|
.col-6
.mb-4.card
.card-header #ABC #DEF
.card-body
%h5.card-title= data_set.title
%h6.card-subtitle.mb-2.text-muted

%p.card-text
%small= strip_tags(data_set.body).truncate(150)
= link_to "데이터셋 보기", data_set, class: "btn btn-primary"
= render data_set
8 changes: 6 additions & 2 deletions app/views/data_sets/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
.my-4.p-4.border.bg-white
= raw @data_set.body

.py-2
- @data_set.tags.each do |tag|
= link_to tag.name, tag_path(tag), class: 'btn btn-outline-secondary btn-sm'

.my-4
%h4.text-muted= t('related_info')
.list-group
Expand All @@ -15,7 +19,7 @@
.d-flex.justify-content-between
%div
- if user_signed_in?
= link_to '데이터 보완하기', edit_data_set_path(@data_set), class: "btn btn-outline-secondary"
= link_to t('edit'), edit_data_set_path(@data_set), class: "btn btn-outline-secondary"
%div
- if user_signed_in? && @data_set.user == current_user
= link_to '데이터 삭제하기', @data_set, method: :delete, data: { confirm: "정말 삭제하시겠습니까?" }, class: "btn btn-outline-danger"
= link_to t('destroy'), @data_set, method: :delete, data: { confirm: "정말 삭제하시겠습니까?" }, class: "btn btn-outline-danger"
2 changes: 2 additions & 0 deletions app/views/tags/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
= simple_form_for @tag, url: tag_path(@tag) do |f|
= f.submit
4 changes: 4 additions & 0 deletions app/views/tags/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.pb-2
%h2= @tag.name

= render "form"
10 changes: 10 additions & 0 deletions app/views/tags/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.pb-2
%h2
= @tag.name

%section.py-2
%h5.text-muted= t('data_sets')
.row
- @data_sets.each do |data_set|
.col-6
= render data_set
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
devise_for :users

resources :data_sets
resources :tags

root "data_sets#index"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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 ActsAsTaggableOn.tags_table do |t|
t.string :name
t.timestamps
end

create_table ActsAsTaggableOn.taggings_table do |t|
t.references :tag, foreign_key: { to_table: ActsAsTaggableOn.tags_table }

# 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 ActsAsTaggableOn.taggings_table, :tag_id
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
end

def self.down
drop_table ActsAsTaggableOn.taggings_table
drop_table ActsAsTaggableOn.tags_table
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 ActsAsTaggableOn.tags_table, :name, unique: true

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

def self.down
remove_index ActsAsTaggableOn.tags_table, :name

remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_idx'

add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
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 ActsAsTaggableOn.tags_table, :taggings_count, :integer, default: 0

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

def self.down
remove_column ActsAsTaggableOn.tags_table, :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 ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
end

def self.down
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
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 #{ActsAsTaggableOn.tags_table} 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 ActsAsTaggableOn.taggings_table, :tag_id unless index_exists? ActsAsTaggableOn.taggings_table, :tag_id
add_index ActsAsTaggableOn.taggings_table, :taggable_id unless index_exists? ActsAsTaggableOn.taggings_table, :taggable_id
add_index ActsAsTaggableOn.taggings_table, :taggable_type unless index_exists? ActsAsTaggableOn.taggings_table, :taggable_type
add_index ActsAsTaggableOn.taggings_table, :tagger_id unless index_exists? ActsAsTaggableOn.taggings_table, :tagger_id
add_index ActsAsTaggableOn.taggings_table, :context unless index_exists? ActsAsTaggableOn.taggings_table, :context

unless index_exists? ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]
add_index ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]
end

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

ActiveRecord::Schema.define(version: 2020_02_12_045100) do
ActiveRecord::Schema.define(version: 2020_02_14_025012) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "categories", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "categories_data_sets", force: :cascade do |t|
t.bigint "category_id"
t.bigint "data_set_id"
t.index ["category_id"], name: "index_categories_data_sets_on_category_id"
t.index ["data_set_id"], name: "index_categories_data_sets_on_data_set_id"
end

create_table "data_sets", force: :cascade do |t|
t.string "title"
t.string "body"
Expand Down Expand Up @@ -44,6 +58,33 @@
t.index ["data_set_id"], name: "index_relateds_on_data_set_id"
end

create_table "taggings", id: :serial, force: :cascade 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: "taggings_taggable_context_idx"
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: :serial, force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "taggings_count", default: 0
t.index ["name"], name: "index_tags_on_name", unique: true
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down Expand Up @@ -77,4 +118,5 @@
end

add_foreign_key "data_sets", "users"
add_foreign_key "taggings", "tags"
end

0 comments on commit 22a4c96

Please sign in to comment.