From 093fa338bbc77c5d102e790dc5816a21d8f23097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Mon, 19 Mar 2018 18:16:02 +0900 Subject: [PATCH 01/21] =?UTF-8?q?channels=E3=83=86=E3=83=BC=E3=83=96?= =?UTF-8?q?=E3=83=AB=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/feeds_controller.rb | 3 ++- app/models/channel.rb | 3 +++ app/models/feed.rb | 3 +++ db/migrate/20180317100730_create_channels.rb | 14 ++++++++++++++ db/schema.rb | 13 ++++++++++++- test/fixtures/channels.yml | 11 +++++++++++ test/models/channel_test.rb | 7 +++++++ 7 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 app/models/channel.rb create mode 100644 db/migrate/20180317100730_create_channels.rb create mode 100644 test/fixtures/channels.yml create mode 100644 test/models/channel_test.rb diff --git a/app/controllers/feeds_controller.rb b/app/controllers/feeds_controller.rb index 5066a0e..19da751 100644 --- a/app/controllers/feeds_controller.rb +++ b/app/controllers/feeds_controller.rb @@ -21,6 +21,7 @@ def create private def feed_params - params.require(:feed).permit(:url) + params.require(:feed).permit(:url, channel_attributes: [:channel_title, :description, :item_title, :link, :pubdate, :feed_id, :id]) end + end diff --git a/app/models/channel.rb b/app/models/channel.rb new file mode 100644 index 0000000..5ff242a --- /dev/null +++ b/app/models/channel.rb @@ -0,0 +1,3 @@ +class Channel < ApplicationRecord + belongs_to :feed +end diff --git a/app/models/feed.rb b/app/models/feed.rb index 0514726..bf79c5d 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -1,4 +1,7 @@ class Feed < ApplicationRecord + has_one :channel + accepts_nested_attributes_for :channel + validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ def self.channels diff --git a/db/migrate/20180317100730_create_channels.rb b/db/migrate/20180317100730_create_channels.rb new file mode 100644 index 0000000..ba70405 --- /dev/null +++ b/db/migrate/20180317100730_create_channels.rb @@ -0,0 +1,14 @@ +class CreateChannels < ActiveRecord::Migration[5.2] + def change + create_table :channels do |t| + t.string :channel_title + t.string :description + t.string :item_title + t.string :link + t.datetime :pubdate + t.integer :feed_id + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 636141f..5dedb11 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,22 @@ # # 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_17_100730) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "channels", force: :cascade do |t| + t.string "channel_title" + t.string "description" + t.string "item_title" + t.string "link" + t.datetime "pubdate" + t.integer "feed_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "feeds", force: :cascade do |t| t.string "url", default: "", null: false, comment: "RSSフィードのURL" t.datetime "created_at", null: false diff --git a/test/fixtures/channels.yml b/test/fixtures/channels.yml new file mode 100644 index 0000000..80aed36 --- /dev/null +++ b/test/fixtures/channels.yml @@ -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 diff --git a/test/models/channel_test.rb b/test/models/channel_test.rb new file mode 100644 index 0000000..3399768 --- /dev/null +++ b/test/models/channel_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ChannelTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 2fdccd66f34ab51cb7d88cadc9b049df70367ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Mon, 19 Mar 2018 19:00:48 +0900 Subject: [PATCH 02/21] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=82=AD=E3=83=BC?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/feed.rb | 2 +- db/migrate/20180317100730_create_channels.rb | 2 +- db/schema.rb | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/feed.rb b/app/models/feed.rb index bf79c5d..fb6499f 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -1,5 +1,5 @@ class Feed < ApplicationRecord - has_one :channel + has_one :channel, dependent: :destroy accepts_nested_attributes_for :channel validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ diff --git a/db/migrate/20180317100730_create_channels.rb b/db/migrate/20180317100730_create_channels.rb index ba70405..347257c 100644 --- a/db/migrate/20180317100730_create_channels.rb +++ b/db/migrate/20180317100730_create_channels.rb @@ -6,7 +6,7 @@ def change t.string :item_title t.string :link t.datetime :pubdate - t.integer :feed_id + t.references :feed, foreign_key: true, null: false t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index 5dedb11..b6c0e88 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -21,9 +21,10 @@ t.string "item_title" t.string "link" t.datetime "pubdate" - t.integer "feed_id" + t.bigint "feed_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["feed_id"], name: "index_channels_on_feed_id" end create_table "feeds", force: :cascade do |t| @@ -32,4 +33,5 @@ t.datetime "updated_at", null: false end + add_foreign_key "channels", "feeds" end From 7f11b9e4800b3e086e03a86ee30fd0fb6e05d3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Tue, 20 Mar 2018 16:29:37 +0900 Subject: [PATCH 03/21] =?UTF-8?q?=E3=83=86=E3=83=BC=E3=83=96=E3=83=AB?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{feeds.coffee => channels.coffee} | 0 .../stylesheets/{feeds.scss => channels.scss} | 0 app/controllers/channels_controller.rb | 27 +++++++++++++++++++ app/controllers/feeds_controller.rb | 27 ------------------- app/models/channel.rb | 9 ++++++- app/models/feed.rb | 10 ------- app/models/item.rb | 3 +++ app/views/{feeds => channels}/index.html.erb | 0 app/views/{feeds => channels}/new.html.erb | 8 +++--- config/routes.rb | 2 +- db/migrate/20180309033241_create_feeds.rb | 3 +++ db/migrate/20180317100730_create_channels.rb | 6 ++--- ...20180320021944_rename_feeds_to_channels.rb | 6 +++++ db/schema.rb | 20 +++++++------- test/controllers/channels_controller_test.rb | 7 +++++ test/fixtures/{feeds.yml => items.yml} | 0 test/models/channel_test.rb | 18 ++++++++++--- test/models/feed_test.rb | 19 ------------- .../item_test.rb} | 2 +- 19 files changed, 87 insertions(+), 80 deletions(-) rename app/assets/javascripts/{feeds.coffee => channels.coffee} (100%) rename app/assets/stylesheets/{feeds.scss => channels.scss} (100%) create mode 100644 app/controllers/channels_controller.rb delete mode 100644 app/controllers/feeds_controller.rb delete mode 100644 app/models/feed.rb create mode 100644 app/models/item.rb rename app/views/{feeds => channels}/index.html.erb (100%) rename app/views/{feeds => channels}/new.html.erb (51%) create mode 100644 db/migrate/20180320021944_rename_feeds_to_channels.rb create mode 100644 test/controllers/channels_controller_test.rb rename test/fixtures/{feeds.yml => items.yml} (100%) delete mode 100644 test/models/feed_test.rb rename test/{controllers/feeds_controller_test.rb => models/item_test.rb} (56%) diff --git a/app/assets/javascripts/feeds.coffee b/app/assets/javascripts/channels.coffee similarity index 100% rename from app/assets/javascripts/feeds.coffee rename to app/assets/javascripts/channels.coffee diff --git a/app/assets/stylesheets/feeds.scss b/app/assets/stylesheets/channels.scss similarity index 100% rename from app/assets/stylesheets/feeds.scss rename to app/assets/stylesheets/channels.scss diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb new file mode 100644 index 0000000..4c145a4 --- /dev/null +++ b/app/controllers/channels_controller.rb @@ -0,0 +1,27 @@ +require 'rss' +# https://news.yahoo.co.jp/pickup/computer/rss.xml +# https://rss-weather.yahoo.co.jp/rss/days/7320.xml +class ChannelsController < ApplicationController + def index + @channels = Channel.channels + end + + def new + @channel = Channel.new + end + + def create + @channel = Channel.new(channel_params) + if @channel.save + redirect_to channels_path + else + render 'new' + end + end + + private + def channel_params + params.require(:channel).permit(:url, :title, :description, items_attributes: [:title, :link, :pubdate]) + end + +end diff --git a/app/controllers/feeds_controller.rb b/app/controllers/feeds_controller.rb deleted file mode 100644 index 19da751..0000000 --- a/app/controllers/feeds_controller.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'rss' -# https://news.yahoo.co.jp/pickup/computer/rss.xml -# https://rss-weather.yahoo.co.jp/rss/days/7320.xml -class FeedsController < ApplicationController - def index - @channels = Feed.channels - end - - def new - @feed = Feed.new - end - - def create - @feed = Feed.new(feed_params) - if @feed.save - redirect_to feeds_path - else - render 'new' - end - end - - private - def feed_params - params.require(:feed).permit(:url, channel_attributes: [:channel_title, :description, :item_title, :link, :pubdate, :feed_id, :id]) - end - -end diff --git a/app/models/channel.rb b/app/models/channel.rb index 5ff242a..9b89f4b 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -1,3 +1,10 @@ class Channel < ApplicationRecord - belongs_to :feed + has_many :items, dependent: :destroy + accepts_nested_attributes_for :items + + validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ + + def self.channels + all.map { |channel| RSS::Parser.parse(channel.url).channel } + end end diff --git a/app/models/feed.rb b/app/models/feed.rb deleted file mode 100644 index fb6499f..0000000 --- a/app/models/feed.rb +++ /dev/null @@ -1,10 +0,0 @@ -class Feed < ApplicationRecord - has_one :channel, dependent: :destroy - accepts_nested_attributes_for :channel - - validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ - - def self.channels - all.map { |feed| RSS::Parser.parse(feed.url).channel } - end -end diff --git a/app/models/item.rb b/app/models/item.rb new file mode 100644 index 0000000..5ac7b11 --- /dev/null +++ b/app/models/item.rb @@ -0,0 +1,3 @@ +class Item < ApplicationRecord + belongs_to :channel +end diff --git a/app/views/feeds/index.html.erb b/app/views/channels/index.html.erb similarity index 100% rename from app/views/feeds/index.html.erb rename to app/views/channels/index.html.erb diff --git a/app/views/feeds/new.html.erb b/app/views/channels/new.html.erb similarity index 51% rename from app/views/feeds/new.html.erb rename to app/views/channels/new.html.erb index a383dde..852f451 100644 --- a/app/views/feeds/new.html.erb +++ b/app/views/channels/new.html.erb @@ -1,15 +1,15 @@ -<% if @feed.errors.any? %> +<% if @channel.errors.any? %>
-

