Skip to content

Commit

Permalink
First pass workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotcm authored and heathd committed Feb 13, 2014
1 parent d06166d commit ce78da7
Show file tree
Hide file tree
Showing 24 changed files with 292 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ gem 'gds-sso', '9.2.1'

gem 'mongoid', '2.5.2'

gem 'generic_form_builder', '0.4.5'

if ENV['CONTENT_MODELS_DEV']
gem 'govuk_content_models', :path => '../govuk_content_models'
else
gem "govuk_content_models"
gem "govuk_content_models", '6.4.0'
end

group :assets do
Expand All @@ -25,6 +27,7 @@ gem 'debugger', group: [:development, :test]

group :test do
gem 'cucumber-rails', '1.4.0', require: false
gem 'launchy'
gem 'rspec-rails', '2.14.1'
gem 'factory_girl', '4.3.0'
gem 'database_cleaner', '1.2.0'
Expand Down
32 changes: 17 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
PATH
remote: ../govuk_content_models
specs:
govuk_content_models (6.1.0)
bson_ext
differ
gds-api-adapters
gds-sso (>= 7.0.0, < 10.0.0)
govspeak (>= 1.0.1, < 2.0.0)
mongoid (~> 2.5)
plek
state_machine

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -42,6 +29,7 @@ GEM
activesupport (3.2.16)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
addressable (2.3.5)
arel (3.0.3)
aws-ses (0.5.0)
builder
Expand Down Expand Up @@ -87,7 +75,7 @@ GEM
activesupport (>= 3.0.0)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
gds-api-adapters (8.3.1)
gds-api-adapters (8.3.2)
link_header
lrucache (~> 0.1.1)
null_logger
Expand All @@ -98,12 +86,22 @@ GEM
rack-accept (~> 0.4.4)
rails (>= 3.0.0)
warden (~> 1.2)
generic_form_builder (0.4.5)
gherkin (2.12.2)
multi_json (~> 1.3)
govspeak (1.3.0)
htmlentities (~> 4)
kramdown (~> 0.13.3)
sanitize (~> 2.0.3)
govuk_content_models (6.4.0)
bson_ext
differ
gds-api-adapters
gds-sso (>= 7.0.0, < 10.0.0)
govspeak (>= 1.0.1, < 2.0.0)
mongoid (~> 2.5)
plek
state_machine
hashie (2.0.5)
hike (1.2.3)
htmlentities (4.3.1)
Expand All @@ -114,6 +112,8 @@ GEM
multi_json (>= 1.5)
kgio (2.9.1)
kramdown (0.13.8)
launchy (2.4.2)
addressable (~> 2.3)
link_header (0.0.8)
lrucache (0.1.4)
PriorityQueue (~> 0.1.2)
Expand Down Expand Up @@ -238,7 +238,9 @@ DEPENDENCIES
exception_notification (= 2.6.1)
factory_girl (= 4.3.0)
gds-sso (= 9.2.1)
govuk_content_models!
generic_form_builder (= 0.4.5)
govuk_content_models (= 6.4.0)
launchy
mongoid (= 2.5.2)
rails (= 3.2.16)
rspec-rails (= 2.14.1)
Expand Down
38 changes: 38 additions & 0 deletions app/controllers/specialist_documents_controller.rb
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
42 changes: 42 additions & 0 deletions app/models/specialist_document.rb
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
67 changes: 67 additions & 0 deletions app/models/specialist_document_registry.rb
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
18 changes: 18 additions & 0 deletions app/models/user.rb
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
7 changes: 7 additions & 0 deletions app/views/specialist_documents/_form.html.erb
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 %>
1 change: 1 addition & 0 deletions app/views/specialist_documents/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'form' %>
1 change: 1 addition & 0 deletions app/views/specialist_documents/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= link_to "New document", new_specialist_document_path %>
1 change: 1 addition & 0 deletions app/views/specialist_documents/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'form' %>
3 changes: 3 additions & 0 deletions config/initializers/content_api_credentials.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONTENT_API_CREDENTIALS = {
bearer_token: "overwritten on deploy"
}
1 change: 1 addition & 0 deletions config/initializers/form_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ActionView::Base.default_form_builder = GenericFormBuilder
1 change: 1 addition & 0 deletions config/initializers/govuk_content_models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
USER_COLLECTION_NAME = "specialist_publisher_users"
1 change: 1 addition & 0 deletions config/routes.rb
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
11 changes: 11 additions & 0 deletions features/creating-and-editing-a-cma-case.feature
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
22 changes: 22 additions & 0 deletions features/step_definitions/creating_a_cma_case_steps.rb
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
13 changes: 13 additions & 0 deletions features/support/cma_cases.rb
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
1 change: 1 addition & 0 deletions features/support/factories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'test/factories'
4 changes: 4 additions & 0 deletions features/support/login.rb
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
1 change: 1 addition & 0 deletions features/support/mocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'cucumber/rspec/doubles'
10 changes: 10 additions & 0 deletions features/support/panopticon.rb
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
5 changes: 5 additions & 0 deletions spec/models/specialist_document_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe SpecialistDocument do

end
Loading

0 comments on commit ce78da7

Please sign in to comment.