Skip to content

Commit

Permalink
Merge pull request #81 from lepo-project/fix-web_page-destroy
Browse files Browse the repository at this point in the history
🔧 Fix web_page destroy
  • Loading branch information
kyoshizaki authored Oct 9, 2017
2 parents f1f85cc + 72d9dd9 commit a633ef5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 0 additions & 5 deletions app/controllers/snippets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ def ajax_destroy
note_id = params[:note_id].to_i if params[:note_id]
snippet.destroy

if snippet.source_type == 'web'
source_id = snippet.source_id
WebPage.find(source_id).destroy if Snippet.where(source_type: 'web', source_id: source_id).count.zero?
end

if note_id
render_snippets note_id
else
Expand Down
4 changes: 2 additions & 2 deletions app/models/bookmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Bookmark < ApplicationRecord
validates_presence_of :target_id
validates_inclusion_of :target_type, in: %w[web]
validates_uniqueness_of :display_title, scope: [:manager_id]
before_destroy :destroy_target
after_destroy :destroy_target

# ====================================================================
# Public Functions
Expand Down Expand Up @@ -55,7 +55,7 @@ def url
def destroy_target
case target_type
when 'web'
target.destroy if target.bookmarks.size == 1 && target.snippets.size.zero?
target.destroy if target.deletable?
end
end
end
13 changes: 13 additions & 0 deletions app/models/snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Snippet < ApplicationRecord
validates_inclusion_of :category, in: %w[text image pdf scratch ted youtube], if: "source_type == 'web'"
validates_inclusion_of :source_type, in: %w[direct upload web]
validates_format_of :description, with: /\.(gif|jpe?g|png)/i, message: 'must have an image extension', if: "source_type == 'web' && category == 'image'"
after_destroy :destroy_source

# ====================================================================
# Public Functions
Expand Down Expand Up @@ -105,4 +106,16 @@ def transferable?(user_id, to_note_id = nil)
Note.find_by(id: to_note_id).manager_id == user_id
end
end

# ====================================================================
# Private Functions
# ====================================================================
private

def destroy_source
case source_type
when 'web'
source.destroy if source.deletable?
end
end
end
4 changes: 4 additions & 0 deletions app/models/web_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ def self.youtube_id(url)
Regexp.last_match(5)
end
end

def deletable?
(bookmarks.size + snippets.size).zero?
end
end

0 comments on commit a633ef5

Please sign in to comment.