Skip to content

Commit 6e9b468

Browse files
author
Chris Parsons
committed
Attach images to topics
1 parent 3088b11 commit 6e9b468

File tree

14 files changed

+128
-12
lines changed

14 files changed

+128
-12
lines changed

Diff for: bin/fog

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env ruby
2+
#
3+
# This file was generated by Bundler.
4+
#
5+
# The application 'fog' is installed as part of a gem, and
6+
# this file is here to facilitate running it.
7+
#
8+
9+
require 'pathname'
10+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11+
Pathname.new(__FILE__).realpath)
12+
13+
require 'rubygems'
14+
require 'bundler/setup'
15+
16+
load Gem.bin_path('fog', 'fog')

Diff for: features/administrator-manual/maintaining-topics.feature

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Feature: Maintaining Topics
5151
Scenario: Adding an image to a topic
5252
Given I have uploaded an image called 'job-centres.png'
5353
When I create a topic "Job Centres" referencing the 'job-centres.png' background image
54-
Then the topic page should show the 'job-centres.png' image as its background
54+
Then an audience page for that topic should show the 'job-centres.png' image as the topic's background
5555

5656
Scenario: Removing a topic
5757
When I create a topic "Youth"

Diff for: features/step_definitions/image_steps.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Given(/^I have uploaded an image called 'job\-centres\.png'$/) do
2+
basic_auth('discover', '')
3+
visit '/admin/images/uploaded?bucket=discover-thamesmead-website-images&key=uploads%2Ffoo%2Fjob-centres.png'
4+
end

Diff for: features/step_definitions/topic_steps.rb

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@
4242
create_topic(topic_name)
4343
end
4444

45+
When(/^I create a topic "(.*?)" referencing the 'job\-centres\.png' background image$/) do |topic_name|
46+
create_topic(topic_name) do
47+
select 'job-centres.png'
48+
end
49+
end
50+
51+
Then(/^an audience page for that topic should show the 'job\-centres\.png' image as the topic's background$/) do
52+
Discover::AudienceRepository.new.apply([Discover::Changes::AudienceCreated.new(Discover::Audience.new("foo", nil, ["job-centres"]))])
53+
visit "/foo"
54+
page.should have_css(".dt-topic-bg[style*='job-centres.png']")
55+
end
56+
4557
When(/^I associate it with the "(.*?)" audience$/) do |audience_name|
4658
@audience_name = audience_name
4759
visit '/admin'

Diff for: features/support/env.rb

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def create_topic(name, description = "")
4747
click_link 'Create topic'
4848
fill_in 'Topic name', :with => name
4949
fill_in 'Description', :with => description
50+
yield if block_given?
5051
click_button 'Create topic'
5152
should_be_success
5253
end

Diff for: lib/discover.rb

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'discover/topic_validator'
55
require 'discover/place'
66
require 'discover/place_validator'
7+
require 'discover/image'
78
require 'discover/changes'
89
require 'discover/reactor'
910

Diff for: lib/discover/app/admin/topics.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,16 @@ def create_from_params(params)
7373
Topic.new(params[:object][:name],
7474
params[:object][:description],
7575
nil,
76-
[params[:object][:places]].flatten.compact)
76+
[params[:object][:places]].flatten.compact).
77+
with_image(params[:object][:image])
7778
end
7879

7980
def update_from_params(object, params)
8081
object.
8182
with_name(params[:object][:name]).
8283
with_description(params[:object][:description]).
83-
with_places([params[:object][:places]].flatten.compact)
84+
with_places([params[:object][:places]].flatten.compact).
85+
with_image(params[:object][:image])
8486
end
8587

8688
def delete_change(slug)

Diff for: lib/discover/image.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Discover
2+
class Image < Struct.new(:path, :bucket)
3+
DEFAULT_BUCKET = 'discover-thamesmead-website-images'
4+
DEFAULT_PATH = 'placeholder.jpg'
5+
6+
def initialize(*)
7+
super
8+
self.path = DEFAULT_PATH if self.path.nil? || self.path == ''
9+
self.bucket ||= DEFAULT_BUCKET
10+
end
11+
12+
def filename
13+
File.basename(path)
14+
end
15+
16+
def url
17+
"https://s3.amazonaws.com/#{bucket}/#{path}"
18+
end
19+
end
20+
end

Diff for: lib/discover/persisted/audience.rb

+29-3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ class Topic
3232
field :name
3333
field :description
3434
field :slug
35+
field :image
3536
has_and_belongs_to_many :audiences
3637
has_and_belongs_to_many :places
3738

3839
def domain_object
39-
Discover::Topic.new(name, description, slug, places.map(&:slug)).freeze
40+
Discover::Topic.new(name, description, slug, places.map(&:slug), image).freeze
4041
end
4142

4243
def sync_places!(places)
@@ -59,6 +60,18 @@ def domain_object
5960
YAML.load(yaml).freeze
6061
end
6162
end
63+
64+
class Image
65+
include Mongoid::Document
66+
include Mongoid::Timestamps
67+
68+
field :path
69+
field :bucket
70+
71+
def domain_object
72+
Discover::Image.new(path, bucket).freeze
73+
end
74+
end
6275
end
6376

