From 531016993a4a71b7986966d3e5578353279cecd9 Mon Sep 17 00:00:00 2001 From: Boris <30473641+BorisJov@users.noreply.github.com> Date: Mon, 7 Aug 2017 15:25:16 +0200 Subject: [PATCH] Commit 3. -Finished I3 of tutorial --- .idea/dictionaries/grim.xml | 7 + .idea/workspace.xml | 249 ++++++++++++++----- app/assets/javascripts/tags.coffee | 3 + app/assets/stylesheets/tags.scss | 3 + app/controllers/tags_controller.rb | 17 ++ app/helpers/articles_helper.rb | 2 +- app/helpers/tags_helper.rb | 2 + app/models/article.rb | 17 ++ app/models/tag.rb | 9 + app/models/tagging.rb | 4 + app/views/articles/_form.html.erb | 4 + app/views/articles/index.html.erb | 2 +- app/views/articles/show.html.erb | 6 + app/views/tags/index.html.erb | 9 + app/views/tags/show.html.erb | 9 + config/routes.rb | 1 + db/migrate/20170807120329_create_tags.rb | 9 + db/migrate/20170807120429_create_taggings.rb | 10 + db/schema.rb | 17 +- test/controllers/tags_controller_test.rb | 7 + test/fixtures/taggings.yml | 9 + test/fixtures/tags.yml | 7 + test/models/tag_test.rb | 7 + test/models/tagging_test.rb | 7 + 24 files changed, 345 insertions(+), 72 deletions(-) create mode 100644 .idea/dictionaries/grim.xml create mode 100644 app/assets/javascripts/tags.coffee create mode 100644 app/assets/stylesheets/tags.scss create mode 100644 app/controllers/tags_controller.rb create mode 100644 app/helpers/tags_helper.rb create mode 100644 app/models/tag.rb create mode 100644 app/models/tagging.rb create mode 100644 app/views/tags/index.html.erb create mode 100644 app/views/tags/show.html.erb create mode 100644 db/migrate/20170807120329_create_tags.rb create mode 100644 db/migrate/20170807120429_create_taggings.rb create mode 100644 test/controllers/tags_controller_test.rb create mode 100644 test/fixtures/taggings.yml create mode 100644 test/fixtures/tags.yml create mode 100644 test/models/tag_test.rb create mode 100644 test/models/tagging_test.rb diff --git a/.idea/dictionaries/grim.xml b/.idea/dictionaries/grim.xml new file mode 100644 index 0000000..60f2e1b --- /dev/null +++ b/.idea/dictionaries/grim.xml @@ -0,0 +1,7 @@ + + + + taggings + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a9e7dcd..935e168 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,8 +3,10 @@ - + + + @@ -26,41 +28,51 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + @@ -75,19 +87,23 @@ @@ -141,6 +157,12 @@ + + + + + + @@ -174,6 +196,13 @@ + + + + + + + @@ -354,25 +383,25 @@ 1501680771324 - + + - - - + - + @@ -441,7 +470,6 @@ - @@ -453,35 +481,110 @@ - + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + - - + + + - + + + + + + + + + + - + @@ -489,50 +592,58 @@ - + - - + + - + - - + + - + - - + + - - + + - + - - + + - + - - + + + + + + + + + + @@ -545,42 +656,42 @@ - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + diff --git a/app/assets/javascripts/tags.coffee b/app/assets/javascripts/tags.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/tags.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/tags.scss b/app/assets/stylesheets/tags.scss new file mode 100644 index 0000000..eb3f7cc --- /dev/null +++ b/app/assets/stylesheets/tags.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the tags 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/tags_controller.rb b/app/controllers/tags_controller.rb new file mode 100644 index 0000000..7bcac9a --- /dev/null +++ b/app/controllers/tags_controller.rb @@ -0,0 +1,17 @@ +class TagsController < ApplicationController + + def index + @tags = Tag.all + end + + def show + @tag = Tag.find(params[:id]) + end + + def destroy + @tag = Tag.find(params[:id]) + @tag.destroy! + + redirect_to tags_path + end +end diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb index 35703ec..20ebc18 100644 --- a/app/helpers/articles_helper.rb +++ b/app/helpers/articles_helper.rb @@ -1,7 +1,7 @@ module ArticlesHelper def article_params - params.require(:article).permit(:title, :body) + params.require(:article).permit(:title, :body, :tag_list) end end diff --git a/app/helpers/tags_helper.rb b/app/helpers/tags_helper.rb new file mode 100644 index 0000000..23450bc --- /dev/null +++ b/app/helpers/tags_helper.rb @@ -0,0 +1,2 @@ +module TagsHelper +end diff --git a/app/models/article.rb b/app/models/article.rb index 900c54e..c779b31 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,3 +1,20 @@ class Article < ApplicationRecord has_many :comments + has_many :taggings + has_many :tags, through: :taggings + + def tag_list + tags.join(', ') + end + + def tag_list=(tags_string) + tag_names = tags_string.split(",").collect do |s| + s.strip.downcase + end.uniq + new_or_found_tags = tag_names.collect do |name| + Tag.find_or_create_by(name: name) + end + self.tags = new_or_found_tags + end + end diff --git a/app/models/tag.rb b/app/models/tag.rb new file mode 100644 index 0000000..a988af6 --- /dev/null +++ b/app/models/tag.rb @@ -0,0 +1,9 @@ +class Tag < ApplicationRecord + has_many :taggings + has_many :articles, through: :taggings + + def to_s + name + end + +end diff --git a/app/models/tagging.rb b/app/models/tagging.rb new file mode 100644 index 0000000..5013c9b --- /dev/null +++ b/app/models/tagging.rb @@ -0,0 +1,4 @@ +class Tagging < ApplicationRecord + belongs_to :tag + belongs_to :article +end diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb index 93017c3..50c70cf 100644 --- a/app/views/articles/_form.html.erb +++ b/app/views/articles/_form.html.erb @@ -12,6 +12,10 @@ <%= f.label :body %>
<%= f.text_area :body %>

