diff --git a/app/controllers/bot/bot_omniauth_callbacks_controller.rb b/app/controllers/bot/bot_omniauth_callbacks_controller.rb index 35b1b09..89561ae 100644 --- a/app/controllers/bot/bot_omniauth_callbacks_controller.rb +++ b/app/controllers/bot/bot_omniauth_callbacks_controller.rb @@ -38,8 +38,16 @@ def callback session[:bot] = bot - unless Bot.find_by_twitter_id(bot["twitter_id"]).nil? - redirect_to(new_bot_path, alert: "すでに登録済みです") + already_bot = Bot.find_by_twitter_id(bot["twitter_id"]) + + unless already_bot.nil? + unless already_bot.deleted + redirect_to(new_bot_path, alert: "すでに登録済みです") + else + already_bot.deleted = false + already_bot.save + redirect_to bots_path(current_user.username) + end else redirect_to new_bot_path end diff --git a/app/controllers/bot/bots_controller.rb b/app/controllers/bot/bots_controller.rb index 9f1c5ae..ed6c8fd 100644 --- a/app/controllers/bot/bots_controller.rb +++ b/app/controllers/bot/bots_controller.rb @@ -33,13 +33,13 @@ def create end def edit - id = params.require(:id) - @bot = Bot.find_by(id) + bot_param = params.permit(:id) + @bot = Bot.find_by(bot_param) end def update hash_tag_ids = params[:hash_tags] - @bot = Bot.find_by(params.require(:id)) + @bot = Bot.find_by(params.permit(:id)) unless register_hashtags(hash_tag_ids) redirect_to(edit_bot_path(current_user.username, @bot.id), alert: "存在しないHashTagです") @@ -119,7 +119,7 @@ def register_hashtags(hash_tag_ids) end hash_tag_ids.each do |hash_tag_id| - hash_tag = HashTag.find_by({:id => hash_tag_id}) + hash_tag = HashTag.find_by({:id => hash_tag_id.to_i}) if @bot.nil? || hash_tag.nil? return false elsif @bot.hash_tags.include?(hash_tag) diff --git a/app/controllers/bot/hash_tags_controller.rb b/app/controllers/bot/hash_tags_controller.rb index 5a81e89..cae7b5d 100644 --- a/app/controllers/bot/hash_tags_controller.rb +++ b/app/controllers/bot/hash_tags_controller.rb @@ -13,4 +13,13 @@ def create redirect_to(new_hash_tag_path, alert: "すでに登録されているHashTagです") end end + + def destroy + hash_tag_param = params.permit(:id) + hash_tag = HashTag.find_by(hash_tag_param) + unless hash_tag.nil? + hash_tag.destroy + end + redirect_to hash_tags_path + end end diff --git a/app/models/bot.rb b/app/models/bot.rb index 2e68a5b..135fc6e 100644 --- a/app/models/bot.rb +++ b/app/models/bot.rb @@ -6,7 +6,7 @@ class Bot < ActiveRecord::Base belongs_to :user has_many :bot_hash_tag_rels - has_many :hash_tags, :through => :bot_hash_tag_rels + has_many :hash_tags, :through => :bot_hash_tag_rels, :dependent => :destroy has_one :schedule, :dependent => :destroy validates :twitter_name, presence: true diff --git a/app/models/hash_tag.rb b/app/models/hash_tag.rb index 51bb7b9..185b7e3 100644 --- a/app/models/hash_tag.rb +++ b/app/models/hash_tag.rb @@ -1,6 +1,6 @@ class HashTag < ActiveRecord::Base has_many :bot_hash_tag_rels - has_many :bots, :through => :bot_hash_tag_rels + has_many :bots, :through => :bot_hash_tag_rels, :dependent => :destroy # 論理削除されていないレコードを全取得 def self.find_by_alive diff --git a/app/views/bot/bots/index.html.erb b/app/views/bot/bots/index.html.erb index c1d1d59..9400f14 100644 --- a/app/views/bot/bots/index.html.erb +++ b/app/views/bot/bots/index.html.erb @@ -31,7 +31,7 @@ <% end %>