6477
class AudienceRepository
@@ -86,7 +99,8 @@ def topic_created(change)
8699
topic = Persisted::Topic.create!(
87100
name: change.topic.name,
88101
description: change.topic.description,
89-
slug: change.topic.slug
102+
slug: change.topic.slug,
103+
image: change.topic.image
90104
)
91105
topic.sync_places!(change.topic.places)
92106
end
@@ -95,7 +109,8 @@ def topic_edited(change)
95109
topic = Persisted::Topic.find_by(slug: change.slug)
96110
topic.update_attributes(
97111
name: change.topic.name,
98-
description: change.topic.description
112+
description: change.topic.description,
113+
image: change.topic.image
99114
)
100115
topic.sync_places!(change.topic.places)
101116
end
@@ -119,6 +134,13 @@ def place_deleted(change)
119134
Persisted::Place.find_by(slug: change.slug).destroy
120135
end
121136

137+
def image_uploaded(change)
138+
Persisted::Image.create!(
139+
path: change.path,
140+
bucket: change.bucket
141+
)
142+
end
143+
122144
def topics
123145
Persisted::Topic.all.map(&:domain_object)
124146
end
@@ -131,6 +153,10 @@ def active_audiences
131153
Persisted::Audience.all.map(&:domain_object)
132154
end
133155

156+
def available_images
157+
Persisted::Image.all.map(&:domain_object)
158+
end
159+
134160
def audience_from_slug(slug)
135161
Persisted::Audience.find_by(slug: slug).domain_object
136162
rescue Mongoid::Errors::DocumentNotFound

Diff for: lib/discover/topic.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
module Discover
2-
class Topic < Struct.new(:name, :description, :slug, :places)
2+
class Topic < Struct.new(:name, :description, :slug, :places, :image)
33
def initialize(*)
44
super
55
self.name ||= ''
66
self.description ||= ''
77
self.slug ||= sluggify(name)
88
self.places ||= []
9+
self.image ||= Image.new.url
910
end
1011

1112
def sluggify(string)
1213
string.downcase.gsub(/\W/,'-')
1314
end
1415

1516
def with_name(new_name)
16-
self.class.new(new_name, description, slug, places)
17+
self.class.new(new_name, description, slug, places, image)
1718
end
1819

1920
def with_description(new_description)
20-
self.class.new(name, new_description, slug, places)
21+
self.class.new(name, new_description, slug, places, image)
2122
end
2223

2324
def with_places(new_places)
24-
self.class.new(name, description, slug, new_places)
25+
self.class.new(name, description, slug, new_places, image)
26+
end
27+
28+
def with_image(new_image)
29+
self.class.new(name, description, slug, places, Image.new(new_image, Image::DEFAULT_BUCKET).url)
2530
end
2631

2732
def places_by_name(repository)

Diff for: spec/lib/discover/image_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
require 'discover/image'
4+
5+
module Discover
6+
describe Image do
7+
subject { Image.new('path/to/image.png', 'bucket') }
8+
9+
it 'displays basic filename' do
10+
expect(subject.filename).to eq('image.png')
11+
end
12+
13+
it 'displays full url' do
14+
expect(subject.url).to eq('https://s3.amazonaws.com/bucket/path/to/image.png')
15+
end
16+
end
17+
end

Diff for: spec/lib/discover/persisted/audience_spec.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'discover/audience'
55
require 'discover/topic'
66
require 'discover/place'
7+
require 'discover/image'
78
require 'discover/changes'
89
require 'discover/reactor'
910

@@ -84,11 +85,12 @@ def create_place!
8485
it "edits topics" do
8586
create_place!
8687
create_topic!
87-
new_topic = Topic.new("new_name", nil, nil, [place.slug])
88+
new_topic = Topic.new("new_name", nil, nil, [place.slug], 'image-url')
8889
subject.apply([Changes::TopicEdited.new(topic.slug, new_topic)])
8990
saved_topic = subject.topic_from_slug(topic.slug)
9091
expect(saved_topic.name).to eq "new_name"
9192
expect(saved_topic.places).to eq [place.slug]
93+
expect(saved_topic.image).to eq "image-url"
9294
end
9395

9496
it 'deletes topics' do
@@ -97,5 +99,11 @@ def create_place!
9799
subject.apply([Changes::TopicDeleted.new(topic.slug)])
98100
expect { subject.topic_from_slug(topic.slug) }.to raise_error(NoTopicFoundError)
99101
end
102+
103+
it 'adds uploaded images' do
104+
subject.apply([Changes::ImageUploaded.new("path", "bucket")])
105+
subject.available_images.size.should == 1
106+
subject.available_images.first.should == Image.new("path", "bucket")
107+
end
100108
end
101109
end

Diff for: views/admin/topics/form.haml

+4
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
%select.chzn-select.span12#places{:multiple => 'multiple', :name => 'object[places][]'}
99
- @places.each do |place|
1010
%option{:value => place.slug, :selected => @object.places.include?(place.slug)}= place.name
11+
%label{:for => "image"} Background Image
12+
%select.chzn-select.span12#image{:name => 'object[image]'}
13+
- repository.available_images.each do |image|
14+
%option{:value => image.path, :selected => @object.image == image}= image.filename
1115
%br
1216
%br

Diff for: views/audience.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.dt-audience-bg.row-fluid
44
- @audience.topics.each do |slug|
55
- topic = repository.topic_from_slug(slug)
6-
.dt-topic-bg{:style => 'background: url(/img/discover/index-bg.jpg)'}
6+
.dt-topic-bg{:style => "background: url(#{topic.image})"}
77
%a.dt-topic{:href => topic_path(@audience, topic)}
88
.name
99
= topic.name

0 commit comments

Comments
 (0)