Skip to content

Commit

Permalink
Update commands for new documents table
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasleese committed Dec 21, 2016
1 parent 9a6b6b3 commit 62c0dc7
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 49 deletions.
12 changes: 6 additions & 6 deletions app/commands/v2/discard_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ def increment_live_lock_version
end

def draft
@draft ||= ContentItem.find_by(
content_id: content_id,
locale: locale,
@draft ||= ContentItem.joins(:document).find_by(
'documents.content_id': content_id,
'documents.locale': locale,
state: "draft",
)
end

def live
@live ||= ContentItem.find_by(
content_id: content_id,
locale: locale,
@live ||= ContentItem.joins(:document).find_by(
'documents.content_id': content_id,
'documents.locale': locale,
state: %w(published unpublished),
)
end
Expand Down
2 changes: 1 addition & 1 deletion app/commands/v2/patch_link_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def schema_validator

def schema_name
@schema_name ||= Queries::GetLatest.(
ContentItem.where(content_id: payload[:content_id])
ContentItem.where('documents.content_id': payload[:content_id])
).pluck(:schema_name).first
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/commands/v2/post_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def action_type
end

def find_content_item
content_item = ContentItem.find_by(
content_id: content_id,
locale: locale,
content_item = ContentItem.joins(:document).find_by(
'documents.content_id': content_id,
'documents.locale': locale,
state: draft? ? %w(draft) : %w(published unpublished),
)

Expand Down
17 changes: 9 additions & 8 deletions app/commands/v2/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,19 @@ def valid_update_types

def find_draft_content_item
ContentItem.find_by(
id: pessimistic_content_item_scope.pluck(:id),
document_id: pessimistic_document_scope.pluck(:id),
state: "draft",
)
end

def already_published?
ContentItem.exists?(content_id: content_id, locale: locale, state: "published")
ContentItem.joins(:document)
.exists?("documents.content_id": content_id,
"documents.locale": locale, state: "published")
end

def pessimistic_content_item_scope
ContentItem.where(content_id: content_id, locale: locale).lock
def pessimistic_document_scope
Document.where(content_id: content_id, locale: locale).lock
end

def clear_published_items_of_same_locale_and_base_path(content_item, locale, base_path)
Expand Down Expand Up @@ -127,9 +129,9 @@ def set_first_published_at(content_item)
end

def publish_redirect(previous_base_path, locale)
draft_redirect = ContentItem.find_by(
draft_redirect = ContentItem.joins(:document).find_by(
state: "draft",
locale: locale,
"documents.locale": locale,
base_path: previous_base_path,
schema_name: "redirect",
)
Expand All @@ -148,8 +150,7 @@ def publish_redirect(previous_base_path, locale)

def lookup_previous_item
previous_items = ContentItem.where(
content_id: content_id,
locale: locale,
document_id: pessimistic_document_scope.pluck(:id),
state: %w(published unpublished),
)

Expand Down
12 changes: 6 additions & 6 deletions app/commands/v2/put_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def create_or_update_access_limit(content_item, users:)

def find_previously_drafted_content_item
ContentItem.find_by(
id: pessimistic_content_item_scope.pluck(:id),
document_id: pessimistic_document_scope.pluck(:id),
state: "draft",
)
end
Expand Down Expand Up @@ -167,16 +167,16 @@ def user_facing_version_number_for_new_draft
end
end

def pessimistic_content_item_scope
ContentItem.where(content_id: content_id, locale: locale).lock
def pessimistic_document_scope
Document.where(content_id: content_id, locale: locale).lock
end

def previously_published_item
@previously_published_item ||=
ContentItem.find_by(
content_id: content_id,
ContentItem.joins(:document).find_by(
"documents.content_id": content_id,
state: %w(published unpublished),
locale: locale,
"documents.locale": locale,
) || ITEM_NOT_FOUND
end

Expand Down
18 changes: 9 additions & 9 deletions app/commands/v2/unpublish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ def find_unpublishable_content_item
allowed_states = %w(draft)
end

content_item = ContentItem.where(
content_id: content_id,
locale: locale,
content_item = ContentItem.joins(:document).where(
'documents.content_id': content_id,
'documents.locale': locale,
state: allowed_states
).lock.first

Expand All @@ -139,17 +139,17 @@ def previous_item
end

def previous_items
@previous_items ||= ContentItem.where(
content_id: content_id,
locale: locale,
@previous_items ||= ContentItem.joins(:document).where(
'documents.content_id': content_id,
'documents.locale': locale,
state: %w(published unpublished),
)
end

def draft_exists?
ContentItem.where(
content_id: content_id,
locale: locale,
ContentItem.joins(:document).where(
'documents.content_id': content_id,
'documents.locale': locale,
state: "draft",
).exists?
end
Expand Down
3 changes: 2 additions & 1 deletion app/presenters/change_history_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def change_notes_for_content_item
end

def content_item_ids
ContentItem.where(content_id: content_item.content_id)
ContentItem.joins(:document)
.where('documents.content_id': content_item.content_id)
.where("user_facing_version <= ?", version_number)
.pluck(:id)
end
Expand Down
5 changes: 3 additions & 2 deletions app/presenters/queries/available_translations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def translations
attr_reader :content_id, :state_fallback_order, :expanded_translations

def grouped_translations
ContentItem.where(content_id: content_id, state: state_fallback_order)
.pluck(:id, :locale, :state)
ContentItem.joins(:document)
.where('documents.content_id': content_id, state: state_fallback_order)
.pluck(:id, 'documents.locale', :state)
.sort_by { |(_, _, state)| state_fallback_order.index(state.to_sym) }
.group_by { |(_, locale)| locale }
end
Expand Down
12 changes: 7 additions & 5 deletions app/queries/get_content_item_ids_with_fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ def self.call(content_ids, locale_fallback_order: ContentItem::DEFAULT_LOCALE, s
state_fallback_order = Array(state_fallback_order).map(&:to_s)
locale_fallback_order = Array(locale_fallback_order).map(&:to_s)

documents = Document.arel_table
content_items = ContentItem.arel_table
unpublishings = Unpublishing.arel_table

fallback_scope = content_items.project(
content_items[:id],
content_items[:content_id],
content_items[:id],
documents[:content_id],
)
.where(content_items[:content_id].in(content_ids))
.where(documents[:content_id].in(content_ids))
.where(content_items[:document_type].not_in(::ContentItem::NON_RENDERABLE_FORMATS))
.where(content_items[:locale].in(locale_fallback_order))
.where(documents[:locale].in(locale_fallback_order))
.join(documents).on(documents[:id].eq(content_items[:document_id]))

if state_fallback_order.include?("withdrawn")
fallback_scope = fallback_scope.where(content_items[:state].in(state_fallback_order).or(content_items[:state]
Expand All @@ -29,7 +31,7 @@ def self.call(content_ids, locale_fallback_order: ContentItem::DEFAULT_LOCALE, s

fallback_scope = fallback_scope.order(
order_by_clause(:content_items, :state, state_fallback_order),
order_by_clause(:content_items, :locale, locale_fallback_order)
order_by_clause(:documents, :locale, locale_fallback_order)
)

fallbacks = cte(fallback_scope, as: "fallbacks")
Expand Down
11 changes: 7 additions & 4 deletions app/queries/get_web_content_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ def self.find(content_item_id)
end

def self.for_content_store(content_id, locale, include_draft = false)
documents = Document.arel_table
content_items = ContentItem.arel_table
unpublishings = Unpublishing.arel_table

allowed_states = [:published, :unpublished]
allowed_states << :draft if include_draft
filtered = scope(content_items[:user_facing_version].desc)
.where(content_items[:content_id].eq(content_id))
.where(content_items[:locale].eq(locale))
.where(documents[:content_id].eq(content_id))
.where(documents[:locale].eq(locale))
.where(content_items[:state].in(allowed_states))
.where(
unpublishings[:type].eq(nil).or(
Expand All @@ -38,14 +39,15 @@ def self.for_content_store(content_id, locale, include_draft = false)
end

def self.scope(order = nil)
documents = Document.arel_table
content_items = ContentItem.arel_table
unpublishings = Unpublishing.arel_table

content_items
.project(
content_items[:id],
content_items[:analytics_identifier],
content_items[:content_id],
documents[:content_id],
content_items[:description],
content_items[:details],
content_items[:document_type],
Expand All @@ -63,10 +65,11 @@ def self.scope(order = nil)
content_items[:update_type],
content_items[:base_path],
content_items[:state],
content_items[:locale],
documents[:locale],
content_items[:user_facing_version],
unpublishings[:type].as("unpublishing_type")
)
.join(documents).on(content_items[:document_id].eq(documents[:id]))
.outer_join(unpublishings).on(
content_items[:id].eq(unpublishings[:content_item_id])
.and(content_items[:state].eq("unpublished"))
Expand Down
3 changes: 1 addition & 2 deletions app/validators/state_for_locale_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ def validate(record)
return unless record.state && record.locale && %w(draft published unpublished).include?(record.state)

criteria = {
content_id: record.content_id,
document: record.document,
state: record.state == "draft" ? "draft" : %w(published unpublished),
locale: record.locale,
}

conflict = ContentItem.where(criteria).where.not(id: record.id).first
Expand Down
3 changes: 1 addition & 2 deletions app/validators/version_for_locale_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ def validate(record)
return unless record.locale && record.user_facing_version

criteria = {
content_id: record.content_id,
document: record.document,
user_facing_version: record.user_facing_version,
locale: record.locale,
}

conflict = ContentItem.where(criteria).where.not(id: record.id).first
Expand Down

0 comments on commit 62c0dc7

Please sign in to comment.