Skip to content

Commit

Permalink
Fixed bug that did not render correct vote links on ajax buttons.
Browse files Browse the repository at this point in the history
Added tests to assert that image vote ajax call renders correct HTML to support both ajax and non-ajax versions.
  • Loading branch information
raysuelzer committed Feb 8, 2015
1 parent 3c1400f commit a55b484
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/controllers/ajax_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def cast_naming_vote(id, value_str)
end
end

def cast_image_vote(id, value) ##TODO: Rewrite tests
def cast_image_vote(id, value)
@image = Image.safe_find(id)
if value != '0' and not Image.validate_vote(value)
raise "Invalid value for vote/image: #{value.inspect}"
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ def image_exporter(image_id, exported)
def image_vote_link(image, vote)
current_vote = image.users_vote(@user)
vote_text = vote == 0 ? "(x)" : image_vote_as_short_string(vote)
link = link_to(vote_text, {}, title: image_vote_as_help_string(vote), data:{role: "image_vote", id: image.id, val: vote }) ##return a link if the user has NOT voted this way
link = link_to(vote_text, {controller: :image, action: :show_image, id: image.id, vote: vote}, title: image_vote_as_help_string(vote), data:{role: "image_vote", id: image.id, val: vote }) ##return a link if the user has NOT voted this way
if (current_vote == vote)
link = content_tag('span', image_vote_as_short_string(vote)) ##return a span if the user has voted this way
end
Expand Down
22 changes: 14 additions & 8 deletions app/views/observer/ask_webmaster_question.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<% @title = :ask_webmaster_title.t %>

<%= :ask_webmaster_note.tp(:todo_list => "/pivotal") %>
<div class="row">
<div class="col-xs-12">
<%= form_tag(:action => 'ask_webmaster_question') do %>

<%= form_tag(:action => 'ask_webmaster_question') do %>
<br/><center><%= submit_tag :SEND.l %></center>
<label for="user_email"><%= :ask_webmaster_your_email.t %>:</label><br/>
<%= text_field(:user, :email, value: @email, size: 60, autofocus: @email.blank? || @email_error) %><br/><br/>

<label for="user_email"><%= :ask_webmaster_your_email.t %>:</label><br/>
<%= text_field(:user, :email, value: @email, size: 60, autofocus: @email.blank? || @email_error) %><br/><br/>

<label for="question_content"><%= :ask_webmaster_question.t %>:</label><br/>
<%= text_area(:question, :content, value: @content, cols: 80, autofocus: !@email.blank? && !@email_error) %>
<% end %>
<label for="question_content"><%= :ask_webmaster_question.t %>:</label><br/>
<%= text_area(:question, :content, value: @content, cols: 80, autofocus: !@email.blank? && !@email_error) %>
</div>
<div class="push-down text-center">
<%= submit_tag :SEND.l %>
</div>
<% end %>
</div>
</div>
45 changes: 45 additions & 0 deletions test/controllers/ajax_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,51 @@ def test_image_vote
bad_ajax_request(:vote, type: :image, id: 99, value: 0)
end

def test_image_vote_renders_partial
##Arrange
login("dick")

#Act
good_ajax_request(:vote, type: :image, id: 1, value: 3)

#Assert
assert_template layout: nil
assert_template layout: false
assert_template partial: 'image/_image_vote_links'
end

def test_image_vote_renders_correct_links
##Arrange
login("dick")

#Act
good_ajax_request(:vote, type: :image, id: 1, value: 3)

assert_tag "a", attributes: {
href: "/image/show_image/1?vote=0"
}
assert_tag "a", attributes: {
href: "/image/show_image/1?vote=1"
}
assert_tag "a", attributes: {
href: "/image/show_image/1?vote=2"
}
assert_tag "a", attributes: {
href: "/image/show_image/1?vote=4"
}
end

def test_image_vote_renders_correct_data_attributes
##Arrange
login("dick")

#Act
good_ajax_request(:vote, type: :image, id: 1, value: 3)

assert_select("[data-role='image_vote']", 4) ##should show four vote links as dick already voted
assert_select("[data-val]", 4) ##should show four vote links as dick already voted
end

def test_old_translation
str = TranslationString::Version.find(1)
bad_ajax_request(:old_translation, id: 0)
Expand Down

0 comments on commit a55b484

Please sign in to comment.