Skip to content

Commit 254713d

Browse files
author
Alex Tharp
authored
Merge branch 'develop' into bugfix/AR-66-Broken-Pagination-Headers
2 parents 59996c3 + 7b2f3b1 commit 254713d

File tree

11 files changed

+351
-25
lines changed

11 files changed

+351
-25
lines changed

app/api/v1/entities/content_item.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ module Entities
33
class ContentItem < Grape::Entity
44
expose :id, documentation: {type: "Integer", desc: "Content Item ID", required: true}
55
expose :publish_state, documentation: {type: "String", desc: "Publish state", required: true}
6-
expose :published_at, documentation: {type: "dateTime", desc: "Date published", required: true}
7-
expose :expired_at, documentation: {type: "dateTime", desc: "Date to expire", required: true}
8-
expose :author, documentation: {type: "dateTime", desc: "Date published", required: true} do |content_item|
9-
content_item.author.fullname
10-
end
116
expose :creator, documentation: {type: "dateTime", desc: "Date published", required: true} do |content_item|
127
content_item.creator.fullname
138
end

app/api/v1/resources/content_items.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
module V1
22
module Resources
33
class ContentItems < Grape::API
4+
helpers ::V1::Helpers::SharedParamsHelper
5+
helpers ::V1::Helpers::ParamsHelper
6+
47
resource :content_items do
8+
include Grape::Kaminari
9+
paginate per_page: 25
10+
511
desc "Create a content item", { entity: ::V1::Entities::ContentItem, params: ::V1::Entities::ContentItem.documentation, nickname: "createContentItem" }
612
params do
713
requires :content_type_id, type: Integer, desc: "content type of content item"
814
end
915
post do
10-
require_scope! 'create:content_items'
16+
require_scope! 'modify:content_items'
1117
authorize! :create, ::ContentItem
1218
@content_item = ::ContentItem.new(params.merge(author_id: current_user.id, creator_id: current_user.id))
1319

@@ -27,6 +33,37 @@ class ContentItems < Grape::API
2733

2834
present @content_items, with: ::V1::Entities::ContentItem, field_items: true
2935
end
36+
37+
desc 'Show published content items', { entity: ::V1::Entities::ContentItem, nickname: "contentItemsFeed" }
38+
params do
39+
use :pagination
40+
requires :content_type_name, type: String, desc: 'ContentType of ContentItem'
41+
end
42+
get :feed do
43+
require_scope! 'view:content_items'
44+
authorize! :view, ::ContentItem
45+
46+
last_updated_at = ContentItem.last_updated_at
47+
params_hash = Digest::MD5.hexdigest(declared(params).to_s)
48+
cache_key = "feed-#{last_updated_at}-#{current_tenant.id}-#{params_hash}"
49+
50+
content_items = ::Rails.cache.fetch(cache_key, expires_in: 30.minutes, race_condition_ttl: 10) do
51+
content_items = ::GetContentItems.call(params: declared(clean_params(params), include_missing: false), tenant: current_tenant, published: true).content_items
52+
paginated_content_items = paginate(content_items).records.to_a
53+
{records: paginated_content_items, headers: header}
54+
end
55+
56+
header.merge!(content_items[:headers])
57+
::V1::Entities::ContentItem.represent content_items[:records], field_items: true
58+
end
59+
60+
desc 'Show a published content item', { entity: ::V1::Entities::ContentItem, nickname: "showFeedContentItem" }
61+
get 'feed/*id' do
62+
@content_item = ::GetContentItem.call(id: params[:id], published: true, tenant: current_tenant.id).content_item
63+
not_found! unless @content_item
64+
authorize! :view, @content_item
65+
present @content_item, with: ::V1::Entities::ContentItem, field_items: true
66+
end
3067
end
3168
end
3269
end

app/assets/javascripts/legacy/controllers/webpages/edit.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,16 @@ angular.module('cortex.controllers.webpages.edit', [
7272
page: page.page
7373
});
7474
}
75+
76+
$scope.appendEditingParamsToUrl = function(url) {
77+
var urlHasParams = _.includes(url, '?');
78+
79+
if (urlHasParams) {
80+
url = url + '&editing_mode=1&disable_redirects=1';
81+
} else {
82+
url = url + '?editing_mode=1&disable_redirects=1';
83+
}
84+
85+
return url;
86+
}
7587
});

