Skip to content

Commit

Permalink
Merge branch 'develop' into bugfix/AR-66-Broken-Pagination-Headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Tharp authored Jul 11, 2017
2 parents 59996c3 + 7b2f3b1 commit 254713d
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 25 deletions.
5 changes: 0 additions & 5 deletions app/api/v1/entities/content_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ module Entities
class ContentItem < Grape::Entity
expose :id, documentation: {type: "Integer", desc: "Content Item ID", required: true}
expose :publish_state, documentation: {type: "String", desc: "Publish state", required: true}
expose :published_at, documentation: {type: "dateTime", desc: "Date published", required: true}
expose :expired_at, documentation: {type: "dateTime", desc: "Date to expire", required: true}
expose :author, documentation: {type: "dateTime", desc: "Date published", required: true} do |content_item|
content_item.author.fullname
end
expose :creator, documentation: {type: "dateTime", desc: "Date published", required: true} do |content_item|
content_item.creator.fullname
end
Expand Down
39 changes: 38 additions & 1 deletion app/api/v1/resources/content_items.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
module V1
module Resources
class ContentItems < Grape::API
helpers ::V1::Helpers::SharedParamsHelper
helpers ::V1::Helpers::ParamsHelper

resource :content_items do
include Grape::Kaminari
paginate per_page: 25

desc "Create a content item", { entity: ::V1::Entities::ContentItem, params: ::V1::Entities::ContentItem.documentation, nickname: "createContentItem" }
params do
requires :content_type_id, type: Integer, desc: "content type of content item"
end
post do
require_scope! 'create:content_items'
require_scope! 'modify:content_items'
authorize! :create, ::ContentItem
@content_item = ::ContentItem.new(params.merge(author_id: current_user.id, creator_id: current_user.id))

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

present @content_items, with: ::V1::Entities::ContentItem, field_items: true
end

desc 'Show published content items', { entity: ::V1::Entities::ContentItem, nickname: "contentItemsFeed" }
params do
use :pagination
requires :content_type_name, type: String, desc: 'ContentType of ContentItem'
end
get :feed do
require_scope! 'view:content_items'
authorize! :view, ::ContentItem

last_updated_at = ContentItem.last_updated_at
params_hash = Digest::MD5.hexdigest(declared(params).to_s)
cache_key = "feed-#{last_updated_at}-#{current_tenant.id}-#{params_hash}"

content_items = ::Rails.cache.fetch(cache_key, expires_in: 30.minutes, race_condition_ttl: 10) do
content_items = ::GetContentItems.call(params: declared(clean_params(params), include_missing: false), tenant: current_tenant, published: true).content_items
paginated_content_items = paginate(content_items).records.to_a
{records: paginated_content_items, headers: header}
end

header.merge!(content_items[:headers])
::V1::Entities::ContentItem.represent content_items[:records], field_items: true
end

desc 'Show a published content item', { entity: ::V1::Entities::ContentItem, nickname: "showFeedContentItem" }
get 'feed/*id' do
@content_item = ::GetContentItem.call(id: params[:id], published: true, tenant: current_tenant.id).content_item
not_found! unless @content_item
authorize! :view, @content_item
present @content_item, with: ::V1::Entities::ContentItem, field_items: true
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions app/assets/javascripts/legacy/controllers/webpages/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ angular.module('cortex.controllers.webpages.edit', [
page: page.page
});
}

$scope.appendEditingParamsToUrl = function(url) {
var urlHasParams = _.includes(url, '?');

if (urlHasParams) {
url = url + '&editing_mode=1&disable_redirects=1';
} else {
url = url + '?editing_mode=1&disable_redirects=1';
}

return url;
}
});
4 changes: 2 additions & 2 deletions app/assets/legacy_templates/webpages/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@
</tab>
</tabset>

<iframe class="webpage-frame" id="webpage-frame"
ng-src="{{ data.webpage.url + '?editing_mode=1' | trustAsResourceUrl }}" disabled></iframe>
<iframe ng-if="data.webpage.url" class="webpage-frame" id="webpage-frame"
ng-src="{{ appendEditingParamsToUrl(data.webpage.url) | trustAsResourceUrl }}" disabled></iframe>
</div>
12 changes: 12 additions & 0 deletions app/interactors/get_content_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class GetContentItem
include Interactor

def call
content_item = ::ContentItem
#content_item = content_item.find_by_tenant_id(context.tenant) if context.tenant
#content_item = content_item.published if context.published
#content_item = content_item.find_by_id_or_slug(context.id)
content_item = content_item.find_by_id(context.id)
context.content_item = content_item
end
end
8 changes: 8 additions & 0 deletions app/interactors/get_content_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class GetContentItems
include Interactor

def call
content_items = ContentType.find_by_name(context.params.content_type_name.titleize).content_items.order(created_at: :desc)
context.content_items = content_items
end
end
26 changes: 14 additions & 12 deletions app/models/content_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@ class ContentItem < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks

state_machine do
state :draft
state :scheduled

event :schedule do
transitions :to => :scheduled, :from => [:draft]
end

event :draft do
transitions :to => :draft, :from => [:scheduled]
end
end
scope :last_updated_at, -> { order(updated_at: :desc).select('updated_at').first.updated_at }

acts_as_paranoid

Expand All @@ -32,6 +21,19 @@ class ContentItem < ApplicationRecord
after_save :index
after_save :update_tag_lists

state_machine do
state :draft
state :scheduled

event :schedule do
transitions :to => :scheduled, :from => [:draft]
end

event :draft do
transitions :to => :draft, :from => [:scheduled]
end
end

def self.taggable_fields
Field.select { |field| field.field_type_instance.is_a?(TagFieldType) }.map { |field_item| field_item.name.parameterize('_') }
end
Expand Down
3 changes: 2 additions & 1 deletion config/initializers/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
optional_scopes 'view:users', 'modify:users', 'view:tenants', 'modify:tenants', 'view:posts',
'modify:posts', 'view:media', 'modify:media', 'view:applications', 'modify:applications',
'view:bulk_jobs', 'modify:bulk_jobs', 'view:documents', 'modify:documents',
'view:snippets', 'modify:snippets', 'view:webpages', 'modify:webpages', 'view:content_types'
'view:snippets', 'modify:snippets', 'view:webpages', 'modify:webpages', 'view:content_types',
'modify:content_types', 'view:content_items', 'modify:content_items'

# Change the way client credentials are retrieved from the request object.
# By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@
t.string "dynamic_yield_sku"
t.string "dynamic_yield_category"
t.jsonb "tables_widget"
t.jsonb "accordion_group_widget"
t.jsonb "charts_widget"
t.jsonb "accordion_group_widget"
t.index ["user_id"], name: "index_webpages_on_user_id", using: :btree
end

Expand Down
4 changes: 1 addition & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
job_phase: initial_post_seed.job_phase,
display: initial_post_seed.display,
copyright_owner: initial_post_seed.copyright_owner,
categories: [Category.first],
primary_category: Category.first,
author: User.first)
author: Author.first)

existing_tenant = Tenant.find_by_name(tenant_seed.name)

Expand Down
Loading

0 comments on commit 254713d

Please sign in to comment.