modified app to return a hash if current_user is not present; removed view specs in postsController specs & moved logic to js/jasmine

This commit is contained in:
danielgrippi 2011-12-28 17:15:28 -05:00 committed by Dennis Collinson
parent 096efee929
commit 719e265b68
12 changed files with 48 additions and 184 deletions

View file

@ -79,11 +79,8 @@ class StatusMessagesController < ApplicationController
end
respond_to do |format|
format.js {
errors = @status_message.errors.full_messages.collect { |msg| msg.gsub(/^Text/, "") }
render :json =>{:errors => errors}, :status => 422
}
format.html {redirect_to :back}
format.json { render :nothing, :status => 403 }
format.html { redirect_to :back }
end
end
end

View file

@ -1,12 +0,0 @@
<%= {:html => render(
:partial => 'shared/stream_element',
:locals => {
:post => @status_message,
:author => @status_message.author,
:photos => @status_message.photos,
:comments => [],
:all_aspects => current_user.aspects,
:reshare => nil
}
),
:post_id => @status_message.guid}.to_json.html_safe%>

View file

@ -12,19 +12,21 @@
<ul class="comments"> </ul>
<div class="new_comment_form_wrapper <%= comments_count > 0 ? '' : 'hidden' %>">
<form accept-charset="UTF-8" action="/posts/<%= id %>/comments" class="new_comment" id="new_comment_on_<%= id %>" method="post">
<a href="/people/<%= current_user.id %>">
<img src="<%= current_user.avatar.small %>" class="avatar" data-person-id="<%= current_user.id %>"/>
</a>
<% if(current_user) { %>
<div class="new_comment_form_wrapper <%= comments_count > 0 ? '' : 'hidden' %>">
<form accept-charset="UTF-8" action="/posts/<%= id %>/comments" class="new_comment" id="new_comment_on_<%= id %>" method="post">
<a href="/people/<%= current_user.id %>">
<img src="<%= current_user.avatar.small %>" class="avatar" data-person-id="<%= current_user.id %>"/>
</a>
<p>
<label for="comment_text_on_<%= id %>">Comment</label>
<textarea class="comment_box" id="comment_text_on_<%= id %>" name="text" rows="2" />
</p>
<div class="submit_button">
<input class="button creation" id="comment_submit_<%= id %>" name="commit" type="submit" value="Comment" />
</div>
</form>
</div>
<p>
<label for="comment_text_on_<%= id %>">Comment</label>
<textarea class="comment_box" id="comment_text_on_<%= id %>" name="text" rows="2" />
</p>
<div class="submit_button">
<input class="button creation" id="comment_submit_<%= id %>" name="commit" type="submit" value="Comment" />
</div>
</form>
</div>
<% } %>
</div>

View file

