-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
292 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class SpecialistDocumentsController < ApplicationController | ||
|
||
def index; end | ||
|
||
def new; end | ||
|
||
def edit; end | ||
|
||
def create | ||
if document.valid? | ||
SpecialistDocumentRegistry.store(document) | ||
redirect_to specialist_documents_path | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def update | ||
if document.valid? | ||
SpecialistDocumentRegistry.store(document) | ||
redirect_to specialist_documents_path | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
protected | ||
|
||
def document | ||
@document ||= if params[:id] | ||
SpecialistDocumentRegistry.fetch(params[:id]) | ||
else | ||
SpecialistDocument.new(params[:specialist_document]) | ||
end | ||
end | ||
helper_method :document | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class SpecialistDocument | ||
include ActiveModel::Conversion | ||
extend ActiveModel::Naming | ||
|
||
ATTRIBUTES = [:id, :title, :summary, :body, :state] | ||
|
||
def initialize(attributes) | ||
attributes ||= {} | ||
|
||
ATTRIBUTES.each do |attribute| | ||
send("#{attribute}=", attributes[attribute]) | ||
end | ||
end | ||
|
||
attr_accessor *ATTRIBUTES | ||
|
||
def slug | ||
"cma-cases/#{slug_from_title}" | ||
end | ||
|
||
def published? | ||
state == 'published' | ||
end | ||
|
||
def valid? | ||
true | ||
end | ||
|
||
def errors | ||
Hash.new({}) | ||
end | ||
|
||
def persisted? | ||
false | ||
end | ||
|
||
protected | ||
|
||
def slug_from_title | ||
title.downcase.gsub(/\W/, '-') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require "gds_api/panopticon" | ||
|
||
class SpecialistDocumentRegistry | ||
|
||
def self.fetch(id, version: nil) | ||
return nil unless Artefact.find(id) | ||
|
||
editions = SpecialistDocumentEdition.where(panopticon_id: panopticon_id).order(:created_at) | ||
|
||
edition = if version | ||
editions.where(version_number: version).last | ||
else | ||
editions.last | ||
end | ||
|
||
SpecialistDocument.new(id: id, title: edition.title, summary: edition.summary, state: edition.state) | ||
end | ||
|
||
def self.store(document) | ||
new(document).store! | ||
end | ||
|
||
def initialize(document) | ||
@document = document | ||
end | ||
|
||
def store! | ||
unless document.id | ||
response = create_artefact | ||
document.id = response['id'] | ||
end | ||
|
||
update_edition | ||
end | ||
|
||
protected | ||
|
||
attr_reader :document | ||
|
||
def update_edition | ||
draft = find_or_create_draft | ||
draft.title = document.title | ||
draft.summary = document.summary | ||
draft.body = document.body | ||
|
||
draft.save! | ||
end | ||
|
||
def create_artefact | ||
panopticon_api.create_artefact!(name: document.title, slug: document.slug, kind: 'specialist-document', owning_app: 'specialist-publisher') | ||
end | ||
|
||
def panopticon_api | ||
@panopticon_api ||= GdsApi::Panopticon.new(Plek.current.find("panopticon"), CONTENT_API_CREDENTIALS) | ||
end | ||
|
||
def find_or_create_draft | ||
latest_edition = SpecialistDocumentEdition.where(panopticon_id: document.id).order(:created_at).last | ||
|
||
if latest_edition.nil? | ||
SpecialistDocumentEdition.new(panopticon_id: document.id, state: 'draft') | ||
elsif document.published? | ||
SpecialistDocumentEdition.new(panopticon_id: document.id, state: 'draft', version_number: (latest_edition.version_number + 1)) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "gds-sso/user" | ||
|
||
class User | ||
include Mongoid::Document | ||
include Mongoid::Timestamps | ||
include GDS::SSO::User | ||
|
||
field "name", type: String | ||
field "uid", type: String | ||
field "email", type: String | ||
field "permissions", type: Array | ||
field "remotely_signed_out", type: Boolean, default: false | ||
field "organisation_slug", type: String | ||
|
||
def self.find_by_uid(uid) | ||
where(uid: uid).first | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<%= form_for document do |f| %> | ||
<%= f.text_field :title %> | ||
<%= f.text_area :summary %> | ||
<%= f.text_area :body %> | ||
|
||
<button>Save as draft</button> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render 'form' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= link_to "New document", new_specialist_document_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render 'form' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CONTENT_API_CREDENTIALS = { | ||
bearer_token: "overwritten on deploy" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ActionView::Base.default_form_builder = GenericFormBuilder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
USER_COLLECTION_NAME = "specialist_publisher_users" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
SpecialistPublisher::Application.routes.draw do | ||
resources :specialist_documents, except: [:show], path: 'specialist-documents' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Feature: Creating and editing a CMA case | ||
As a CMA editor | ||
I want to create and edit a case and see it in the publisher | ||
So that I can start moving my content to gov.uk | ||
|
||
Background: | ||
Given I am logged in as a CMA editor | ||
|
||
Scenario: Create a new CMA case | ||
When I create a CMA case | ||
Then the CMA case should exist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Given(/^I am logged in as a CMA editor$/) do | ||
login_as(:cma_editor) | ||
end | ||
|
||
When(/^I create a CMA case$/) do | ||
stub_out_panopticon | ||
|
||
visit new_specialist_document_path | ||
|
||
@cma_fields = { | ||
title: 'Example CMA Case', | ||
summary: 'Nullam quis risus eget urna mollis ornare vel eu leo.', | ||
body: ('Praesent commodo cursus magna, vel scelerisque nisl consectetur et.' * 10) | ||
} | ||
fill_in_cma_fields(@cma_fields) | ||
|
||
save_document | ||
end | ||
|
||
Then(/^the CMA case should exist$/) do | ||
check_cma_case_exists_with(@cma_fields) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
def fill_in_cma_fields(fields) | ||
fields.slice(:title, :summary, :body).each do |field, text| | ||
fill_in field.to_s.titlecase, with: text | ||
end | ||
end | ||
|
||
def save_document | ||
click_on "Save as draft" | ||
end | ||
|
||
def check_cma_case_exists_with(attributes) | ||
assert SpecialistDocumentEdition.exists?(conditions: attributes) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'test/factories' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def login_as(user_type) | ||
user = FactoryGirl.create(user_type.to_sym) | ||
GDS::SSO.test_user = user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'cucumber/rspec/doubles' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class MockPanopticon | ||
def create_artefact!(attributes = {}) | ||
artefact = Artefact.create!(attributes) | ||
{'id' => artefact.id} | ||
end | ||
end | ||
|
||
def stub_out_panopticon | ||
GdsApi::Panopticon.stub('new' => MockPanopticon.new) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
describe SpecialistDocument do | ||
|
||
end |
Oops, something went wrong.