app/assets/legacy_templates/webpages/edit.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@
129129
</tab>
130130
</tabset>
131131

132-
<iframe class="webpage-frame" id="webpage-frame"
133-
ng-src="{{ data.webpage.url + '?editing_mode=1' | trustAsResourceUrl }}" disabled></iframe>
132+
<iframe ng-if="data.webpage.url" class="webpage-frame" id="webpage-frame"
133+
ng-src="{{ appendEditingParamsToUrl(data.webpage.url) | trustAsResourceUrl }}" disabled></iframe>
134134
</div>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class GetContentItem
2+
include Interactor
3+
4+
def call
5+
content_item = ::ContentItem
6+
#content_item = content_item.find_by_tenant_id(context.tenant) if context.tenant
7+
#content_item = content_item.published if context.published
8+
#content_item = content_item.find_by_id_or_slug(context.id)
9+
content_item = content_item.find_by_id(context.id)
10+
context.content_item = content_item
11+
end
12+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class GetContentItems
2+
include Interactor
3+
4+
def call
5+
content_items = ContentType.find_by_name(context.params.content_type_name.titleize).content_items.order(created_at: :desc)
6+
context.content_items = content_items
7+
end
8+
end

app/models/content_item.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,7 @@ class ContentItem < ApplicationRecord
33
include Elasticsearch::Model
44
include Elasticsearch::Model::Callbacks
55

6-
state_machine do
7-
state :draft
8-
state :scheduled
9-
10-
event :schedule do
11-
transitions :to => :scheduled, :from => [:draft]
12-
end
13-
14-
event :draft do
15-
transitions :to => :draft, :from => [:scheduled]
16-
end
17-
end
6+
scope :last_updated_at, -> { order(updated_at: :desc).select('updated_at').first.updated_at }
187

198
acts_as_paranoid
209

@@ -32,6 +21,19 @@ class ContentItem < ApplicationRecord
3221
after_save :index
3322
after_save :update_tag_lists
3423

24+
state_machine do
25+
state :draft
26+
state :scheduled
27+
28+
event :schedule do
29+
transitions :to => :scheduled, :from => [:draft]
30+
end
31+
32+
event :draft do
33+
transitions :to => :draft, :from => [:scheduled]
34+
end
35+
end
36+
3537
def self.taggable_fields
3638
Field.select { |field| field.field_type_instance.is_a?(TagFieldType) }.map { |field_item| field_item.name.parameterize('_') }
3739
end

config/initializers/doorkeeper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
optional_scopes 'view:users', 'modify:users', 'view:tenants', 'modify:tenants', 'view:posts',
4040
'modify:posts', 'view:media', 'modify:media', 'view:applications', 'modify:applications',
4141
'view:bulk_jobs', 'modify:bulk_jobs', 'view:documents', 'modify:documents',
42-
'view:snippets', 'modify:snippets', 'view:webpages', 'modify:webpages', 'view:content_types'
42+
'view:snippets', 'modify:snippets', 'view:webpages', 'modify:webpages', 'view:content_types',
43+
'modify:content_types', 'view:content_items', 'modify:content_items'
4344

4445
# Change the way client credentials are retrieved from the request object.
4546
# By default it retrieves first from the `HTTP_AUTHORIZATION` header, then

db/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,8 @@
472472
t.string "dynamic_yield_sku"
473473
t.string "dynamic_yield_category"
474474
t.jsonb "tables_widget"
475-
t.jsonb "accordion_group_widget"
476475
t.jsonb "charts_widget"
476+
t.jsonb "accordion_group_widget"
477477
t.index ["user_id"], name: "index_webpages_on_user_id", using: :btree
478478
end
479479

db/seeds.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
job_phase: initial_post_seed.job_phase,
1313
display: initial_post_seed.display,
1414
copyright_owner: initial_post_seed.copyright_owner,
15-
categories: [Category.first],
16-
primary_category: Category.first,
17-
author: User.first)
15+
author: Author.first)
1816

1917
existing_tenant = Tenant.find_by_name(tenant_seed.name)
2018

0 commit comments

Comments
 (0)