<%= @feed.errors.count %>件のエラーがあります。

+

<%= @channel.errors.count %>件のエラーがあります。

    - <% @feed.errors.full_messages.each do |msg| %> + <% @channel.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %> -<%= form_for(@feed) do |f| %> +<%= form_for(@channel) do |f| %> <%= f.label :url %>
<%= f.text_field :url %> <%= f.submit "登録" %> diff --git a/config/routes.rb b/config/routes.rb index 454f428..bafdf12 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,3 @@ Rails.application.routes.draw do - resources :feeds, only: [:index, :new, :create] + resources :channels, only: [:index, :new, :create] end diff --git a/db/migrate/20180309033241_create_feeds.rb b/db/migrate/20180309033241_create_feeds.rb index 90c90c5..15b0296 100644 --- a/db/migrate/20180309033241_create_feeds.rb +++ b/db/migrate/20180309033241_create_feeds.rb @@ -2,6 +2,9 @@ class CreateFeeds < ActiveRecord::Migration[5.2] def change create_table :feeds do |t| t.string :url, null: false, default: '', comment: 'RSSフィードのURL' + t.string :title + t.string :description + t.timestamps end end diff --git a/db/migrate/20180317100730_create_channels.rb b/db/migrate/20180317100730_create_channels.rb index 347257c..841c4ba 100644 --- a/db/migrate/20180317100730_create_channels.rb +++ b/db/migrate/20180317100730_create_channels.rb @@ -1,12 +1,10 @@ class CreateChannels < ActiveRecord::Migration[5.2] def change create_table :channels do |t| - t.string :channel_title - t.string :description - t.string :item_title + t.string :title t.string :link t.datetime :pubdate - t.references :feed, foreign_key: true, null: false + t.references :channel, foreign_key: true, null: false t.timestamps end diff --git a/db/migrate/20180320021944_rename_feeds_to_channels.rb b/db/migrate/20180320021944_rename_feeds_to_channels.rb new file mode 100644 index 0000000..09de4cb --- /dev/null +++ b/db/migrate/20180320021944_rename_feeds_to_channels.rb @@ -0,0 +1,6 @@ +class RenameFeedsToChannels < ActiveRecord::Migration[5.2] + def change + rename_table :channels, :items + rename_table :feeds, :channels + end +end diff --git a/db/schema.rb b/db/schema.rb index b6c0e88..7340031 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,28 +10,28 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_03_17_100730) do +ActiveRecord::Schema.define(version: 2018_03_20_021944) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "channels", force: :cascade do |t| - t.string "channel_title" + t.string "url", default: "", null: false, comment: "RSSフィードのURL" + t.string "title" t.string "description" - t.string "item_title" - t.string "link" - t.datetime "pubdate" - t.bigint "feed_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["feed_id"], name: "index_channels_on_feed_id" end - create_table "feeds", force: :cascade do |t| - t.string "url", default: "", null: false, comment: "RSSフィードのURL" + 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 "channels", "feeds" + add_foreign_key "items", "items", column: "channel_id" end diff --git a/test/controllers/channels_controller_test.rb b/test/controllers/channels_controller_test.rb new file mode 100644 index 0000000..439c7ff --- /dev/null +++ b/test/controllers/channels_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ChannelsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/feeds.yml b/test/fixtures/items.yml similarity index 100% rename from test/fixtures/feeds.yml rename to test/fixtures/items.yml diff --git a/test/models/channel_test.rb b/test/models/channel_test.rb index 3399768..961d650 100644 --- a/test/models/channel_test.rb +++ b/test/models/channel_test.rb @@ -1,7 +1,19 @@ require 'test_helper' class ChannelTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + + 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 diff --git a/test/models/feed_test.rb b/test/models/feed_test.rb deleted file mode 100644 index b0fc85d..0000000 --- a/test/models/feed_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'test_helper' - -class FeedTest < ActiveSupport::TestCase - - test "should save feed with valid url" do - feed = Feed.new(url: "https://rss-weather.yahoo.co.jp/rss/days/7320.xml") - assert feed.valid? - end - - test "should not save feed when scheme is not http/https" do - feed = Feed.new(url: "javascript:alert('XSS');//http://shiraishi.jp/") - assert feed.invalid? - end - - test "should not save feed without url" do - feed = Feed.new - assert feed.invalid? - end -end diff --git a/test/controllers/feeds_controller_test.rb b/test/models/item_test.rb similarity index 56% rename from test/controllers/feeds_controller_test.rb rename to test/models/item_test.rb index 0bfc997..f564d81 100644 --- a/test/controllers/feeds_controller_test.rb +++ b/test/models/item_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class FeedsControllerTest < ActionDispatch::IntegrationTest +class ItemTest < ActiveSupport::TestCase # test "the truth" do # assert true # end From 78b1ac71d90f3a75932c47e2b2724ddf624300cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Tue, 20 Mar 2018 19:12:51 +0900 Subject: [PATCH 04/21] =?UTF-8?q?migration=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 2 +- app/models/channel.rb | 1 - db/migrate/20180309033241_create_feeds.rb | 2 -- db/migrate/20180320021944_rename_feeds_to_channels.rb | 1 - ..._create_channels.rb => 20180320093018_create_items.rb} | 4 ++-- db/migrate/20180320093810_add_column_to_channels.rb | 6 ++++++ db/schema.rb | 8 ++++---- test/test_helper.rb | 1 - 8 files changed, 13 insertions(+), 12 deletions(-) rename db/migrate/{20180317100730_create_channels.rb => 20180320093018_create_items.rb} (67%) create mode 100644 db/migrate/20180320093810_add_column_to_channels.rb diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index 4c145a4..f5ac5f5 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -21,7 +21,7 @@ def create private def channel_params - params.require(:channel).permit(:url, :title, :description, items_attributes: [:title, :link, :pubdate]) + params.require(:channel).permit(:url) end end diff --git a/app/models/channel.rb b/app/models/channel.rb index 9b89f4b..39c4d79 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -1,6 +1,5 @@ class Channel < ApplicationRecord has_many :items, dependent: :destroy - accepts_nested_attributes_for :items validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ diff --git a/db/migrate/20180309033241_create_feeds.rb b/db/migrate/20180309033241_create_feeds.rb index 15b0296..ee3fc77 100644 --- a/db/migrate/20180309033241_create_feeds.rb +++ b/db/migrate/20180309033241_create_feeds.rb @@ -2,8 +2,6 @@ class CreateFeeds < ActiveRecord::Migration[5.2] def change create_table :feeds do |t| t.string :url, null: false, default: '', comment: 'RSSフィードのURL' - t.string :title - t.string :description t.timestamps end diff --git a/db/migrate/20180320021944_rename_feeds_to_channels.rb b/db/migrate/20180320021944_rename_feeds_to_channels.rb index 09de4cb..9175f49 100644 --- a/db/migrate/20180320021944_rename_feeds_to_channels.rb +++ b/db/migrate/20180320021944_rename_feeds_to_channels.rb @@ -1,6 +1,5 @@ class RenameFeedsToChannels < ActiveRecord::Migration[5.2] def change - rename_table :channels, :items rename_table :feeds, :channels end end diff --git a/db/migrate/20180317100730_create_channels.rb b/db/migrate/20180320093018_create_items.rb similarity index 67% rename from db/migrate/20180317100730_create_channels.rb rename to db/migrate/20180320093018_create_items.rb index 841c4ba..04fd617 100644 --- a/db/migrate/20180317100730_create_channels.rb +++ b/db/migrate/20180320093018_create_items.rb @@ -1,6 +1,6 @@ -class CreateChannels < ActiveRecord::Migration[5.2] +class CreateItems < ActiveRecord::Migration[5.2] def change - create_table :channels do |t| + create_table :items do |t| t.string :title t.string :link t.datetime :pubdate diff --git a/db/migrate/20180320093810_add_column_to_channels.rb b/db/migrate/20180320093810_add_column_to_channels.rb new file mode 100644 index 0000000..30767b5 --- /dev/null +++ b/db/migrate/20180320093810_add_column_to_channels.rb @@ -0,0 +1,6 @@ +class AddColumnToChannels < ActiveRecord::Migration[5.2] + def change + add_column :channels, :title, :string + add_column :channels, :description, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 7340031..125b48f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,17 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_03_20_021944) 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 "channels", force: :cascade do |t| t.string "url", default: "", null: false, comment: "RSSフィードのURL" - t.string "title" - t.string "description" 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| @@ -33,5 +33,5 @@ t.index ["channel_id"], name: "index_items_on_channel_id" end - add_foreign_key "items", "items", column: "channel_id" + add_foreign_key "items", "channels" end diff --git a/test/test_helper.rb b/test/test_helper.rb index 3ab84e3..b0c4c82 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,7 +4,6 @@ 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 From bc5b4e990b0c5e6eaccf89dc089c3a2d9450d4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Fri, 23 Mar 2018 01:34:56 +0900 Subject: [PATCH 05/21] =?UTF-8?q?=E6=9C=80=E6=96=B0=E6=83=85=E5=A0=B1?= =?UTF-8?q?=E3=81=AE=E5=8F=96=E5=BE=97=E3=83=9C=E3=82=BF=E3=83=B3=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 20 +++++++++++++++++++- app/models/channel.rb | 1 - app/views/channels/index.html.erb | 3 ++- config/routes.rb | 4 +++- test/test_helper.rb | 2 -- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index f5ac5f5..a00ce17 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -3,7 +3,7 @@ # https://rss-weather.yahoo.co.jp/rss/days/7320.xml class ChannelsController < ApplicationController def index - @channels = Channel.channels + @channels = Channel.all end def new @@ -19,6 +19,24 @@ def create end end + def fetch_items + @channels = Channel.channels + @channel = Channel.find(params[:channel_id]) + + @channels.each do |channel| + unless Channel.exists?(:title => channel.title, :description => channel.description) + @channel.update(title: channel.title, description: channel.description) + end + channel.items.each do |fetch| + item = @channel.items.build + unless Item.exists?(:title => fetch.title, :link => fetch.link, :pubdate => fetch.pubDate) + item.update(title: fetch.title, link: fetch.link, pubdate: fetch.pubDate) + end + end + end + redirect_to channels_path + end + private def channel_params params.require(:channel).permit(:url) diff --git a/app/models/channel.rb b/app/models/channel.rb index 39c4d79..f07f534 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -1,6 +1,5 @@ class Channel < ApplicationRecord has_many :items, dependent: :destroy - validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ def self.channels diff --git a/app/views/channels/index.html.erb b/app/views/channels/index.html.erb index 15d4354..af2e25b 100644 --- a/app/views/channels/index.html.erb +++ b/app/views/channels/index.html.erb @@ -1,12 +1,13 @@ <% @channels.each do |channel| %>