@ -6,7 +6,7 @@ var app = {
user: function(user) {
if(user) { return this._user = user; }
return this._user;
return this._user || {current_user : false};
},
initialize: function() {

View file

@ -26,8 +26,10 @@ app.views.Base = Backbone.View.extend({
var self = this;
_.each(this.subviews, function(property, selector){
var view = _.isFunction(self[property]) ? self[property]() : self[property]
self.$(selector).html(view.render().el)
view.delegateEvents();
if(view) {
self.$(selector).html(view.render().el)
view.delegateEvents();
}
})
return this

View file

@ -22,8 +22,16 @@ app.views.Post = app.views.StreamObject.extend({
],
initialize : function() {
this.feedbackView = new app.views.Feedback({model : this.model});
// commentStream view
this.commentStreamView = new app.views.CommentStream({ model : this.model});
// feedback view
if(window.app.user().current_user) {
this.feedbackView = new app.views.Feedback({model : this.model});
} else {
this.feedbackView = null;
}
return this;
},

View file

@ -23,8 +23,6 @@ describe PostsController do
it 'succeeds' do
get :show, "id" => @message.id
response.should be_success
doc.has_link?('Like').should be_true
doc.has_link?('Comment').should be_true
end
it 'succeeds on mobile' do
@ -38,9 +36,8 @@ describe PostsController do
end
it 'marks a corresponding notification as read' do
alice.comment("comment after me", :post => @message)
bob.comment("here you go", :post => @message)
note = Notification.where(:recipient_id => alice.id, :target_id => @message.id).first
note = Notification.create(:recipient => alice, :target => @message, :unread => true)
lambda{
get :show, :id => @message.id
note.reload
@ -55,7 +52,6 @@ describe PostsController do
end
context 'user not signed in' do
context 'given a public post' do
before :each do
@status = alice.post(:status_message, :text => "hello", :public => true, :to => 'all')
@ -76,27 +72,6 @@ describe PostsController do
get :show, :id => @status.guid, :format => :xml
response.body.should == @status.to_diaspora_xml
end
context 'with more than 3 comments' do
before do
(1..5).each do |i|
alice.comment "comment #{i}", :post => @status
end
end
it 'shows all comments of a public post' do
get :show, :id => @status.id
response.body.should =~ /comment 3/
response.body.should_not =~ /comment 2/
get :show, :id => @status.id, 'all_comments' => '1'
response.body.should =~ /comment 3/
response.body.should =~ /comment 2/
end
end
end
it 'does not show a private post' do
@ -116,127 +91,13 @@ describe PostsController do
it 'assumes guids less than 8 chars are ids and not guids' do
Post.should_receive(:where).with(hash_including(:id => @status.id)).and_return(Post)
get :show, :id => @status.id
response.status= 200
response.should be_success
end
it 'assumes guids more than (or equal to) 8 chars are actually guids' do
Post.should_receive(:where).with(hash_including(:guid => @status.guid)).and_return(Post)
get :show, :id => @status.guid
response.status= 200
end
end
end
context 'when a post is public' do
before do
@post = alice.post( :status_message, :public => true, :to => alice.aspects, :text => 'abc 123' )
end
context 'and visitor is not signed in' do
it 'does not show social links' do
get :show, 'id' => @post.id
doc.has_content?('abc 123').should be_true
doc.has_link?('Like').should be_false
doc.has_link?('Comment').should be_false
doc.has_link?('Reshare').should be_false
end
end
context 'and signed in as poster' do
before do
sign_in alice
end
it 'does not show a reshare link' do
get :show, 'id' => @post.id
doc.has_content?('abc 123').should be_true
doc.has_link?('Like').should be_true
doc.has_link?('Comment').should be_true
doc.has_link?('Reshare').should be_false
end
context 'a reshare of the post' do
before do
@reshare = bob.post( :reshare, :public => true, :root_guid => @post.guid, :to => bob.aspects )
end
it 'does not show a reshare link' do
get :show, 'id' => @reshare.id
doc.has_content?('abc 123').should be_true
doc.has_link?('Like').should be_true
doc.has_link?('Comment').should be_true
doc.has_link?('Reshare').should be_false
doc.has_link?('Reshare original').should be_false
doc.has_link?('1 reshare').should be_false
end
end
end
context 'and signed in as someone other than the poster' do
before do
sign_in bob
end
it 'shows reshare link' do
get :show, 'id' => @post.id
doc.has_content?('abc 123').should be_true
doc.has_link?('Like').should be_true
doc.has_link?('Comment').should be_true
doc.has_link?('Reshare').should be_true
end
end
context 'and signed in as the resharer of the post' do
context 'a reshare of the post' do
before do
@reshare = bob.post( :reshare, :public => true, :root_guid => @post.guid, :to => bob.aspects )
# Don't know why this is needed, but this spec fails without it
sign_in bob
end
it 'does not show any reshare link' do
get :show, 'id' => @reshare.id
doc.has_content?('abc 123').should be_true
doc.has_link?('Like').should be_true
doc.has_link?('Comment').should be_true
doc.has_link?('1 reshare').should be_false
doc.has_link?('Reshare').should be_false
end
end
end
context 'and signed in as neither the poster nor the resharer of the post' do
before do
sign_in eve
end
it 'shows reshare link' do
get :show, 'id' => @post.id
doc.has_content?('abc 123').should be_true
doc.has_link?('Like').should be_true
doc.has_link?('Comment').should be_true
doc.has_link?('Reshare').should be_true
end
context 'a reshare of the post' do
before do
@reshare = bob.post( :reshare, :public => true, :root_guid => @post.guid, :to => bob.aspects )
end
it 'shows a reshare link' do
get :show, 'id' => @reshare.id
doc.has_content?('abc 123').should be_true
doc.has_link?('Like').should be_true
doc.has_link?('Comment').should be_true
doc.has_link?('Reshare original').should be_true
end
response.should be_success
end
end
end

View file

@ -1,7 +1,7 @@
describe("app", function() {
describe("user", function() {
it("sets the user if given one and returns the current user", function() {
expect(app.user()).toBeUndefined();
expect(app.user()).toEqual({current_user : false});
app.user({name: "alice"});

View file

@ -1,6 +1,6 @@
describe("app.views.Feedback", function(){
beforeEach(function(){
window.current_user = app.user({id : 1, name: "alice", avatar : {small : "http://avatar.com/photo.jpg"}});
window.current_user = app.user({id : -1, name: "alice", avatar : {small : "http://avatar.com/photo.jpg"}});
var posts = $.parseJSON(spec.readFixture("multi_stream_json"))["posts"];
@ -60,6 +60,10 @@ describe("app.views.Feedback", function(){
this.view.render();
})
it("contains a .like_action", function(){
expect($(this.view.el).html()).toContain("like_action");
})
it("the like action should be 'Like'", function(){
expect(this.link().text()).toContain('Like');
})

View file

@ -11,11 +11,13 @@ describe("app.views.Post", function(){
this.statusMessage = this.collection.models[0];
})
it("contains a '.like_action' link", function(){
var view = new app.views.Post({model : this.statusMessage}).render();
var statusElement = $(view.el);
context("user not signed in", function(){
it("does not provide a Feedback view", function(){
window.current_user = app.user(null);
expect(statusElement.find(".like_action").html()).not.toBeNull();
var view = new app.views.Post({model : this.statusMessage}).render();
expect(view.feedbackView).toBeNull();
})
})
context("NSFW", function(){