Skip to content

Commit

Permalink
Merge pull request #2 from MushroomObserver/master
Browse files Browse the repository at this point in the history
Pushing to mo-nathan
  • Loading branch information
mo-nathan committed Jan 1, 2015
2 parents e4be8dc + 531bc2a commit 08f19fd
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 65 deletions.
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def show_previous_version(obj)
if previous_version = latest_version.previous
html += link_with_query("#{:show_name_previous_version.t}: %d" % previous_version.version,
:action => "show_past_#{type}", :id => obj.id,
:version => previous_version)
:version => previous_version.version)
if (previous_version.merge_source_id rescue false)
html += indent(1) + get_version_merge_link(obj, previous_version)
end
Expand Down
12 changes: 12 additions & 0 deletions app/models/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,18 @@ def self.suggest_alternate_spellings(str)
return results
end

def self.count_observations(names)
ids = names.map(&:id)
counts_and_ids = Name.connection.select(%(
SELECT count(*) c, names.id i FROM observations, names
WHERE observations.name_id = names.id
AND names.id IN (#{ids.join(", ")}) group by names.id
))
result = {}
counts_and_ids.each { |row| result[row["i"]] = row["c"] }
result
end

private

# Guess correct name of partial string.
Expand Down
3 changes: 2 additions & 1 deletion app/views/name/list_names.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ args = @test_pagination_args || {} %>
<%= paginate_block(@pages, args) do %>
<% if @objects != [] %>
<% odd_or_even = 0 %>
<% counts = Name.count_observations(@objects) %>
<table cellpadding="5" cellspacing="0" width="100%">
<% for name in @objects || []
odd_or_even = 1 - odd_or_even %>
<tr class="ListLine<%= odd_or_even %>">
<td>
<%= link_with_query(name.display_name.html_safe.t,
:action => 'show_name',
:id => name.id) + " [#{name.observations.count}]" %></td>
:id => name.id) + " [#{counts[name.id]}]" %></td>
<% if @extra_data %>
<% data = @extra_data[name.id]
if data && !data.empty?
Expand Down
1 change: 1 addition & 0 deletions config/locales/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ it.txt
pl.txt
pt.txt
ru.txt
tr.txt
11 changes: 11 additions & 0 deletions test/fixtures/glossary_terms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ plane_glossary_term:
thumb_image_id: 11
created_at: 2013-08-17 06:33:50
updated_at: 2013-08-17 06:35:20

square_glossary_term:
id: 4
version: 3
user_id: 1
name: Square
description: equilateral
thumb_image_id: nil
created_at: 2013-08-17 06:33:50
updated_at: 2013-08-17 06:35:20

25 changes: 25 additions & 0 deletions test/fixtures/glossary_terms_versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,28 @@ conic_glossary_term_v1:
updated_at: 2013-08-04 07:49:12
name: Conic
description: Cute little cone head

square_glossary_term_v1:
id: 4
glossary_term_id: 4
version: 1
updated_at: 2013-08-04 07:49:12
name: Square
description:

square_glossary_term_v2:
id: 5
glossary_term_id: 4
version: 2
updated_at: 2013-08-04 07:49:12
name: Square
description: quadrilateral

square_glossary_term_v3:
id: 6
glossary_term_id: 4
version: 3
updated_at: 2013-08-04 07:49:12
name: Square
description: equilateral

218 changes: 155 additions & 63 deletions test/functional/glossary_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,101 +1,193 @@
require 'test_helper'
require "test_helper"

# functional tests of glossary controller and views
class GlossaryControllerTest < FunctionalTestCase
def test_show_glossary_term
glossary_term = glossary_terms(:plane_glossary_term)
get_with_dump(:show_glossary_term, :id => glossary_term.id)
assert_template(action: 'show_glossary_term')
def conic
glossary_terms(:conic_glossary_term)
end

def plane
glossary_terms(:plane_glossary_term)
end

def square
glossary_terms(:square_glossary_term)
end
end

# tests of controller methods which do not change glossary terms
class GlossaryControllerShowAndIndexTest < GlossaryControllerTest
def setup
@controller = GlossaryController.new
end

def test_show_past_glossary_term
conic = glossary_terms(:conic_glossary_term)
get_with_dump(:show_past_glossary_term, :id => conic.id, :version => conic.version - 1)
assert_template(action: 'show_past_glossary_term', partial: "_glossary_term")
# ***** show *****
def test_show_glossary_term
get_with_dump(:show_glossary_term, id: plane.id)
assert_template(action: "show_glossary_term")
end

def test_show_past_glossary_term_no_version
conic = glossary_terms(:conic_glossary_term)
get_with_dump(:show_past_glossary_term, :id => conic.id)
get_with_dump(:show_past_glossary_term, id: conic.id)
assert_response(:redirect)
end

def test_show_past_glossary_term_with_version
get_with_dump(:show_past_glossary_term,
id: conic.id, version: conic.version - 1)
assert_template(action: "show_past_glossary_term",
partial: "_glossary_term")
end

test "show past glossary term prior version link target" do
prior_version_target = "/glossary/show_past_glossary_term/" \
"#{square.id}?version=#{square.version - 1}"
get(:show_glossary_term, id: square.id)
assert_select "a[href=?]", prior_version_target
end

# ***** index *****
def test_index
get_with_dump(:index)
assert_template(action: 'index')
assert_template(action: "index")
end
end