<%= channel.title %>

<%= channel.description %>

+ <%= button_to '最新の情報を取得', channel_fetch_items_path(channel), :method => :get %> <% channel.items.each do |item| %>

<%= link_to item.link do %>

<%= item.title %>

<% end %> - <%= time_ago_in_words(item.pubDate) %>前 + <%= time_ago_in_words(item.pubdate) %>前

<% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index bafdf12..ba06f26 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,5 @@ Rails.application.routes.draw do - resources :channels, only: [:index, :new, :create] + resources :channels do + get 'fetch_items' + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index b0c4c82..7e956d1 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,7 +3,5 @@ require 'rails/test_help' class ActiveSupport::TestCase - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. - # Add more helper methods to be used by all tests here... end From 5c0ecd844d00b44d921a8e2168f007bfbfc2966b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Fri, 23 Mar 2018 01:41:57 +0900 Subject: [PATCH 06/21] =?UTF-8?q?routes.rb=E8=A8=98=E8=BF=B0=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index ba06f26..9be1ddb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - resources :channels do + resources :channels, only: [:index, :new, :create] do get 'fetch_items' end end From a9b62c0db5004134c46bd911418d5c7939de712c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Mon, 26 Mar 2018 02:46:41 +0900 Subject: [PATCH 07/21] =?UTF-8?q?fetch=5Fitems=E5=86=85=E4=BF=AE=E6=AD=A3(?= =?UTF-8?q?=E9=80=94=E4=B8=AD=E6=AE=B5=E9=9A=8E)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 16 ++++++---------- app/models/channel.rb | 7 ++++--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index a00ce17..e69588f 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -20,18 +20,14 @@ def create end def fetch_items - @channels = Channel.channels @channel = Channel.find(params[:channel_id]) + channel = RSS::Parser.parse(@channel.url).channel + @channel.update(title: channel.title, description: channel.description) - @channels.each do |channel| - unless Channel.exists?(:title => channel.title, :description => channel.description) - @channel.update(title: channel.title, description: channel.description) - end - channel.items.each do |fetch| - item = @channel.items.build - unless Item.exists?(:title => fetch.title, :link => fetch.link, :pubdate => fetch.pubDate) - item.update(title: fetch.title, link: fetch.link, pubdate: fetch.pubDate) - end + channel.items.each do |fetch| + item = @channel.items.build + unless Item.exists?(title: fetch.title, link: fetch.link, pubdate: fetch.pubDate) + item.update(title: fetch.title, link: fetch.link, pubdate: fetch.pubDate) end end redirect_to channels_path diff --git a/app/models/channel.rb b/app/models/channel.rb index f07f534..1bc608b 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -2,7 +2,8 @@ class Channel < ApplicationRecord has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ - def self.channels - all.map { |channel| RSS::Parser.parse(channel.url).channel } - end + #def self.channels + #all.map { |channel| RSS::Parser.parse(channel.url).channel } + #end + end From 1173a6462934b0f7a666312919ab65265a13849b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Thu, 29 Mar 2018 02:19:54 +0900 Subject: [PATCH 08/21] =?UTF-8?q?fetch=5Fitems=E5=86=85=E3=81=A7changed?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 32 +++++++++++++++++++------- app/models/channel.rb | 3 ++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index e69588f..d119850 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -20,15 +20,31 @@ def create end def fetch_items - @channel = Channel.find(params[:channel_id]) - channel = RSS::Parser.parse(@channel.url).channel - @channel.update(title: channel.title, description: channel.description) - - channel.items.each do |fetch| - item = @channel.items.build - unless Item.exists?(title: fetch.title, link: fetch.link, pubdate: fetch.pubDate) - item.update(title: fetch.title, link: fetch.link, pubdate: fetch.pubDate) + + channel = Channel.find(params[:channel_id]) + fetch_channel = RSS::Parser.parse(channel.url).channel + + channel.title = fetch_channel.title + channel.description = fetch_channel.description + channel.changed? + channel.save + + if channel.items.first == nil + fetch_channel.items.each.map do |fetch_item| + item = channel.items.build + item.title = fetch_item.title + item.link = fetch_item.link + item.pubdate = fetch_item.pubDate + item.save end + else + item = channel.items.first + fetch_item = fetch_channel.items.first + item.title = fetch_item.title + item.link = fetch_item.link + item.pubdate = fetch_item.pubDate + item.changed? + item.save end redirect_to channels_path end diff --git a/app/models/channel.rb b/app/models/channel.rb index 1bc608b..f1bc660 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -1,9 +1,10 @@ class Channel < ApplicationRecord + has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ #def self.channels #all.map { |channel| RSS::Parser.parse(channel.url).channel } #end - + end From c3544d43ad6533a418fb1b333cbe9ce5692ef050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Fri, 30 Mar 2018 06:06:55 +0900 Subject: [PATCH 09/21] =?UTF-8?q?fetch=5Fitems=E5=86=85=E3=81=AE=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E3=82=92=E6=A4=9C=E7=9F=A5=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 42 ++++++++++++-------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index d119850..65681b1 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -1,5 +1,5 @@ require 'rss' -# https://news.yahoo.co.jp/pickup/computer/rss.xml +# https://news.yahoo.co.jp/pickup/rss.xml # https://rss-weather.yahoo.co.jp/rss/days/7320.xml class ChannelsController < ApplicationController def index @@ -20,31 +20,27 @@ def create end def fetch_items - channel = Channel.find(params[:channel_id]) - fetch_channel = RSS::Parser.parse(channel.url).channel - - channel.title = fetch_channel.title - channel.description = fetch_channel.description - channel.changed? - channel.save - - if channel.items.first == nil - fetch_channel.items.each.map do |fetch_item| - item = channel.items.build - item.title = fetch_item.title - item.link = fetch_item.link - item.pubdate = fetch_item.pubDate - item.save + source_channel = RSS::Parser.parse(channel.url).channel + + channel.assign_attributes(title: source_channel.title, description: source_channel.description) + channel.save if channel.changed? + + if channel.items.last + item = channel.items.order(:pubdate).last + source_items = source_channel.items.sort!{ |a,b| b.date <=> a.date } + source_item = source_items.first + item.title = source_item.title + #item.assign_attributes(title: source_item.title, link: source_item.link, pubdate: sou rce_item.pubDate) + if item.changed? + source_channel.items.each do |source_item| + item.update(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) + end end else - item = channel.items.first - fetch_item = fetch_channel.items.first - item.title = fetch_item.title - item.link = fetch_item.link - item.pubdate = fetch_item.pubDate - item.changed? - item.save + source_channel.items.each do |source_item| + channel.items.create(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) + end end redirect_to channels_path end From d1ba0e573a51996257ed8c3444c441a088d51318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Sat, 31 Mar 2018 03:52:36 +0900 Subject: [PATCH 10/21] =?UTF-8?q?fetch=5Fitems=E5=86=85=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=EF=BC=88zip=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E8=A9=A6=E9=A8=93=E6=8E=A1=E7=94=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index 65681b1..e1dddbf 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -26,14 +26,12 @@ def fetch_items channel.assign_attributes(title: source_channel.title, description: source_channel.description) channel.save if channel.changed? - if channel.items.last - item = channel.items.order(:pubdate).last + if channel.items.first + items = Array(channel.items.order("pubdate DESC")) source_items = source_channel.items.sort!{ |a,b| b.date <=> a.date } - source_item = source_items.first - item.title = source_item.title - #item.assign_attributes(title: source_item.title, link: source_item.link, pubdate: sou rce_item.pubDate) - if item.changed? - source_channel.items.each do |source_item| + items.zip(source_items).each do |item, source_item| + item.assign_attributes(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) + if item.changed? item.update(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) end end From 1dd49118fb1513fd997a2a202f4fd18eb57a919b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Mon, 2 Apr 2018 10:46:16 +0900 Subject: [PATCH 11/21] =?UTF-8?q?channel.items.first=E3=81=AB=E3=82=88?= =?UTF-8?q?=E3=82=8B=E5=88=86=E5=B2=90=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 23 ++++++++++------------- app/models/channel.rb | 2 +- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index e1dddbf..1cad390 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -24,20 +24,17 @@ def fetch_items source_channel = RSS::Parser.parse(channel.url).channel channel.assign_attributes(title: source_channel.title, description: source_channel.description) - channel.save if channel.changed? + if channel.changed? + channel.update(title: source_channel.title, description: source_channel.description) + end - if channel.items.first - items = Array(channel.items.order("pubdate DESC")) - source_items = source_channel.items.sort!{ |a,b| b.date <=> a.date } - items.zip(source_items).each do |item, source_item| - item.assign_attributes(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) - if item.changed? - item.update(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) - end - end - else - source_channel.items.each do |source_item| - channel.items.create(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) + items = Array(channel.items.order("pubdate DESC")) + source_items = source_channel.items.sort!{ |a,b| b.date <=> a.date } + source_items.zip(items).each do |source_item, item| + item = channel.items.build unless item + item.assign_attributes(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) + if item.changed? + item.save(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) end end redirect_to channels_path diff --git a/app/models/channel.rb b/app/models/channel.rb index f1bc660..80b2141 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -6,5 +6,5 @@ class Channel < ApplicationRecord #def self.channels #all.map { |channel| RSS::Parser.parse(channel.url).channel } #end - + end From 38748bd6a7fe7327e2816a7220d8d186f9ea4eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Mon, 2 Apr 2018 14:48:47 +0900 Subject: [PATCH 12/21] =?UTF-8?q?fetch=5Fitems=E5=86=85=E3=81=AE=E7=84=A1?= =?UTF-8?q?=E9=A7=84=E5=89=8A=E9=99=A4&=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index 1cad390..24dbd38 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -24,18 +24,13 @@ def fetch_items source_channel = RSS::Parser.parse(channel.url).channel channel.assign_attributes(title: source_channel.title, description: source_channel.description) - if channel.changed? - channel.update(title: source_channel.title, description: source_channel.description) - end + channel.save if channel.changed? - items = Array(channel.items.order("pubdate DESC")) - source_items = source_channel.items.sort!{ |a,b| b.date <=> a.date } - source_items.zip(items).each do |source_item, item| - item = channel.items.build unless item - item.assign_attributes(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) - if item.changed? - item.save(title: source_item.title, link: source_item.link, pubdate: source_item.pubDate) - end + items = channel.items + 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 redirect_to channels_path end From def28463db7a8ba64208ca4407f67bb36a04550e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Wed, 4 Apr 2018 16:36:23 +0900 Subject: [PATCH 13/21] =?UTF-8?q?=E3=83=95=E3=82=A3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E7=99=BB=E9=8C=B2=E6=99=82=E3=81=AB=E8=87=AA=E5=8B=95=E5=8F=96?= =?UTF-8?q?=E5=BE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 16 ++++------------ app/models/channel.rb | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index 24dbd38..a91f27e 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -17,21 +17,13 @@ def create else render 'new' end + channel_id = @channel.id + Channel.channel_item_save(channel_id) end def fetch_items - channel = Channel.find(params[:channel_id]) - source_channel = RSS::Parser.parse(channel.url).channel - - channel.assign_attributes(title: source_channel.title, description: source_channel.description) - channel.save if channel.changed? - - items = channel.items - 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 + channel_id = params[:channel_id] + Channel.channel_item_save(channel_id) redirect_to channels_path end diff --git a/app/models/channel.rb b/app/models/channel.rb index 80b2141..c371707 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -3,8 +3,18 @@ class Channel < ApplicationRecord has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ - #def self.channels - #all.map { |channel| RSS::Parser.parse(channel.url).channel } - #end + def self.channel_item_save(channel_id) + channel = find(channel_id) + source_channel = RSS::Parser.parse(channel.url).channel + channel.assign_attributes(title: source_channel.title, description: source_channel.description) + channel.save if channel.changed? + + items = channel.items + 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 From 097f0fc1d935e81c8b7eb477fc3703a7d021fba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Wed, 4 Apr 2018 23:46:09 +0900 Subject: [PATCH 14/21] =?UTF-8?q?=E3=83=A2=E3=83=87=E3=83=AB=E5=86=85?= =?UTF-8?q?=E3=81=AE=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E5=90=8D=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 2 +- app/models/channel.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index a91f27e..f135fab 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -23,7 +23,7 @@ def create def fetch_items channel_id = params[:channel_id] - Channel.channel_item_save(channel_id) + Channel.items_save(channel_id) redirect_to channels_path end diff --git a/app/models/channel.rb b/app/models/channel.rb index c371707..920c053 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -3,7 +3,7 @@ class Channel < ApplicationRecord has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ - def self.channel_item_save(channel_id) + def self.items_save(channel_id) channel = find(channel_id) source_channel = RSS::Parser.parse(channel.url).channel channel.assign_attributes(title: source_channel.title, description: source_channel.description) From aa9d946dae3c09786d138410746d7072d32f984d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Thu, 5 Apr 2018 10:51:20 +0900 Subject: [PATCH 15/21] =?UTF-8?q?create=E5=86=85=E3=81=AE=E3=83=A1?= =?UTF-8?q?=E3=82=BD=E3=83=83=E3=83=89=E5=90=8D=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index f135fab..fc31944 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -18,7 +18,7 @@ def create render 'new' end channel_id = @channel.id - Channel.channel_item_save(channel_id) + Channel.items_save(channel_id) end def fetch_items From 006f2a9fbb36df52994c4953a77e0e50ba7751d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Sun, 8 Apr 2018 03:12:05 +0900 Subject: [PATCH 16/21] =?UTF-8?q?create=20&=20fetch=5Fitems=E5=86=85?= =?UTF-8?q?=E3=81=AE=E8=A8=98=E8=BF=B0=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 13 ++++++++----- app/models/channel.rb | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index fc31944..2711bc8 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -2,28 +2,27 @@ # https://news.yahoo.co.jp/pickup/rss.xml # https://rss-weather.yahoo.co.jp/rss/days/7320.xml class ChannelsController < ApplicationController + before_action :set_channel,only:[:new, :fetch_items] + def index @channels = Channel.all end def new - @channel = Channel.new end def create @channel = Channel.new(channel_params) if @channel.save + @channel.items_save(@channel.id) redirect_to channels_path else render 'new' end - channel_id = @channel.id - Channel.items_save(channel_id) end def fetch_items - channel_id = params[:channel_id] - Channel.items_save(channel_id) + @channel.items_save(params[:channel_id]) redirect_to channels_path end @@ -32,4 +31,8 @@ def channel_params params.require(:channel).permit(:url) end + def set_channel + @channel = Channel.new + end + end diff --git a/app/models/channel.rb b/app/models/channel.rb index 920c053..40397f8 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -3,8 +3,8 @@ class Channel < ApplicationRecord has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ - def self.items_save(channel_id) - channel = find(channel_id) + def items_save(channel_id) + channel = Channel.find(channel_id) source_channel = RSS::Parser.parse(channel.url).channel channel.assign_attributes(title: source_channel.title, description: source_channel.description) channel.save if channel.changed? From df2b593f064eba09f26ced7eddb5188966353d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Mon, 9 Apr 2018 16:24:14 +0900 Subject: [PATCH 17/21] =?UTF-8?q?items=5Fsave=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=81=AB=E9=96=A2=E3=81=99=E3=82=8B=E8=A8=98=E8=BF=B0?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 11 ++++------- app/models/channel.rb | 3 +-- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index 2711bc8..e5c7195 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -2,19 +2,19 @@ # https://news.yahoo.co.jp/pickup/rss.xml # https://rss-weather.yahoo.co.jp/rss/days/7320.xml class ChannelsController < ApplicationController - before_action :set_channel,only:[:new, :fetch_items] def index @channels = Channel.all end def new + @channel = Channel.new end def create @channel = Channel.new(channel_params) if @channel.save - @channel.items_save(@channel.id) + Channel.new.items_save(@channel) redirect_to channels_path else render 'new' @@ -22,7 +22,8 @@ def create end def fetch_items - @channel.items_save(params[:channel_id]) + channel = Channel.find(params[:channel_id]) + Channel.new.items_save(channel) redirect_to channels_path end @@ -31,8 +32,4 @@ def channel_params params.require(:channel).permit(:url) end - def set_channel - @channel = Channel.new - end - end diff --git a/app/models/channel.rb b/app/models/channel.rb index 40397f8..7d35356 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -3,8 +3,7 @@ class Channel < ApplicationRecord has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ - def items_save(channel_id) - channel = Channel.find(channel_id) + def items_save(channel) source_channel = RSS::Parser.parse(channel.url).channel channel.assign_attributes(title: source_channel.title, description: source_channel.description) channel.save if channel.changed? From 19e8f724ef06750a4d05e00f4a11cbcd7dd2447e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Mon, 16 Apr 2018 12:40:47 +0900 Subject: [PATCH 18/21] =?UTF-8?q?items=5Fsave=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=81=AB=E9=96=A2=E3=81=99=E3=82=8B=E8=A8=98=E8=BF=B0?= =?UTF-8?q?=E5=86=8D=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 11 +++++++---- app/models/channel.rb | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index e5c7195..2711bc8 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -2,19 +2,19 @@ # https://news.yahoo.co.jp/pickup/rss.xml # https://rss-weather.yahoo.co.jp/rss/days/7320.xml class ChannelsController < ApplicationController + before_action :set_channel,only:[:new, :fetch_items] def index @channels = Channel.all end def new - @channel = Channel.new end def create @channel = Channel.new(channel_params) if @channel.save - Channel.new.items_save(@channel) + @channel.items_save(@channel.id) redirect_to channels_path else render 'new' @@ -22,8 +22,7 @@ def create end def fetch_items - channel = Channel.find(params[:channel_id]) - Channel.new.items_save(channel) + @channel.items_save(params[:channel_id]) redirect_to channels_path end @@ -32,4 +31,8 @@ def channel_params params.require(:channel).permit(:url) end + def set_channel + @channel = Channel.new + end + end diff --git a/app/models/channel.rb b/app/models/channel.rb index 7d35356..6036a17 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -3,7 +3,8 @@ class Channel < ApplicationRecord has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ - def items_save(channel) + def items_save(channel_id) + channel = self.class.find(channel_id) source_channel = RSS::Parser.parse(channel.url).channel channel.assign_attributes(title: source_channel.title, description: source_channel.description) channel.save if channel.changed? From 69e006169cf766dfd9c0e9eed831b86eff57b7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Tue, 17 Apr 2018 02:56:01 +0900 Subject: [PATCH 19/21] =?UTF-8?q?items=5Fsave=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E4=BF=AE=E6=AD=A3=EF=BC=88create,=20fetch=5Fitems?= =?UTF-8?q?=E3=82=A2=E3=82=AF=E3=82=B7=E3=83=A7=E3=83=B3=E5=86=85=E3=82=82?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 11 ++++------- app/models/channel.rb | 12 ++++++------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index 2711bc8..d169111 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -2,19 +2,19 @@ # https://news.yahoo.co.jp/pickup/rss.xml # https://rss-weather.yahoo.co.jp/rss/days/7320.xml class ChannelsController < ApplicationController - before_action :set_channel,only:[:new, :fetch_items] def index @channels = Channel.all end def new + @channel = Channel.new end def create @channel = Channel.new(channel_params) if @channel.save - @channel.items_save(@channel.id) + @channel.items_save redirect_to channels_path else render 'new' @@ -22,7 +22,8 @@ def create end def fetch_items - @channel.items_save(params[:channel_id]) + channel = Channel.find(params[:channel_id]) + channel.items_save redirect_to channels_path end @@ -31,8 +32,4 @@ def channel_params params.require(:channel).permit(:url) end - def set_channel - @channel = Channel.new - end - end diff --git a/app/models/channel.rb b/app/models/channel.rb index 6036a17..3133589 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -1,15 +1,15 @@ class Channel < ApplicationRecord + attr_accessor :channel has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ - def items_save(channel_id) - channel = self.class.find(channel_id) - source_channel = RSS::Parser.parse(channel.url).channel - channel.assign_attributes(title: source_channel.title, description: source_channel.description) - channel.save if channel.changed? + def items_save + source_channel = RSS::Parser.parse(self.url).channel + self.assign_attributes(title: source_channel.title, description: source_channel.description) + self.save if self.changed? - items = channel.items + items = self.items 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) From 5e084a68600787a5f32d563c2443163c93ca542d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Tue, 17 Apr 2018 16:15:20 +0900 Subject: [PATCH 20/21] =?UTF-8?q?=E6=B6=88=E3=81=97=E5=BF=98=E3=82=8C?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/channel.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/models/channel.rb b/app/models/channel.rb index 3133589..8f45b14 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -1,5 +1,4 @@ class Channel < ApplicationRecord - attr_accessor :channel has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ From 0938d9111ac3efa6695163550f976a6ee12b0ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=9F=B3=E9=9A=86=E4=BA=8C?= Date: Wed, 18 Apr 2018 11:46:21 +0900 Subject: [PATCH 21/21] =?UTF-8?q?update=5Fitems=E3=81=AB=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=80=81self=E7=9C=81=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/channels_controller.rb | 4 ++-- app/models/channel.rb | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index d169111..c524fa0 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -14,7 +14,7 @@ def new def create @channel = Channel.new(channel_params) if @channel.save - @channel.items_save + @channel.update_items redirect_to channels_path else render 'new' @@ -23,7 +23,7 @@ def create def fetch_items channel = Channel.find(params[:channel_id]) - channel.items_save + channel.update_items redirect_to channels_path end diff --git a/app/models/channel.rb b/app/models/channel.rb index 8f45b14..540853b 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -3,12 +3,11 @@ class Channel < ApplicationRecord has_many :items, dependent: :destroy validates :url, format: /\A#{URI::regexp(%w(http https))}\z/ - def items_save - source_channel = RSS::Parser.parse(self.url).channel - self.assign_attributes(title: source_channel.title, description: source_channel.description) - self.save if self.changed? + def update_items + source_channel = RSS::Parser.parse(url).channel + assign_attributes(title: source_channel.title, description: source_channel.description) + save if changed? - items = self.items 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)