Skip to content

Commit

Permalink
ability to set wiki main image to none (#6283)
Browse files Browse the repository at this point in the history
* ability to set wiki main image to none

* Update wiki_controller.rb

* small fix

* Try longer timeout

* Try even longer

* Update wiki_controller_test.rb

* Update _main_image.html.erb
  • Loading branch information
jywarren authored Sep 16, 2019
1 parent 5eff61b commit 928f3e2
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ install:
- cp db/schema.rb.example db/schema.rb
- docker-compose build
- docker-compose up -d
- docker-compose exec web sleep 15 # Need to wait for MySQL
- docker-compose exec web sleep 15 # wait until mysql is up
- netstat -ltpn
- docker-compose exec web bash -c "rake db:setup"
- docker-compose exec web bash -c "rake db:migrate"
Expand Down
12 changes: 5 additions & 7 deletions app/assets/javascripts/main_image.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
$(document).ready(function(){
$('#image_revision').change(function(){
console.log("inside sample");
console.log($(this).val());
$('#main_image').val($('#image_revision option:selected').attr("id"));
$('#leadImage').attr("src",$('#image_revision option:selected').val());
});
});
$('#image_revision').change(function(){
$('#main_image').val($('#image_revision option:selected').attr("id"));
$('#leadImage').attr("src",$('#image_revision option:selected').val());
});
});
2 changes: 1 addition & 1 deletion app/assets/javascripts/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jQuery(document).ready(function() {
$('#side-fileinput').bind('focus',function(e) { $('#side-dropzone').css('border-color','#4ac') })
$('#side-fileinput').bind('focusout',function(e) { $('#side-dropzone').css('border-color','#ccc') })
publish = function(e) {
if ($('#main_image').val() == "" && $('#has_main_image').val() != "true" && !$D.warn_image) {
if ($('#main_image').val() == "" && !$D.warn_image) {
// prompt to choose a lead image
$D.warn_image = true;
$('.side-dropzone').css('border-color','#d99')
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/wiki_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,9 @@ def update_node_attributes
main_image.save
end

if params[:main_image].present? && img = Image.find(params[:main_image])
if params[:main_image].to_i == 0
@node.main_image_id = nil
elsif params[:main_image].present? && img = Image.find(params[:main_image])
img.nid = @node.id
@node.main_image_id = img.id
img.save
Expand Down
46 changes: 5 additions & 41 deletions app/views/editor/_main_image.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<h3 class="d-md-none d-lg-block">Main image <i id="main-image-spinner" class="hidden icon-spinner icon-spin"></i></h3>

<% if @node && @node.main_image_id != 0 %>
<img class="rounded" id="leadImage" style="width:100%;<% unless (@node && @node.main_image) || params[:main_image] || params[:i] %>display:none;<% end %>"
src="<% if @node && @node.main_image %>
<%= @node.main_image.path(:default) %>
Expand All @@ -10,6 +11,7 @@
<% elsif @image %>
<%= @image.path(:default) %>
<% end %>" />
<% end %>

<div class="side-dropzone" id="side-dropzone">
<p class="prompt">
Expand All @@ -35,52 +37,14 @@
</div>
<p class="side-uploading" style="display:none;padding-top:4px;color:#aaa;">Uploading...</p>
<% if @node && @node.images && @node.images.length > 0 %>
<p>Select previous image</p>
<p>Select a previous image:</p>
<p>
<select name="image_revision" id="image_revision">
<% @node.images.each_with_index do |i,index| %>
<option id="<%=i.id%>" value="<%=i.path(:default)%>" <% if i.id == @node.main_image_id %>selected='selected'<% end %> >Image <%=index+1%></option>
<option id="<%=i.id%>" value="<%=i.path(:default)%>" <% if i.id == @node.main_image_id %>selected='selected'<% end %> ><%= index + 1 %>: <%= i.filename %></option>
<% end %>
<option id="0" value="none" <% if @node.main_image_id == 0 %>selected='selected'<% end %> >None</option>
</select>
</p>
<% end %>
<% if params[:newsletter] %>

<script>
(function(){
upload_main_img = function (i,author) {
$('#main-image-spinner').removeClass('hidden')
$.ajax({
url: "/images/create?i="+i,
dataType: "json",
type: 'POST',
success: function(response){
$('#main-image-spinner').addClass('hidden')
$('#text-input').val($('#text-input').val().replace('<!-- Caption for lead image -->','Above image by ['+author+'](/profile/'+author+')<!-- Caption for lead image -->'))
$('#leadImage').show()
$('#leadImage')[0].src = response.url
$('#main_image').val(response.id)
$('#has_main_image').val(true)
}
})
}
})()
</script>

<h4 style="margin-top:20px;">Choose a main image:</h4>

<div class="row">
<% @all.each do |note| %>
<% if note.main_image %>
<a class="col-lg-6" onClick="upload_main_img('<%= note.main_image.path(:original) %>','<%= note.author.username %>')">
<img class="rounded" src="<%= note.main_image.path(:default) %>" />
</a>
<% end %>
<% end %>
</div>

<p><i>The last newsletter was published <%= time_ago_in_words(@newsletter.created_at) %> ago.</i></p>

<% end %>

</div>
1 change: 0 additions & 1 deletion app/views/editor/post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
<input id="title" tabindex="1" name="title" class="form-control" placeholder="<% if params[:template] == "question" %>Ask a question as specifically as possible<% else %>Title your post descriptively<% end %>" value="<%= if @node then @node.title else params[:title] end %>"/>
</div>

<input class="form-control" id="has_main_image" type="hidden" name="has_main_image" value="<% if @node && @node.main_image %>true<% end %>" />
<input class="form-control" id="main_image" type="hidden" name="main_image" value="<% if @node && @node.main_image(:rails) %><%= @node.main_image(:rails).id %><% else %><%= params[:main_image] %><% end %>" />
<input class="form-control" id="node_images" type="hidden" name="node_images" value="" />

Expand Down
1 change: 0 additions & 1 deletion app/views/editor/question.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
<input id="title" tabindex="1" name="title" class="form-control" placeholder="<% if params[:template] == "question" %>Ask a question as specifically as possible<% else %>Title your post descriptively<% end %>" value="<%= if @node then @node.title else params[:title] end %>"/>
</div>

<input class="form-control" id="has_main_image" type="hidden" name="has_main_image" value="<% if @node && @node.main_image %>true<% end %>" />
<input class="form-control" id="main_image" type="hidden" name="main_image" value="<% if @node && @node.main_image(:rails) %><%= @node.main_image(:rails).id %><% else %><%= params[:main_image] %><% end %>" />
<input class="form-control" id="node_images" type="hidden" name="node_images" value="" />

Expand Down
3 changes: 1 addition & 2 deletions app/views/map/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<h3>Publish a map to the Archive</h3>

<input id="title" tabindex="1" name="title" type="text" class="form-control" placeholder="Title" value="<%= @node.title %>"><br />
<input id="has_main_image" type="hidden" name="has_main_image" value="<% if @node.main_image %>true<% end %>" />
<input id="main_image" type="hidden" name="main_image" value="<% if @node.main_image(:rails) %><%= @node.main_image(:rails).id %><% else %><%= params[:main_image] %><% end %>" />
<input id="node_images" type="hidden" name="node_images" value="" />

Expand Down Expand Up @@ -153,7 +152,7 @@
if ($('#latitude').val() == "" || $('#longitude').val() == "") {
alert_notice('You must specify a longitude and latitude.', {'scroll': true})
} else {
if ($('#main_image').val() == "" && $('#has_main_image').val() != "true" && !warn_image) {
if ($('#main_image').val() == "" && !warn_image) {
// prompt to choose a lead image
warn_image = true;
$('.side-dropzone').css('border-color','#d99')
Expand Down
1 change: 0 additions & 1 deletion app/views/wiki/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
<% else %>
<h3><%= t('wiki.edit.create_page') %>: <i><%=h params[:id] %></i></h3>
<% end %>
<input class="form-control" id="has_main_image" type="hidden" name="has_main_image" value="<% if @node && @node.main_image %>true<% end %>" />
<input class="form-control" id="main_image" type="hidden" name="main_image" value="<% if @node && @node.main_image(:rails) %><%= @node.main_image(:rails).id %><% else %><%= params[:main_image] %><% end %>" />
<input class="form-control" id="images_input" type="hidden" name="images" value="" />

Expand Down
17 changes: 16 additions & 1 deletion test/functional/wiki_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,26 @@ def teardown
node = nodes(:about)
image = node.images.where(photo_file_name: 'filename-1.jpg').last

post :update, params: { id: node.nid, uid: users(:bob).id, title: 'New Title', body: 'Editing about Page', image_revision: image.path(:default) }
post :update, params: { id: node.nid, uid: users(:bob).id, title: 'New Title', body: 'Editing about Page', main_image: image.id }

node.reload
assert_redirected_to node.path
assert_equal flash[:notice], 'Edits saved.'
assert_equal node.main_image_id, image.id
end

test 'update wiki selecting no image' do
node = nodes(:about)
node.main_image_id = 1
node.save
assert_equal 1, node.main_image_id

post :update, params: { id: node.nid, uid: users(:bob).id, title: 'New Title', body: 'Editing about Page', main_image: 0 }

node.reload
assert_redirected_to node.path
assert_equal flash[:notice], 'Edits saved.'
assert_equal nil, node.main_image_id
end

test 'normal user should not delete wiki page' do
Expand Down

0 comments on commit 928f3e2

Please sign in to comment.