# tests of controller methods which create glossary terms
class GlossaryControllerCreateTest < GlossaryControllerTest
def setup
@controller = GlossaryController.new
end

# ***** create *****
def convex_params
{ glossary_term:
{ name: "Convex", description: "Boring" },
copyright_holder: "Me",
date: { copyright_year: 2013 }, upload: { license_id: 1 }
}
end

def posted_term
login_and_post_convex
GlossaryTerm.find(:all, order: "created_at DESC")[0]
end

def test_create_glossary_term
def login_and_post_convex
login
post(:create_glossary_term, convex_params)
end

def test_create_glossary_term_no_login
get(:create_glossary_term)
assert_response(:redirect)
end

def test_create_glossary_term_logged_in
login
get_with_dump(:create_glossary_term)
assert_template(action: 'create_glossary_term')
end

def create_glossary_term_params
return {
:glossary_term => {
:name => 'Convex',
:description => 'Boring old convex',
},
:copyright_holder => "Insil Choi",
:date => {
:copyright_year => '2013'
},
:upload => {
:license_id => '1'
}
}
assert_template(action: "create_glossary_term")
end

def test_create_glossary_term_name
assert_equal(convex_params[:glossary_term][:name], posted_term.name)
end

def test_create_glossary_term_post

def test_create_glossary_term_description
assert_equal(convex_params[:glossary_term][:description],
posted_term.description)
end

def test_create_glossary_term_rss_log
assert_not_nil(posted_term.rss_log)
end

def test_create_glossary_term_user_id
user = login
params = create_glossary_term_params
post(:create_glossary_term, params)
glossary_term = GlossaryTerm.find(:all, :order => "created_at DESC")[0]
assert_equal(params[:glossary_term][:name], glossary_term.name)
assert_equal(params[:glossary_term][:description], glossary_term.description)
assert_not_nil(glossary_term.rss_log)
assert_equal(user.id, glossary_term.user_id)
assert_equal(user.id, posted_term.user_id)
end

def test_create_glossary_term_redirect
login_and_post_convex
assert_response(:redirect)
end
end

# tests of controller methods which edit glossary terms
class GlossaryControllerEditTest < GlossaryControllerTest
def setup
@controller = GlossaryController.new
end

def test_edit_glossary_term
conic = glossary_terms(:conic_glossary_term)
get_with_dump(:edit_glossary_term, :id => conic.id)
# ***** edit *****
def test_edit_glossary_term_no_login
get_with_dump(:edit_glossary_term, id: conic.id)
assert_response(:redirect)
end

def test_edit_glossary_term_logged_in
login
get_with_dump(:edit_glossary_term, :id => conic.id)
assert_template(action: 'edit_glossary_term')
get_with_dump(:edit_glossary_term, id: conic.id)
assert_template(action: "edit_glossary_term")
end

def test_edit_glossary_term_post
conic = glossary_terms(:conic_glossary_term)
count = GlossaryTerm::Version.count

def changes_to_conic
{ id: conic.id,
glossary_term: { name: "Convex", description: "Boring old convex" },
copyright_holder: "Insil Choi", date: { copyright_year: 2013 },
upload: { license_id: 1 }
}
end

def post_conic_edit_changes
make_admin
post(:edit_glossary_term, changes_to_conic)
end

params = create_glossary_term_params
params[:id] = conic.id
post(:edit_glossary_term, params)
def post_conic_edit_changes_and_reload
post_conic_edit_changes
conic.reload
assert_equal(params[:glossary_term][:name], conic.name)
assert_equal(params[:glossary_term][:description], conic.description)
assert_equal(count+1, GlossaryTerm::Version.count)
end

def test_edit_glossary_term_redirect
post_conic_edit_changes
assert_response(:redirect)
end

def test_generate_and_show_past_glossary_term
def test_edit_glossary_term_count
old_count = GlossaryTerm::Version.count
post_conic_edit_changes
assert_equal(old_count + 1, GlossaryTerm::Version.count)
end

def test_edit_glossary_term_name
post_conic_edit_changes_and_reload
assert_equal(changes_to_conic[:glossary_term][:name], conic.name)
end

def test_edit_glossary_term_name_and_description
post_conic_edit_changes_and_reload
assert_equal(changes_to_conic[:glossary_term][:description],
conic.description)
end

def update_and_reload_plane_past_version
login
glossary_term = glossary_terms(:plane_glossary_term)
old_count = glossary_term.versions.length
glossary_term.update_attributes(:description => 'Are we flying yet?')
glossary_term.reload
new_count = glossary_term.versions.length
assert_equal(1, new_count - old_count)
get_with_dump(:show_past_glossary_term, :id => glossary_term.id, :version => glossary_term.version - 1)
assert_template(action: 'show_past_glossary_term', partial: "_glossary_term")
plane.update_attributes(description: "Are we flying yet?")
plane.reload
end

def test_generate_past_glossary_term
old_length = plane.versions.length
update_and_reload_plane_past_version
assert_equal(old_length + 1, plane.versions.length)
end

def test_generate_and_show_past_glossary_term
update_and_reload_plane_past_version
get_with_dump(:show_past_glossary_term,
id: plane.id, version: plane.version - 1)
assert_template(action: "show_past_glossary_term",
partial: "_glossary_term")
end
end

0 comments on commit 08f19fd

Please sign in to comment.