Skip to content

Commit

Permalink
Commit 3. -Finished I3 of tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisJov committed Aug 7, 2017
1 parent 8fa572e commit 5310169
Show file tree
Hide file tree
Showing 24 changed files with 345 additions and 72 deletions.
7 changes: 7 additions & 0 deletions .idea/dictionaries/grim.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

249 changes: 180 additions & 69 deletions .idea/workspace.xml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions app/assets/javascripts/tags.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/tags.scss
Original file line number Diff line number Diff line change
@@ -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/
17 changes: 17 additions & 0 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/helpers/articles_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/helpers/tags_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TagsHelper
end
17 changes: 17 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Tag < ApplicationRecord
has_many :taggings
has_many :articles, through: :taggings

def to_s
name
end

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 < ApplicationRecord
belongs_to :tag
belongs_to :article
end
4 changes: 4 additions & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<%= f.label :body %> <br />
<%= f.text_area :body %>
</p>
<p>
<%= f.label :tag_list %><br/>
<%= f.text_field :tag_list %>
</p>
<p>
<%= f.submit %>
</p>
Expand Down
2 changes: 1 addition & 1 deletion app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
<% end %>
</ul>

<%= link_to "Create a new article", new_article_path, class: "new_article"%>
<%= link_to 'Create a new article', new_article_path, class: "new_article"%>
6 changes: 6 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<h1><%= @article.title %></h1>
<p>
Tags:
<% @article.tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
<% end %>
</p>
<p><%= @article.body %></p>

<%= link_to 'Edit Article', edit_article_path(@article) %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/tags/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>All Tags</h1>

<ul id="tags">
<% @tags.each do |t| %>
<li><%= link_to t.name, tag_path(t)%></li>
<% end %>
</ul><br/>

<%= link_to 'Home', articles_path %>
9 changes: 9 additions & 0 deletions app/views/tags/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Articles by Tag <%=@tag.name%></h1>

<u1>
<% @tag.articles.each do |article|%>
<li><%= link_to article.title, article_path(article) %></li>
<%end%>
</u1>

<%= link_to 'Delete Tag', tag_path(@tag), method: :delete, data: {confirm: 'Are you sure it is wise to delete this tag?'} %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
resources :articles do
resources :comments
end
resources :tags
end
9 changes: 9 additions & 0 deletions db/migrate/20170807120329_create_tags.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions db/migrate/20170807120429_create_taggings.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 16 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
7 changes: 7 additions & 0 deletions test/controllers/tags_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TagsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
9 changes: 9 additions & 0 deletions test/fixtures/taggings.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/fixtures/tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString

two:
name: MyString
7 changes: 7 additions & 0 deletions test/models/tag_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TagTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/tagging_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TaggingTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 5310169

Please sign in to comment.