From 621095005c62deccd032d921c8cb9064bdffbcb6 Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Sun, 26 Oct 2014 18:20:05 +0900 Subject: [PATCH 1/6] Add tag model --- app/models/tag.rb | 4 ++++ db/migrate/20141025042303_create_tags.rb | 9 +++++++++ db/schema.rb | 9 +++++++++ spec/fabricators/tag_fabricator.rb | 3 +++ spec/models/tag_spec.rb | 5 +++++ 5 files changed, 30 insertions(+) create mode 100644 app/models/tag.rb create mode 100644 db/migrate/20141025042303_create_tags.rb create mode 100644 spec/fabricators/tag_fabricator.rb create mode 100644 spec/models/tag_spec.rb diff --git a/app/models/tag.rb b/app/models/tag.rb new file mode 100644 index 0000000..7775e75 --- /dev/null +++ b/app/models/tag.rb @@ -0,0 +1,4 @@ +class Tag < ActiveRecord::Base + has_many :taggings + has_many :articles, through: :taggings +end diff --git a/db/migrate/20141025042303_create_tags.rb b/db/migrate/20141025042303_create_tags.rb new file mode 100644 index 0000000..048be7c --- /dev/null +++ b/db/migrate/20141025042303_create_tags.rb @@ -0,0 +1,9 @@ +class CreateTags < ActiveRecord::Migration + def change + create_table :tags do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e06ad68..edc0293 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -20,4 +20,13 @@ t.datetime "updated_at" end + add_index "taggings", ["article_id"], name: "index_taggings_on_article_id" + add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id" + + create_table "tags", force: true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + end diff --git a/spec/fabricators/tag_fabricator.rb b/spec/fabricators/tag_fabricator.rb new file mode 100644 index 0000000..d830409 --- /dev/null +++ b/spec/fabricators/tag_fabricator.rb @@ -0,0 +1,3 @@ +Fabricator(:tag) do + name "MyString" +end diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb new file mode 100644 index 0000000..2ed5ed6 --- /dev/null +++ b/spec/models/tag_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Tag, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 0e19fa0588bc9f9fa712bfab68d54b141c887930 Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Sun, 26 Oct 2014 18:21:36 +0900 Subject: [PATCH 2/6] Add tagging controller --- app/models/tagging.rb | 4 ++++ db/migrate/20141025042749_create_taggings.rb | 10 ++++++++++ db/schema.rb | 9 ++++++++- spec/fabricators/tagging_fabricator.rb | 4 ++++ spec/models/tagging_spec.rb | 5 +++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 app/models/tagging.rb create mode 100644 db/migrate/20141025042749_create_taggings.rb create mode 100644 spec/fabricators/tagging_fabricator.rb create mode 100644 spec/models/tagging_spec.rb diff --git a/app/models/tagging.rb b/app/models/tagging.rb new file mode 100644 index 0000000..7e6d0df --- /dev/null +++ b/app/models/tagging.rb @@ -0,0 +1,4 @@ +class Tagging < ActiveRecord::Base + belongs_to :tag + belongs_to :article +end diff --git a/db/migrate/20141025042749_create_taggings.rb b/db/migrate/20141025042749_create_taggings.rb new file mode 100644 index 0000000..2a8c05e --- /dev/null +++ b/db/migrate/20141025042749_create_taggings.rb @@ -0,0 +1,10 @@ +class CreateTaggings < ActiveRecord::Migration + def change + create_table :taggings do |t| + t.references :tag, index: true + t.references :article, index: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index edc0293..f8ca934 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141024184049) do +ActiveRecord::Schema.define(version: 20141025042749) do create_table "articles", force: true do |t| t.string "title" @@ -20,6 +20,13 @@ t.datetime "updated_at" end + create_table "taggings", force: true do |t| + t.integer "tag_id" + t.integer "article_id" + t.datetime "created_at" + t.datetime "updated_at" + end + add_index "taggings", ["article_id"], name: "index_taggings_on_article_id" add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id" diff --git a/spec/fabricators/tagging_fabricator.rb b/spec/fabricators/tagging_fabricator.rb new file mode 100644 index 0000000..5730747 --- /dev/null +++ b/spec/fabricators/tagging_fabricator.rb @@ -0,0 +1,4 @@ +Fabricator(:tagging) do + tag nil + article nil +end diff --git a/spec/models/tagging_spec.rb b/spec/models/tagging_spec.rb new file mode 100644 index 0000000..2908d2a --- /dev/null +++ b/spec/models/tagging_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Tagging, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 6da0540080951181fa431ba36b62adcf9771a548 Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Sun, 26 Oct 2014 18:23:42 +0900 Subject: [PATCH 3/6] Add tagging controller --- app/assets/javascripts/taggings.coffee | 3 +++ app/assets/stylesheets/taggings.css.scss | 3 +++ app/controllers/taggings_controller.rb | 24 ++++++++++++++++++++ app/helpers/taggings_helper.rb | 2 ++ config/routes.rb | 1 + spec/controllers/taggings_controller_spec.rb | 5 ++++ spec/helpers/taggings_helper_spec.rb | 15 ++++++++++++ 7 files changed, 53 insertions(+) create mode 100644 app/assets/javascripts/taggings.coffee create mode 100644 app/assets/stylesheets/taggings.css.scss create mode 100644 app/controllers/taggings_controller.rb create mode 100644 app/helpers/taggings_helper.rb create mode 100644 spec/controllers/taggings_controller_spec.rb create mode 100644 spec/helpers/taggings_helper_spec.rb diff --git a/app/assets/javascripts/taggings.coffee b/app/assets/javascripts/taggings.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/taggings.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/taggings.css.scss b/app/assets/stylesheets/taggings.css.scss new file mode 100644 index 0000000..bb50052 --- /dev/null +++ b/app/assets/stylesheets/taggings.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the taggings controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/taggings_controller.rb b/app/controllers/taggings_controller.rb new file mode 100644 index 0000000..c3d046c --- /dev/null +++ b/app/controllers/taggings_controller.rb @@ -0,0 +1,24 @@ +class TaggingsController < ApplicationController + def create + tags = params[:tag_names].split(',') + tag_ids = tags.map do |tag_name| + tag = Tag.new + tag.name = tag_name + tag.save + tag.id + end + + tag_ids.each do |tag_id| + tagging = Tagging.new(tagging_params) + tagging.tag_id = tag_id + tagging.save + end + + redirect_to root_path + end + + private + def tagging_params + params.require(:tagging).permit(:article_id) + end +end diff --git a/app/helpers/taggings_helper.rb b/app/helpers/taggings_helper.rb new file mode 100644 index 0000000..06615b9 --- /dev/null +++ b/app/helpers/taggings_helper.rb @@ -0,0 +1,2 @@ +module TaggingsHelper +end diff --git a/config/routes.rb b/config/routes.rb index 732e30f..7c53b4a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ Rails.application.routes.draw do root 'articles#index' resources :articles + resources :taggings # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/spec/controllers/taggings_controller_spec.rb b/spec/controllers/taggings_controller_spec.rb new file mode 100644 index 0000000..b01bd6e --- /dev/null +++ b/spec/controllers/taggings_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe TaggingsController, :type => :controller do + +end diff --git a/spec/helpers/taggings_helper_spec.rb b/spec/helpers/taggings_helper_spec.rb new file mode 100644 index 0000000..2bfc261 --- /dev/null +++ b/spec/helpers/taggings_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the TaggingsHelper. For example: +# +# describe TaggingsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe TaggingsHelper, :type => :helper do + pending "add some examples to (or delete) #{__FILE__}" +end From 85082d9fcc86212de530b9228f156d02a313999b Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Sun, 26 Oct 2014 18:24:09 +0900 Subject: [PATCH 4/6] Extend article model to relate taggings and tags models --- app/models/article.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/article.rb b/app/models/article.rb index b7ac9f7..0e33037 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,2 +1,4 @@ class Article < ActiveRecord::Base + has_many :taggings + has_many :tags, through: :taggings end From 50e97e8b694535d04c8944140e78551ccfc240e0 Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Sun, 26 Oct 2014 18:28:43 +0900 Subject: [PATCH 5/6] Add tag form to article view --- app/views/articles/show.html.slim | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/views/articles/show.html.slim b/app/views/articles/show.html.slim index b032fc2..623800d 100644 --- a/app/views/articles/show.html.slim +++ b/app/views/articles/show.html.slim @@ -10,6 +10,14 @@ p strong Body: = processor.call(@article.body)[:output].to_s.html_safe += form_for Tagging.new do |f| + p + |tags: + = text_field_tag :tag_names + + = hidden_field :tagging, :article_id, value: @article.id + p= f.submit 'Tag Create' + = link_to 'Edit', edit_article_path(@article) '| = link_to 'Back', articles_path From 1a7340eeb8e3d5e3dbbc46d535a2d9e481b10e4c Mon Sep 17 00:00:00 2001 From: alitaso345 Date: Sun, 26 Oct 2014 19:06:27 +0900 Subject: [PATCH 6/6] Improve show article, it can show article's tag --- app/controllers/articles_controller.rb | 1 + app/views/articles/show.html.slim | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 1c105b6..df92eb1 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -10,6 +10,7 @@ def index # GET /articles/1 # GET /articles/1.json def show + @tags = @article.tags.map{|tag| tag.name} end # GET /articles/new diff --git a/app/views/articles/show.html.slim b/app/views/articles/show.html.slim index 623800d..ed53271 100644 --- a/app/views/articles/show.html.slim +++ b/app/views/articles/show.html.slim @@ -10,6 +10,10 @@ p strong Body: = processor.call(@article.body)[:output].to_s.html_safe +p + strong Tags: + = @tags.join(', ') + = form_for Tagging.new do |f| p |tags: