Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Widget activity info #19

Closed
wants to merge 5 commits into from

2 participants

Commits on Jul 19, 2013
  1. @kyungbae
Commits on Jul 22, 2013
  1. @kyungbae

    Add more integration tests

    kyungbae authored
  2. @kyungbae
  3. @kyungbae

    Edit the integration test

    kyungbae authored
Commits on Jul 29, 2013
  1. @kyungbae
This page is out of date. Refresh to see the latest.
View
41 app/controllers/activity_controller.rb
@@ -0,0 +1,41 @@
+class ActivityController < ApplicationController
+ respond_to :json, :html
+
+ def activity_data
+ if params[:accession_no]
+ activities = ItemRelation.where(accession_no: params[:accession_no])
+ if activities.empty?
+ respond_with Hash["Not Found", "Not Found"]
+ else
+ type_array = ['Question', 'Discussion', 'Research', 'Translation', 'Transcription', 'Biography', 'Comment']
+
+ # Remove 'Contribution Request' objects since they are not considered
+ # as an activity.
+ activities.delete_if { |a| a.itemable.class == ContributionRequest }
+ respond_with collect_numbers(type_array, activities), :callback => params[:callback]
+ end
+ end
+ end
+
+ # For the comments request
+ # def comments
+ # if params[:accession_no]
+ # @comments = Comment.joins(:item_relations).where('accession_no = ?', params[:accession_no])
+ # if @comments.empty?
+ # @comments = Comment.all
+ # end
+ # end
+ # respond_with @comments
+ # end
+
+ def collect_numbers(types, activities)
+ if types.length > 0 and activities.length > 0
+ type = types.pop
+ num = activities.length
+ activities.delete_if { |ir| ir.itemable.class == "#{type}".constantize }
+ Hash["#{type}", num - activities.length].merge(collect_numbers(types, activities))
+ else
+ Hash["end", "end"]
+ end
+ end
+end
View
2  app/controllers/autocomplete_controller.rb
@@ -11,4 +11,4 @@ def users
# list[list.length] = { user_id: 0, label: "Invite the person!", user_email: "email", user_name: "name"}
respond_with list
end
-end
+end
View
11 app/controllers/contributions_controller.rb
@@ -9,10 +9,13 @@ def show
end
def index
- # Recently created or updated contributions
- @contributions = Contribution.where('type =? AND created_at > ?', params[:type] ||= 'Transcription', 6.days.ago).decorate
-
- # All items for now.
+ # List recently created or updated contributions if the reaquest is not from
+ # Qajar Women. If so, it responses the request.
+ if params[:accession_no]
+ @contributions = Contribution.joins(:item_relation).where('type = :type AND item_relations.accession_no = :accession_no', {type: params[:type], accession_no: params[:accession_no]}).decorate
+ else
+ @contributions = Contribution.where('type =? AND created_at > ?', params[:type] ||= 'Transcription', 6.days.ago).decorate
+ end
@requests = ContributionRequest.where('details = ? ', params[:type])
end
View
9 app/controllers/posts_controller.rb
@@ -58,6 +58,15 @@ def collection
posts = posts.search_text(params[:q])
end
+ # Handles the request from Qajar Women to list the post related an item
+ if params[:accession_no]
+ posts = Post.joins(:item_relations).where('type = ? AND accession_no = ?',
+ params[:type] ||= 'Research', params[:accession_no])
+ if posts.empty?
+ posts = scoped_collection
+ flash[:notice] = 'Missing item'
+ end
+ end
decorated_posts = posts.decorate.select do |post|
post.can_see?(current_user)
View
1  app/helpers/application_helper.rb
@@ -67,4 +67,5 @@ def choose_edit_path(resource)
edit_research_path(resource)
end
end
+
end
View
2  app/models/contribution.rb
@@ -1,7 +1,7 @@
class Contribution < ActiveRecord::Base
include AssociateItems
- attr_accessible :details, :uploads_attributes, :item_relation_attributes, :creator_id, :type
+ attr_accessible :details, :uploads_attributes, :item_relation_attributes, :creator_id, :type, :item_attributes
belongs_to :creator, class_name: 'User'
has_many :comments, as: :commentable, dependent: :destroy
View
2  config/routes.rb
@@ -1,6 +1,8 @@
WwqiPlatform::Application.routes.draw do
#devise_for :admin_users, ActiveAdmin::Devise.config
get "autocomplete/users"
+
+ get '/activity/activity_data'
devise_for :users, controllers: { registrations: 'registrations' }
devise_scope :user do
View
2  spec/factories/contributions.rb
@@ -6,7 +6,7 @@
creator
after(:build) do |contribution, evaluator|
- contribution.item_relation ||= build(:item_relation)
+ contribution.item_relation ||= build(:item_relation, itemable: contribution)
end
end
end
View
35 spec/factories/item_relations.rb
@@ -2,8 +2,39 @@
FactoryGirl.define do
factory :item_relation do
- accession_no { generate(:string) }
- association :itemable, factory: :question
item
+ accession_no { item ? item.accession_no : generate(:string) }
+
+ trait :with_discussion do
+ association :itemable, factory: :discussion
+ end
+
+ trait :with_research do
+ association :itemable, factory: :research
+ end
+
+ trait :with_translation do
+ association :itemable, factory: :translation
+ end
+
+ trait :with_transcription do
+ association :itemable, factory: :transcription
+ end
+
+ trait :with_biography do
+ association :itemable, factory: :biography
+ end
+
+ trait :with_comment do
+ association :itemable, factory: :comment
+ end
+
+ trait :with_contribution_request do
+ association :itemable, factory: :contribution_request
+ end
+
+ after(:build) do |ir|
+ ir.itemable = create(:question) if ir.itemable.nil?
+ end
end
end
View
89 spec/features/activity_request_spec.rb
@@ -0,0 +1,89 @@
+require 'spec_helper'
+
+describe 'Activity Request' do
+ let(:item) { create(:item,
+ accession_no: '31g166',
+ thumbnail: 'http://s3.amazonaws.com/assets.qajarwomen.org/thumbs/it_2387.jpg?1329177600',
+ name: 'Ewer'
+ ) }
+ let(:user) { create(:user) }
+
+ it 'redirects to the related question list' do
+ # Create 5 questions with the item and 3 questions without the item
+ create_list(:item_relation, 5, item: item)
+ create_list(:item_relation, 3)
+ visit '/threads?type=Question&accession_no=31g166'
+
+ # Test there are only questions related to the item showing on the page
+ expect(page).to have_content('question')
+ expect(page).not_to have_content('discussion')
+ expect(page).to have_css('.item-header', count: 5)
+ end
+
+ it 'redirects to the related discussion list' do
+ # Create 3 discussions with the item and 7 discussions without the item
+ create_list(:item_relation, 3, :with_discussion, item: item)
+ create_list(:item_relation, 7, :with_discussion)
+ visit '/threads?type=Discussion&accession_no=31g166'
+
+ # Test there are only discussions related to the item showing on the page
+ expect(page).to have_content('discussion')
+ expect(page).not_to have_content('question')
+ expect(page).to have_css('.item-header', count: 3)
+ end
+
+ it 'redirects to the related research list' do
+ # Create 9 researches with the item and 3 researches without the item
+ create_list(:item_relation, 9, :with_research, item: item)
+ create_list(:item_relation, 3, :with_research)
+ visit '/researches?accession_no=31g166'
+
+ # Test there are only resesearches related to the item showing on the page
+ expect(page).to have_content('research in progress')
+ expect(page).to have_css('.item-header', count: 9)
+ end
+
+ it 'redirects to the related translation list' do
+ # Create 2 translations with the item
+ create_contribution('Translation', item, 2)
+ visit '/contributions?type=Translation&accession_no=31g166'
+
+ expect(page).to have_content('contributed a translation for')
+ expect(page).to have_css('a', text: item.name, count: 2)
+ end
+
+ it 'redirects to the related transcription list' do
+ # Create 4 transcriptions with the item
+ create_contribution('Transcription', item, 4)
+ visit '/contributions?type=Transcription&accession_no=31g166'
+
+ expect(page).to have_content('contributed a transcription for')
+ expect(page).to have_css('a', text: item.name, count: 4)
+ end
+
+ it 'redirects to the related biography list' do
+ visit '/contributions?type=Biography&accession_no=31g166'
+
+ expect(page).to have_content('No recent contributions')
+ expect(page).not_to have_css('a', text: item.name)
+ end
+
+ def create_contribution(type, selected_item, repeat)
+ sign_in(user)
+ repeat.times do
+ visit new_contribution_path(type: type, item: selected_item)
+ fill_in "#{type.downcase}_details", with: "Transcription Test"
+ click_on "Submit #{type.titleize}"
+ end
+ sign_out
+ end
+
+ it 'redirects to the root page if the item is not found' do
+ visit '/threads?type=discussion&accession_no=31g6'
+
+ expect(page).to have_content('Missing item')
+ expect(page).not_to have_content('question')
+ expect(page).not_to have_content('discussion')
+ expect(page).not_to have_content('research')
+ end
+end
View
17 spec/features/invitations_spec.rb
@@ -2,14 +2,10 @@
feature "Post creator(registered user)" do
describe "as a test user" do
- before :each do
- @user = create(:user)
- visit new_post_path
- click_on 'Sign in'
- expect(page).to have_content('SIGN IN')
- fill_in 'user_email', with: @user.email
- fill_in 'user_password', with: @user.password
- click_button 'Sign In'
+ let(:user) { create(:user) }
+
+ before do
+ sign_in(user)
visit new_post_path
end
@@ -20,7 +16,6 @@
fill_post('Queen', 'another one bites the dust')
click_button 'People I Choose'
-
page.execute_script('$("#add_invitation").modal("show")')
# Check the modal form has been opened
@@ -31,7 +26,6 @@
# Fill the invitation form
fill_invitation(test_email, test_name, test_message)
-
page.execute_script("$('#create_invitation').click()")
sleep 5
@@ -50,6 +44,7 @@
end
click_button 'Submit'
+
expect(page).to have_content('Thread was successfully posted.')
end
@@ -71,6 +66,7 @@
end
it 'adds a invitee without a recipient name and message', js: true do
+ find('#post_title').visible?
fill_post('Test Title', 'This is a test.')
page.execute_script('$("#add_invitation").modal("show")')
sleep 5
@@ -106,7 +102,6 @@ def fill_invitation(email, name, message)
end
def fill_post(title, detail)
- find('#post_title').visible?
fill_in 'post_title', with: title
page.execute_script("editor.setValue('#{detail}')")
end
View
53 spec/requests/activity_info_request_spec.rb
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+describe 'json request' do
+ let!(:item) { create(:item, accession_no: '1253A24') }
+
+ # Create multiple activities with an item
+ let!(:item_related_questions) { create_list(:item_relation, 4, item: item) }
+ let!(:item_related_discussions) { create_list(:item_relation, 6, :with_discussion, item: item) }
+ let!(:item_related_researches) { create_list(:item_relation, 2, :with_research, item: item) }
+ let!(:item_related_translations) { create_list(:item_relation, 5, :with_translation, item: item) }
+ let!(:item_related_transcriptions) { create_list(:item_relation, 1, :with_transcription, item: item) }
+ let!(:item_related_biographies) { create_list(:item_relation, 3, :with_biography, item: item) }
+ let!(:item_related_comments) { create_list(:item_relation, 7, :with_comment, item: item) }
+
+ # Creating a contribution request is not considered as an activity. Also, it
+ # should not change any activity counts.
+ let!(:item_related_contribution_request) { create_list(:item_relation, 8, :with_contribution_request, item: item) }
+
+ # Make a request for the activity data in json format
+ before { get '/activity/activity_data?accession_no=1253A24', :format => :json }
+
+ it 'receives a success response' do
+ expect(response).to be_success
+ expect(response.header['Content-Type']).to include 'application/json'
+ end
+
+ it 'receives a activity data in json' do
+ activity = ActiveSupport::JSON.decode(response.body)
+
+ # Test each activity data are matched as expected
+ expect(activity['Question']).to eq 4
+ expect(activity['Discussion']).to eq 6
+ expect(activity['Research']).to eq 2
+ expect(activity['Translation']).to eq 5
+ expect(activity['Transcription']).to eq 1
+ expect(activity['Biography']).to eq 3
+ expect(activity['Comment']).to eq 7
+ end
+
+ it 'receives "Not Found" if the item is not found' do
+
+ # Make a request with a wrong accession number
+ get '/activity/activity_data?accession_no=1253', :format => :json
+
+ # Test it still receives a success response from the platform
+ expect(response).to be_success
+ expect(response.header['Content-Type']).to include 'application/json'
+
+ # Test the received json data indicates that the item is not found
+ activity = ActiveSupport::JSON.decode(response.body)
+ expect(activity['Not Found']).to eq 'Not Found'
+ end
+end
View
1  spec/spec_helper.rb
@@ -67,4 +67,5 @@
config.include Warden::Test::Helpers
config.include WaitSteps
config.color_enabled = true
+ config.include RSpec::Rails::RequestExampleGroup
end
Something went wrong with that request. Please try again.