Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

各記事にタグを付ける #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/taggings.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/taggings.css.scss
Original file line number Diff line number Diff line change
@@ -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/
1 change: 1 addition & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/taggings_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/helpers/taggings_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TaggingsHelper
end
2 changes: 2 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Article < ActiveRecord::Base
has_many :taggings
has_many :tags, through: :taggings
end
4 changes: 4 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Tag < ActiveRecord::Base
has_many :taggings
has_many :articles, through: :taggings
end
4 changes: 4 additions & 0 deletions app/models/tagging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :article
end
12 changes: 12 additions & 0 deletions app/views/articles/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ 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:
= 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
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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".
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20141025042303_create_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateTags < ActiveRecord::Migration
def change
create_table :tags do |t|
t.string :name

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20141025042749_create_taggings.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 17 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,4 +20,20 @@
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"

create_table "tags", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

end
5 changes: 5 additions & 0 deletions spec/controllers/taggings_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe TaggingsController, :type => :controller do

end
3 changes: 3 additions & 0 deletions spec/fabricators/tag_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fabricator(:tag) do
name "MyString"
end
4 changes: 4 additions & 0 deletions spec/fabricators/tagging_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fabricator(:tagging) do
tag nil
article nil
end
15 changes: 15 additions & 0 deletions spec/helpers/taggings_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/models/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Tag, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/tagging_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Tagging, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end