Skip to content
This repository was archived by the owner on May 22, 2018. It is now read-only.

[WIP] 『最新の情報を取得』 #14

Open
wants to merge 21 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
35 changes: 35 additions & 0 deletions app/controllers/channels_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'rss'
# https://news.yahoo.co.jp/pickup/rss.xml
# https://rss-weather.yahoo.co.jp/rss/days/7320.xml
class ChannelsController < ApplicationController

def index
@channels = Channel.all
end

def new
@channel = Channel.new
end

def create
@channel = Channel.new(channel_params)
if @channel.save
@channel.update_items
redirect_to channels_path
else
render 'new'
end
end

def fetch_items
channel = Channel.find(params[:channel_id])
channel.update_items
redirect_to channels_path
end

private
def channel_params
params.require(:channel).permit(:url)
end

end
26 changes: 0 additions & 26 deletions app/controllers/feeds_controller.rb

This file was deleted.

18 changes: 18 additions & 0 deletions app/models/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Channel < ApplicationRecord

has_many :items, dependent: :destroy
validates :url, format: /\A#{URI::regexp(%w(http https))}\z/

def update_items
source_channel = RSS::Parser.parse(url).channel
assign_attributes(title: source_channel.title, description: source_channel.description)
save if changed?

source_channel.items.each do |source_item|
item = items.find_or_initialize_by(title: source_item.title)
item.assign_attributes(link: source_item.link, pubdate: source_item.pubDate)
item.save if item.changed?
end
end

end
7 changes: 0 additions & 7 deletions app/models/feed.rb

This file was deleted.

3 changes: 3 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Item < ApplicationRecord
belongs_to :channel
end
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<% @channels.each do |channel| %>
<h3><strong><%= channel.title %></strong></h3>
<h4><%= channel.description %><h4>
<%= button_to '最新の情報を取得', channel_fetch_items_path(channel), :method => :get %>
<% channel.items.each do |item| %>
<p>
<%= link_to item.link do %>
<h4><strong><%= item.title %></strong></h4>
<% end %>
<%= time_ago_in_words(item.pubDate) %>前
<%= time_ago_in_words(item.pubdate) %>前
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一つのコミットにはその目的のものだけ含まれてるべきなので、こういう修正は「最新情報の取得ボタン追加」とは別のコミットにしましょう

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixtures :allのコメント消したのもそう

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すいません、前回も指摘いただいていたのに、、、もっと注意して目的ごとにその都度コミットします!

</p>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<% if @feed.errors.any? %>
<% if @channel.errors.any? %>
<div id="error_explanation">
<h2><%= @feed.errors.count %>件のエラーがあります。</h2>
<h2><%= @channel.errors.count %>件のエラーがあります。</h2>
<ul>
<% @feed.errors.full_messages.each do |msg| %>
<% @channel.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form_for(@feed) do |f| %>
<%= form_for(@channel) do |f| %>
<%= f.label :url %><br />
<%= f.text_field :url %>
<%= f.submit "登録" %>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rails.application.routes.draw do
resources :feeds, only: [:index, :new, :create]
resources :channels, only: [:index, :new, :create] do
get 'fetch_items'
end
end
1 change: 1 addition & 0 deletions db/migrate/20180309033241_create_feeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class CreateFeeds < ActiveRecord::Migration[5.2]
def change
create_table :feeds do |t|
t.string :url, null: false, default: '', comment: 'RSSフィードのURL'

t.timestamps
end
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20180320021944_rename_feeds_to_channels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameFeedsToChannels < ActiveRecord::Migration[5.2]
def change
rename_table :feeds, :channels
end
end
12 changes: 12 additions & 0 deletions db/migrate/20180320093018_create_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateItems < ActiveRecord::Migration[5.2]
def change
create_table :items do |t|
t.string :title
t.string :link
t.datetime :pubdate
t.references :channel, foreign_key: true, null: false

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20180320093810_add_column_to_channels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddColumnToChannels < ActiveRecord::Migration[5.2]
def change
add_column :channels, :title, :string
add_column :channels, :description, :string
end
end
17 changes: 15 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_03_09_033241) do
ActiveRecord::Schema.define(version: 2018_03_20_093810) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "feeds", force: :cascade do |t|
create_table "channels", force: :cascade do |t|
t.string "url", default: "", null: false, comment: "RSSフィードのURL"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.string "description"
end

create_table "items", force: :cascade do |t|
t.string "title"
t.string "link"
t.datetime "pubdate"
t.bigint "channel_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["channel_id"], name: "index_items_on_channel_id"
end

add_foreign_key "items", "channels"
end
7 changes: 7 additions & 0 deletions test/controllers/channels_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ChannelsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
File renamed without changes.
11 changes: 11 additions & 0 deletions test/fixtures/items.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
19 changes: 19 additions & 0 deletions test/models/channel_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'

class ChannelTest < ActiveSupport::TestCase

test "should save channel with valid url" do
channel = Channel.new(url: "https://rss-weather.yahoo.co.jp/rss/days/7320.xml")
assert channel.valid?
end

test "should not save feed when scheme is not http/https" do
channel = Channel.new(url: "javascript:alert('XSS');//http://shiraishi.jp/")
assert channel.invalid?
end

test "should not save channel without url" do
channel = Channel.new
assert channel.invalid?
end
end
19 changes: 0 additions & 19 deletions test/models/feed_test.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class FeedsControllerTest < ActionDispatch::IntegrationTest
class ItemTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
Expand Down
3 changes: 0 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@
require 'rails/test_help'

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all

# Add more helper methods to be used by all tests here...
end