Browse files

Merge commit '2cc4f9dbe8be8b5526d42890894db0d7ccab369d' into HEAD

  • Loading branch information...
2 parents ad02fd0 + 2cc4f9d commit ef815558296cfcf6116bbbfd15e8bb52499039bd Jenkins committed May 14, 2012
View
5 Gemfile
@@ -1,4 +1,5 @@
source 'http://rubygems.org'
+source 'https://gems.gemfury.com/vo6ZrmjBQu5szyywDszE/'
group :passenger_compatibility do
gem 'rack', '1.3.5'
@@ -20,7 +21,7 @@ gem 'null_logger'
gem 'exception_notification'
-gem 'gds-api-adapters', '~> 0.0.15'
+gem 'gds-api-adapters'
gem 'aws-ses', :require => 'aws/ses'
gem 'rails', '~> 3.1.1'
@@ -32,6 +33,8 @@ gem "mongo", "1.5.2"
gem "bson_ext", "1.5.2"
gem "bson", "1.5.2"
+gem "govuk_content_models", "0.0.1"
+
if ENV['BUNDLE_DEV']
gem 'gds-sso', :path => '../gds-sso'
else
View
17 Gemfile.lock
@@ -23,6 +23,7 @@ GIT
GEM
remote: http://rubygems.org/
+ remote: https://gems.gemfury.com/vo6ZrmjBQu5szyywDszE/
specs:
PriorityQueue (0.1.2)
actionmailer (3.1.3)
@@ -109,7 +110,7 @@ GEM
multipart-post (~> 1.1)
rack (~> 1.1)
ffi (1.0.11)
- gds-api-adapters (0.0.40)
+ gds-api-adapters (0.0.47)
lrucache (~> 0.1.1)
null_logger
plek
@@ -127,13 +128,18 @@ GEM
json
gherkin (2.7.3)
json (>= 1.4.6)
+ govuk_content_models (0.0.1)
+ bson_ext
+ gds-api-adapters (~> 0.0.47)
+ mongoid (~> 2.4.10)
+ plek (~> 0.1.21)
hike (1.2.1)
i18n (0.6.0)
json (1.6.6)
launchy (2.0.5)
addressable (~> 2.2.6)
libv8 (3.3.10.4)
- lrucache (0.1.1)
+ lrucache (0.1.3)
PriorityQueue (~> 0.1.2)
mail (2.3.3)
i18n (>= 0.4.0)
@@ -155,7 +161,7 @@ GEM
metaclass (~> 0.0.1)
mongo (1.5.2)
bson (= 1.5.2)
- mongoid (2.4.1)
+ mongoid (2.4.10)
activemodel (~> 3.1)
mongo (~> 1.3)
tzinfo (~> 0.3.22)
@@ -178,7 +184,7 @@ GEM
oauth2 (0.5.2)
faraday (~> 0.7)
multi_json (~> 1.0)
- plek (0.1.20)
+ plek (0.1.21)
builder
polyglot (0.3.3)
rack (1.3.5)
@@ -276,10 +282,11 @@ DEPENDENCIES
fakeweb
formtastic!
formtastic-bootstrap!
- gds-api-adapters (~> 0.0.15)
+ gds-api-adapters
gds-sso (~> 0.5)
gds-warmup-controller
gelf
+ govuk_content_models (= 0.0.1)
launchy
marples (~> 1.0)
minitest
View
126 app/models/artefact.rb
@@ -1,126 +0,0 @@
-require 'marples/model_action_broadcast'
-
-class Artefact
- include Mongoid::Document
- include Mongoid::Timestamps
-
- include Marples::ModelActionBroadcast
- self.marples_client_name = 'panopticon'
- self.marples_logger = Rails.logger
-
- field "section", type: String
- field "name", type: String
- field "slug", type: String
- field "kind", type: String
- field "owning_app", type: String
- field "active", type: Boolean, default: false
- field "tags", type: String
- field "need_id", type: String
- field "department", type: String
- field "fact_checkers", type: String
- field "relatedness_done", type: Boolean, default: false
- field "publication_id", type: String
- field "business_proposition", type: Boolean, default: false
- field "tag_ids", type: Array
-
- MAXIMUM_RELATED_ITEMS = 8
-
- FORMATS = [
- "answer",
- "guide",
- "programme",
- "local_transaction",
- "transaction",
- "place",
- "smart-answer",
- "custom-application"
- ].freeze
-
- KIND_TRANSLATIONS = {
- 'standard transaction link' => 'transaction',
- 'local authority transaction link' => 'local_transaction',
- 'benefit / scheme' => 'programme',
- 'find my nearest' => 'place',
- }.tap { |h| h.default_proc = -> _, k { k } }.freeze
-
- has_and_belongs_to_many :related_artefacts, :class_name => "Artefact"
- belongs_to :contact
-
- before_validation :normalise, :on => :create
-
- validates :name, :presence => true
- validates :slug, :presence => true, :uniqueness => true, :slug => true
- validates :kind, :inclusion => { :in => FORMATS }
- validates_presence_of :owning_app
-
- before_save :save_section_as_tags
-
- # TODO: Remove this 'unless' hack after importing. It's only here because
- # some old entries in Panopticon lack a need_id.
- validates_presence_of :need_id, :unless => lambda { defined? IMPORTING_LEGACY_DATA }
-
- def self.in_alphabetical_order
- order_by([[:name, :asc]])
- end
-
- def self.find_by_slug(s)
- where(slug: s).first
- end
-
-
- def save_section_as_tags
- return if self.section.blank?
-
- # goes from 'Crime and Justice:The police'
- # to 'crime-and-justice', 'the-police'
- # tag_ids: 'crime-and-justice', 'crime-and-justice/the-police'
- section, sub_section = self.section.downcase.gsub(' ', '-').split(':')
-
- tag_ids = [section]
- tag_ids.push "#{section}/#{sub_section}" unless sub_section.blank?
-
- tag_ids.each do |tag_id|
- raise "missing tag #{tag_id}" unless TagRepository.load(tag_id)
- end
- self.tag_ids = tag_ids
- end
-
- def normalise
- return unless kind.present?
- self.kind = KIND_TRANSLATIONS[kind.to_s.downcase.strip]
- end
-
- def admin_url(options = {})
- [ "#{Plek.current.find(owning_app)}/admin/publications/#{id}",
- options.to_query
- ].reject(&:blank?).join("?")
- end
-
- # TODO: Replace this nonsense with a proper API layer.
- def as_json(options={})
- super(options.merge(
- include: {contact: {}}
- )).tap { |hash|
- if hash['tag_ids']
- hash['tag_ids'] = hash['tag_ids'].map { |tag_id| TagRepository.load(tag_id).as_json }
- else
- hash.delete 'tag_ids'
- end
-
- unless options[:ignore_related_artefacts]
- hash["related_items"] = related_artefacts.map { |a| {"artefact" => a.as_json(ignore_related_artefacts: true)} }
- end
- hash.delete("related_artefacts")
- hash.delete("related_artefact_ids")
- hash["id"] = hash.delete("_id")
- hash["contact"]["id"] = hash["contact"].delete("_id") if hash["contact"]
- }
- end
-
- def self.from_param(slug_or_id)
- # FIXME: A hack until the Publisher has panopticon ids for every article
- find_by_slug(slug_or_id) || find(slug_or_id)
- rescue BSON::InvalidObjectId
- raise Mongoid::Errors::DocumentNotFound.new(self, slug_or_id)
- end
-end
View
44 app/models/contact.rb
@@ -1,44 +0,0 @@
-require 'gds_api/contactotron'
-
-class Contact
- include Mongoid::Document
- include Mongoid::Timestamps
-
- field "name", type: String
- field "postal_address", type: String
- field "email_address", type: String
- field "website_url", type: String
- field "opening_hours", type: String
- field "contactotron_id", type: Integer
- field "phone_numbers", type: Array
-
- validates :name, :presence => true
-
- def update_from_contactotron
- [ :name, :postal_address, :email_address, :website_url,
- :opening_hours ].each do |k|
- send "#{k}=", data_from_contactotron.send(k)
- end
- self.phone_numbers = data_from_contactotron.phone_numbers.map { |pn|
- { kind: pn.kind, label: pn.label, value: pn.value }
- }
- save!
- end
-
- def self.in_alphabetical_order
- order_by([[:name, :asc]])
- end
-
- private
- def contactotron_uri
- URI.join(Plek.current.find('contactotron'), "/contacts/#{contactotron_id}").to_s
- end
-
- def api_adapter
- GdsApi::Contactotron.new(Plek.current.environment)
- end
-
- def data_from_contactotron
- @data_from_contactotron ||= api_adapter.contact_for_uri(contactotron_uri)
- end
-end
View
35 app/models/section.rb
@@ -1,35 +0,0 @@
-class Section
-
- def initialize(top_level, name)
- @top_level = top_level
- @name = name
- end
-
- def slug
- [@top_level, @name].join(":")
- end
-
- def to_s
- [@top_level, @name].join(" > ")
- end
-
- class << self
- def all
- @sections ||= load_sections
- end
-
- private
- def load_sections
- top_level = nil
- File.open(File.expand_path("../sections.txt", __FILE__)).inject([]) { |sections, line|
- if line =~ /^ /
- raise "Bad section.txt, must start with a section (no spaces at start of first line)" if top_level.nil?
- sections << new(top_level, line.strip)
- else
- top_level = line.strip
- sections
- end
- }
- end
- end
-end
View
89 app/models/sections.txt
@@ -1,89 +0,0 @@
-Crime and justice
- Reporting crime
- Terrorism and extremism
- The police
- Courts
- Sentencing
- Prisons and probation
- Complaints
- Young people and crime
- Your neighbourhood
- Victims of crime
-Education
- School admissions
- In school
- Special educational needs
- Student money
- Further and higher education
- Adult learning
- Libraries
-Work
- Finding a job
- Starting work
- Types of employment
- Employment rights
- Time off work
- Health and safety at work
- Work-related benefits and schemes
- Ending work
- Sickness
-Family
- Maternity and paternity
- Parenting
- Adoption and fostering
- Divorce and separation
- Death and bereavement
- Children
- Registering life events
- Marriage and civil partnership
- Mental capacity and the law
-Money and tax
- Children
- Home
- Disability and carers
- Tax credits
- Pensions
- Debt and bankruptcy
- Tax
- Winter payments
-Driving
- Learning to drive
- Your driving licence
- Vehicle tax
- Buying and selling a vehicle
- MOT
- Owning a car or motorbike
- Driving and your neighbourhood
- Driving and the law
- Driving courses
-Housing
- Emergency housing
- Council Tax
- Council and Housing Association homes
- Local council services
- Housing problems and issues
- Housing benefits, grants and schemes
- Planning and building
- Refuse and disposal
- Your home
- Buying and selling a home
- Renting a home
-Life in the UK
- The monarchy
- How government works
- Voting
- Rights and citizenship
-Travel
- Foreign travel
- Air travel
- Passports
- Animals, food and plants
- Moving and living abroad
- Domestic travel
-Neighbourhoods
- Community and local services
- Libraries and learning
- Charities and volunteering
- Council services
- Reporting a problem
- Parks and open spaces
View
18 app/models/tag.rb
@@ -1,18 +0,0 @@
-class Tag
- include Mongoid::Document
- field :tag_id, :type => String
- field :title, :type => String
- field :tag_type, :type => String #TODO: list of accepted types?
-
- index :tag_id, :unique => true
-
- validates_presence_of :tag_id, :title, :tag_type
-
- def as_json(options={})
- {
- :id => self.tag_id,
- :title => self.title,
- :type => self.tag_type
- }
- end
-end
View
34 app/models/user.rb
@@ -1,34 +0,0 @@
-class User
- include Mongoid::Document
- include Mongoid::Timestamps
-
- field "name", type: String
- field "uid", type: String
- field "version", type: Integer
- field "email", type: String
-
- # Setup accessible (or protected) attributes for your model
- attr_accessible :email, :name, :uid, :version
-
- # GDS::SSO specifically looks for find_by_uid within warden
- # when loading authentication user from session
- def self.find_by_uid(uid)
- where(uid: uid).first
- end
-
- # GDS::SSO override for first time authentication of the app
- def self.find_for_gds_oauth(auth_hash)
- find_by_uid(auth_hash["uid"]) || create_from_auth_hash(auth_hash)
- end
-
- def self.create_from_auth_hash(auth_hash)
- user_params = auth_hash['extra']['user_hash'].select { |k,v|
- %w[ uid email name version ].include?(k)
- }
- User.create!(user_params)
- end
-
- def to_s
- email
- end
-end
View
19 app/repositories/tag_repository.rb
@@ -1,19 +0,0 @@
-module TagRepository
-
- def self.load_all
- Tag.all
- end
-
- def self.load(id)
- Tag.where(:tag_id => id).first
- end
-
- def self.put(tag)
- t = Tag.where(:tag_id => tag[:tag_id]).first
- unless t
- Tag.create! tag
- else
- t.update_attributes! tag
- end
- end
-end
View
8 app/validators/slug_validator.rb
@@ -1,8 +0,0 @@
-class SlugValidator < ActiveModel::EachValidator
- # implement the method called during validation
- def validate_each(record, attribute, value)
- unless ActiveSupport::Inflector.parameterize(value.to_s) == value.to_s
- record.errors[attribute] << 'must be usable in a URL'
- end
- end
-end
View
1 config/application.rb
@@ -5,6 +5,7 @@
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
+require "govuk_content_models"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
View
8 config/initializers/messenger.rb
@@ -1,4 +1,12 @@
require 'messenger'
+require 'marples/model_action_broadcast'
+require 'artefact'
+
+class Artefact
+ include Marples::ModelActionBroadcast
+ self.marples_client_name = 'panopticon'
+ self.marples_logger = Rails.logger
+end
# Marples config needs to be triggered by on_prepare so it doesn't get merrily
# blatted whenever the Artefact model reloads in development mode
View
1 test/fixtures/contactotron_api_response.json
@@ -1 +0,0 @@
-{"created_at":"2011-12-02T12:02:43Z","email_address":"contact@example.com","id":189,"name":"Contact Name","opening_hours":null,"postal_address":"1 High Street\nTown","updated_at":"2011-12-02T12:02:43Z","website_url":"http://www.example.com/","phone_numbers":[{"contact_id":189,"created_at":"2011-12-02T12:02:43Z","id":447,"kind":"phone","label":null,"updated_at":"2011-12-02T12:02:43Z","value":"0113 496 0123"},{"contact_id":189,"created_at":"2011-12-02T12:02:43Z","id":448,"kind":"fax","label":null,"updated_at":"2011-12-02T12:02:43Z","value":"028 9018 1337"}]}
View
69 test/unit/artefact_test.rb
@@ -1,69 +0,0 @@
-require 'test_helper'
-
-class ArtefactTest < ActiveSupport::TestCase
- test "it allows nice clean slugs" do
- a = Artefact.new(:slug => "its-a-nice-day")
- a.valid?
- assert a.errors[:slug].empty?
- end
-
- test "it doesn't allow apostrophes in slugs" do
- a = Artefact.new(:slug => "it's-a-nice-day")
- assert ! a.valid?
- assert a.errors[:slug].any?
- end
-
- test "it doesn't allow spaces in slugs" do
- a = Artefact.new(:slug => "it is-a-nice-day")
- assert ! a.valid?
- assert a.errors[:slug].any?
- end
-
- test "should translate kind into internally normalised form" do
- a = Artefact.new(:kind => "benefit / scheme")
- a.normalise
- assert_equal "programme", a.kind
- end
-
- test "should not translate unknown kinds" do
- a = Artefact.new(:kind => "other")
- a.normalise
- assert_equal "other", a.kind
- end
-
- test "should store related artefacts in order" do
- a = Artefact.create!(slug: "a", name: "a", kind: "place", need_id: 1, owning_app: "x")
- b = Artefact.create!(slug: "b", name: "b", kind: "place", need_id: 2, owning_app: "x")
- c = Artefact.create!(slug: "c", name: "c", kind: "place", need_id: 3, owning_app: "x")
-
- a.related_artefacts = [b, c]
- a.save!
- a.reload
-
- assert_equal [b, c], a.related_artefacts
- end
-
- test "should raise a not found exception if the slug doesn't match" do
- assert_raise 'Mongoid::Errors::DocumentNotFound' do
- Artefact.from_param('something-fake')
- end
- end
-
- test "should save section as tags" do
- TagRepository.put(:tag_id => "crime", :tag_type => "section", :title => "Crime")
- TagRepository.put(:tag_id => "crime/the-police", :tag_type => "section", :title => "The Police")
-
- a = Artefact.create!(:slug => "a", :name => "a", :kind => "answer", :need_id => 1,
- :owning_app => "x", :section => "Crime:The Police")
- assert_equal a.tag_ids, ["crime", "crime/the-police"]
- end
-
- test "should save top level section as tag" do
- TagRepository.put(:tag_id => "crime", :tag_type => "section", :title => "Crime")
-
- a = Artefact.create!(:slug => "a", :name => "a", :kind => "answer", :need_id => 1,
- :owning_app => "x", :section => "Crime")
- assert_equal a.tag_ids, ["crime"]
- end
-
-end
View
23 test/unit/contact_test.rb
@@ -1,23 +0,0 @@
-require 'test_helper'
-
-class ContactTest < ActiveSupport::TestCase
- test "should store multiple phone numbers" do
- phone_numbers = ["0113 496 0123", "028 9018 1337"]
- contact = Contact.create!(
- name: "Name", phone_numbers: phone_numbers,
- contactotron_id: 1
- )
- assert_equal phone_numbers, contact.reload.phone_numbers
- end
-
- test "should import contact details from contactotron" do
- FakeWeb.register_uri(:get, "#{Plek.current.find("contactotron")}/contacts/189",
- body: File.read(Rails.root.join("test", "fixtures", "contactotron_api_response.json"))
- )
- contact = Contact.new(contactotron_id: 189)
- contact.update_from_contactotron
- contact.reload
- assert_equal "Contact Name", contact.name
- assert_equal "contact@example.com", contact.email_address
- end
-end
View
32 test/unit/user_test.rb
@@ -1,32 +0,0 @@
-require 'test_helper'
-
-class UserTest < ActiveSupport::TestCase
- test "should convert to string" do
- user = User.new(:email => "user@example.com")
- assert_equal "user@example.com", user.to_s
- end
-
- test "should find existing user by oauth hash" do
- user = User.create!("uid" => "1234abcd")
- assert_equal user, User.find_for_gds_oauth("uid" => "1234abcd")
- end
-
- test "should create new user with oauth params" do
- auth_hash = {
- "uid" => "1234abcd",
- "extra" => {
- "user_hash" => {
- "uid" => "1234abcd",
- "email" => "user@example.com",
- "name" => "Luther Blisset",
- "version" => 2
- }
- }
- }
- user = User.find_for_gds_oauth(auth_hash).reload
- assert_equal "1234abcd", user.uid
- assert_equal "user@example.com", user.email
- assert_equal "Luther Blisset", user.name
- assert_equal 2, user.version
- end
-end

0 comments on commit ef81555

Please sign in to comment.