diff --git a/app/controllers/data_sets_controller.rb b/app/controllers/data_sets_controller.rb index 12a2c3c..8722b33 100644 --- a/app/controllers/data_sets_controller.rb +++ b/app/controllers/data_sets_controller.rb @@ -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 diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb new file mode 100644 index 0000000..8794522 --- /dev/null +++ b/app/controllers/tags_controller.rb @@ -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 \ No newline at end of file diff --git a/app/models/data_set.rb b/app/models/data_set.rb index 2904876..1753135 100644 --- a/app/models/data_set.rb +++ b/app/models/data_set.rb @@ -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 diff --git a/app/views/data_sets/_data_set.html.haml b/app/views/data_sets/_data_set.html.haml new file mode 100644 index 0000000..4979b97 --- /dev/null +++ b/app/views/data_sets/_data_set.html.haml @@ -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" diff --git a/app/views/data_sets/_form.html.haml b/app/views/data_sets/_form.html.haml index f20e867..d6abe45 100644 --- a/app/views/data_sets/_form.html.haml +++ b/app/views/data_sets/_form.html.haml @@ -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 diff --git a/app/views/data_sets/index.html.haml b/app/views/data_sets/index.html.haml index a183068..d866d5b 100644 --- a/app/views/data_sets/index.html.haml +++ b/app/views/data_sets/index.html.haml @@ -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 diff --git a/app/views/data_sets/show.html.haml b/app/views/data_sets/show.html.haml index 29aec19..931eb75 100644 --- a/app/views/data_sets/show.html.haml +++ b/app/views/data_sets/show.html.haml @@ -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 @@ -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" \ No newline at end of file + = link_to t('destroy'), @data_set, method: :delete, data: { confirm: "정말 삭제하시겠습니까?" }, class: "btn btn-outline-danger" \ No newline at end of file diff --git a/app/views/tags/_form.html.haml b/app/views/tags/_form.html.haml new file mode 100644 index 0000000..394a8ae --- /dev/null +++ b/app/views/tags/_form.html.haml @@ -0,0 +1,2 @@ += simple_form_for @tag, url: tag_path(@tag) do |f| + = f.submit \ No newline at end of file diff --git a/app/views/tags/edit.html.haml b/app/views/tags/edit.html.haml new file mode 100644 index 0000000..49c0438 --- /dev/null +++ b/app/views/tags/edit.html.haml @@ -0,0 +1,4 @@ +.pb-2 + %h2= @tag.name + += render "form" \ No newline at end of file diff --git a/app/views/tags/show.html.haml b/app/views/tags/show.html.haml new file mode 100644 index 0000000..12f1c35 --- /dev/null +++ b/app/views/tags/show.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 1e4e324..acf1e8b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ devise_for :users resources :data_sets + resources :tags root "data_sets#index" end diff --git a/db/migrate/20200214025007_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb b/db/migrate/20200214025007_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb new file mode 100644 index 0000000..51a01a0 --- /dev/null +++ b/db/migrate/20200214025007_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb @@ -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 diff --git a/db/migrate/20200214025008_add_missing_unique_indices.acts_as_taggable_on_engine.rb b/db/migrate/20200214025008_add_missing_unique_indices.acts_as_taggable_on_engine.rb new file mode 100644 index 0000000..93a4587 --- /dev/null +++ b/db/migrate/20200214025008_add_missing_unique_indices.acts_as_taggable_on_engine.rb @@ -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 diff --git a/db/migrate/20200214025009_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb b/db/migrate/20200214025009_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb new file mode 100644 index 0000000..2bdad22 --- /dev/null +++ b/db/migrate/20200214025009_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb @@ -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 diff --git a/db/migrate/20200214025010_add_missing_taggable_index.acts_as_taggable_on_engine.rb b/db/migrate/20200214025010_add_missing_taggable_index.acts_as_taggable_on_engine.rb new file mode 100644 index 0000000..3f30b37 --- /dev/null +++ b/db/migrate/20200214025010_add_missing_taggable_index.acts_as_taggable_on_engine.rb @@ -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 diff --git a/db/migrate/20200214025011_change_collation_for_tag_names.acts_as_taggable_on_engine.rb b/db/migrate/20200214025011_change_collation_for_tag_names.acts_as_taggable_on_engine.rb new file mode 100644 index 0000000..5464deb --- /dev/null +++ b/db/migrate/20200214025011_change_collation_for_tag_names.acts_as_taggable_on_engine.rb @@ -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 diff --git a/db/migrate/20200214025012_add_missing_indexes_on_taggings.acts_as_taggable_on_engine.rb b/db/migrate/20200214025012_add_missing_indexes_on_taggings.acts_as_taggable_on_engine.rb new file mode 100644 index 0000000..7c39589 --- /dev/null +++ b/db/migrate/20200214025012_add_missing_indexes_on_taggings.acts_as_taggable_on_engine.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 6c9b122..5bb7cf1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 @@ -77,4 +118,5 @@ end add_foreign_key "data_sets", "users" + add_foreign_key "taggings", "tags" end