+

+ <%= f.label :tag_list %>
+ <%= f.text_field :tag_list %> +

<%= f.submit %>

diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index 5453308..b1a0df7 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -9,4 +9,4 @@ <% end %> -<%= link_to "Create a new article", new_article_path, class: "new_article"%> \ No newline at end of file +<%= link_to 'Create a new article', new_article_path, class: "new_article"%> \ No newline at end of file diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index c8d6a96..d2eaf3a 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -1,4 +1,10 @@

<%= @article.title %>

+

+ Tags: + <% @article.tags.each do |tag| %> + <%= link_to tag.name, tag_path(tag) %> + <% end %> +

<%= @article.body %>

<%= link_to 'Edit Article', edit_article_path(@article) %> diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erb new file mode 100644 index 0000000..f51776a --- /dev/null +++ b/app/views/tags/index.html.erb @@ -0,0 +1,9 @@ +

All Tags

+ +
    + <% @tags.each do |t| %> +
  • <%= link_to t.name, tag_path(t)%>
  • + <% end %> +

+ +<%= link_to 'Home', articles_path %> \ No newline at end of file diff --git a/app/views/tags/show.html.erb b/app/views/tags/show.html.erb new file mode 100644 index 0000000..54b53b1 --- /dev/null +++ b/app/views/tags/show.html.erb @@ -0,0 +1,9 @@ +

Articles by Tag <%=@tag.name%>

+ + + <% @tag.articles.each do |article|%> +
  • <%= link_to article.title, article_path(article) %>
  • + <%end%> +
    + +<%= link_to 'Delete Tag', tag_path(@tag), method: :delete, data: {confirm: 'Are you sure it is wise to delete this tag?'} %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 66009e5..e52e4fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,4 +4,5 @@ resources :articles do resources :comments end + resources :tags end diff --git a/db/migrate/20170807120329_create_tags.rb b/db/migrate/20170807120329_create_tags.rb new file mode 100644 index 0000000..8ee6a19 --- /dev/null +++ b/db/migrate/20170807120329_create_tags.rb @@ -0,0 +1,9 @@ +class CreateTags < ActiveRecord::Migration[5.1] + def change + create_table :tags do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20170807120429_create_taggings.rb b/db/migrate/20170807120429_create_taggings.rb new file mode 100644 index 0000000..685ad40 --- /dev/null +++ b/db/migrate/20170807120429_create_taggings.rb @@ -0,0 +1,10 @@ +class CreateTaggings < ActiveRecord::Migration[5.1] + def change + create_table :taggings do |t| + t.references :tag, foreign_key: true + t.references :article, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 28acd75..1700316 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170806133537) do +ActiveRecord::Schema.define(version: 20170807120429) do create_table "articles", force: :cascade do |t| t.string "title" @@ -28,4 +28,19 @@ t.index ["article_id"], name: "index_comments_on_article_id" end + create_table "taggings", force: :cascade do |t| + t.integer "tag_id" + t.integer "article_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["article_id"], name: "index_taggings_on_article_id" + t.index ["tag_id"], name: "index_taggings_on_tag_id" + end + + create_table "tags", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end diff --git a/test/controllers/tags_controller_test.rb b/test/controllers/tags_controller_test.rb new file mode 100644 index 0000000..f049a1e --- /dev/null +++ b/test/controllers/tags_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TagsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/taggings.yml b/test/fixtures/taggings.yml new file mode 100644 index 0000000..a21d34a --- /dev/null +++ b/test/fixtures/taggings.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + tag: one + article: one + +two: + tag: two + article: two diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml new file mode 100644 index 0000000..56066c6 --- /dev/null +++ b/test/fixtures/tags.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb new file mode 100644 index 0000000..b8498a1 --- /dev/null +++ b/test/models/tag_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TagTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/tagging_test.rb b/test/models/tagging_test.rb new file mode 100644 index 0000000..77dbeac --- /dev/null +++ b/test/models/tagging_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TaggingTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end