Skip to content

Commit

Permalink
Merge pull request #46 from wponrails/feature/unpublish
Browse files Browse the repository at this point in the history
[READY] Add unpublish method
  • Loading branch information
Marthyn committed Mar 9, 2016
2 parents 856b190 + f5e4040 commit ca54eb9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
19 changes: 13 additions & 6 deletions app/models/concerns/wp_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create_or_update(wp_type, wp_id, preview = false)
# WP API will return a code if the route is incorrect or
# the specified entry is none existant. If so return early.
return if wp_json[0] and invalid_api_responses.include? wp_json[0]["code"]
where(wp_id: wp_id).first_or_initialize.update_wp_cache(wp_json)
unscoped.where(wp_id: wp_id).first_or_initialize.update_wp_cache(wp_json)
end

def update_options
Expand Down Expand Up @@ -81,34 +81,41 @@ def create_or_update_all_paginated
break if wp_json.empty?
ids << wp_json.map do |json|
wp_id = json['ID']
where(wp_id: wp_id).first_or_initialize.update_wp_cache(json)
unscoped.where(wp_id: wp_id).first_or_initialize.update_wp_cache(json)
wp_id
end
page = page + 1
end
where('wp_id NOT IN (?)', ids.flatten).destroy_all unless ids.empty?
unscoped.where('wp_id NOT IN (?)', ids.flatten).destroy_all unless ids.empty?
end

# TODO (dunyakirkali) doc
def create_or_update_all_non_paginated
wp_json = get_from_wp_api(wp_type)
ids = wp_json.map do |json|
wp_id = json['ID']
where(wp_id: wp_id).first_or_initialize.update_wp_cache(json)
unscoped.where(wp_id: wp_id).first_or_initialize.update_wp_cache(json)
wp_id
end
where('wp_id NOT IN (?)', ids).destroy_all unless ids.empty?
unscoped.where('wp_id NOT IN (?)', ids).destroy_all unless ids.empty?
end

#
# Purge a cached piece of content, while logging any exceptions.
#
def purge(wp_id)
where(wp_id: wp_id).first!.destroy
unscoped.where(wp_id: wp_id).first!.destroy
rescue
logger.warn "Could not purge #{self} with id #{wp_id}, no record with that id was found."
end

def unpublish(wp_id)
where(wp_id: wp_id).first!.update_attribute(:status, "draft")
rescue
logger.warn "Could not unpublish #{self} with id #{wp_id}, no record with that id was found."
end


private

#
Expand Down
1 change: 0 additions & 1 deletion app/workers/wp_api_worker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'sidekiq'

#
# This worker is used to schedule a `create_or_update` class method call
# on the provided model for ASAP.
Expand Down

0 comments on commit ca54eb9

Please sign in to comment.