Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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
Loading