Skip to content

Commit

Permalink
Merge pull request #15 from Gaddict/master
Browse files Browse the repository at this point in the history
削除系の機能追加 #4
  • Loading branch information
pollseed committed Sep 12, 2015
2 parents e5e2c09 + 7257da2 commit 2d05377
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 13 deletions.
12 changes: 10 additions & 2 deletions app/controllers/bot/bot_omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/bot/bots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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です")
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/bot/hash_tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/models/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/hash_tag.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/bot/bots/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<% end %>
</td>
<td><%= button_to "編集", edit_bot_path(current_user.username, b.id), class: 'btn btn-warning', method: :get %></td>
<td><%= button_to "削除", "bot/#{b.id}", class: 'btn btn-danger', method: :delete %></td>
<td><%= button_to "削除", destroy_bot_path(current_user.username, b.id), class: 'btn btn-danger', method: :delete %></td>
</tr>
<%end%>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/bot/hash_tags/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
<tr>
<th>No.</th>
<th>HashTag</th>
<th>削除</th>
</tr>
<% HashTag.all.each_with_index do |b, cnt| %>
<tr>
<td><%= cnt+1 %></td>
<td><%= b.hash_tag %></td>
<td><%= button_to "削除", hash_tag_path(b.id), class: 'btn btn-danger', method: :delete %></td>
</tr>
<% end %>
</table>
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<!-- current_user は現在ログインしているUserオブジェクトを返すdeviseのHelperメソッド -->
<!-- *_path はUserモデルを作成したときに、
deviseにより自動で作成されてますので、rake routesで確認できます -->
Logged in as <strong><%=link_to current_user.username, bots_path(current_user.username)%></strong>
<%= link_to 'プロフィール変更', edit_user_registration_path(current_user.username) %> |
Logged in as <strong><%=link_to current_user.username, bots_path(current_user.username)%></strong>|
<%= link_to 'HashTagの登録', hash_tags_path %> |
<%= link_to "ログアウト", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to image_tag("signin_with_twitter_logo.png", :size => "188x30", :class => "twitter_layout"), user_omniauth_authorize_path(:twitter) %>
Expand Down
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

root to: "home#index"

resources :hash_tags, :module => :bot, :except => [:show, :edit, :update, :destroy]
resources :hash_tags, :module => :bot, :except => [:show, :edit, :update]

devise_scope :user do
# sessions
Expand All @@ -25,9 +25,10 @@
delete ':username' => 'devise/registrations#destroy'
end

resources :bots, :module => :bot, :path => ':username', :except => [:edit, :new, :create]
resources :bots, :module => :bot, :path => ':username', :except => [:edit, :new, :create, :destroy]
match ":username/:id/settings", :to => 'bot/bots#edit', :via => :get, :as => :edit_bot
match "new", :to => 'bot/bots#new', :via => :get, :as => :new_bot
match ":username/:id", :to => 'bot/bots#destroy', :via => :delete, :as => :destroy_bot
match ":username/bots", :to => 'bot/bots#create', :via => :post, :as => :create_bot

match 'bots/auth/twitter/callback', :to => 'bot/bot_omniauth_callbacks#callback', :via => [:get, :post], :as => :bot_omniauth_callback
Expand Down

0 comments on commit 2d05377

Please sign in to comment.