diff options
| author | mo k <mo@mokhan.ca> | 2012-06-11 20:56:05 -0600 |
|---|---|---|
| committer | mo k <mo@mokhan.ca> | 2012-06-11 20:56:05 -0600 |
| commit | ee686cc7845a466abc39baccf298459fa93abfad (patch) | |
| tree | d7ea78f08455f6d9986cb32449f270940832228f | |
| parent | a19f70e8b03edab0e66cfa2d845d9755d9166a0f (diff) | |
| parent | d4a1d71f7d0ce2c3f915b57582b6c1dcb712304f (diff) | |
Merge branch 'master' of bitbucket.org:mocheen/cakeside
| -rw-r--r-- | app/controllers/comments_controller.rb | 16 | ||||
| -rw-r--r-- | app/models/comment.rb | 4 | ||||
| -rw-r--r-- | app/models/user.rb | 5 | ||||
| -rw-r--r-- | app/services/comment_on_creation_command.rb | 9 | ||||
| -rw-r--r-- | config/application.rb | 3 | ||||
| -rw-r--r-- | config/sphinx.yml | 10 | ||||
| -rw-r--r-- | spec/controllers/comments_controller_spec.rb | 24 | ||||
| -rw-r--r-- | spec/models/comment_spec.rb | 18 | ||||
| -rw-r--r-- | spec/models/user_spec.rb | 27 | ||||
| -rw-r--r-- | spec/services/command_on_creation_command_spec.rb | 20 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 56 |
11 files changed, 103 insertions, 89 deletions
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 4848c972..6ea6f0b1 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -6,14 +6,12 @@ class CommentsController < ApplicationController end def create - creation = Creation.find(params[:creation_id]) - comment = Comment.build_from(creation, current_user.id, params[:comment][:body]) - if comment.save - flash[:notice] = "Your comment was added!" - redirect_to creation_path(creation) - else - flash[:error] = "Ooops... we couldn't save your comment at this time." - render 'new' - end + command_for(CommentOnCreationCommand).run(params) + flash[:notice] = params[:comment][:body] + redirect_to :controller => 'creations', :action => 'show', :id => params[:creation_id] + end + + def command_for(command) + command.new(current_user) end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 8587f611..183c85b7 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -25,6 +25,10 @@ class Comment < ActiveRecord::Base c end + def self.create_for(user,creation,body) + self.build_from(creation, user.id, body) + end + #helper method to check if a comment has children def has_children? self.children.size > 0 diff --git a/app/models/user.rb b/app/models/user.rb index cbc5c40e..2204dd7d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -35,5 +35,8 @@ class User < ActiveRecord::Base def owns(creation) creation.user == self end -end + def comment_on(creation, comment) + Comment.create_for(self, creation, comment) + end +end diff --git a/app/services/comment_on_creation_command.rb b/app/services/comment_on_creation_command.rb new file mode 100644 index 00000000..3217dcbd --- /dev/null +++ b/app/services/comment_on_creation_command.rb @@ -0,0 +1,9 @@ +class CommentOnCreationCommand + def initialize(user) + @current_user = user + end + def run(params) + comment = @current_user.comment_on(Creation.find(params[:creation_id]), params[:comment][:body]) + comment.save + end +end diff --git a/config/application.rb b/config/application.rb index 2d61e6d8..5ef98155 100644 --- a/config/application.rb +++ b/config/application.rb @@ -16,7 +16,8 @@ module Cake # -- all .rb files in that directory are automatically loaded. # Custom directories with classes and modules you want to be autoloadable. - # config.autoload_paths += %W(#{config.root}/extras) + #config.autoload_paths += %W(#{config.root}/app/services) + config.autoload_paths += %W(#{config.root}/app/services) # Only load the plugins named here, in the order given (default is alphabetical). # :all can be used as a placeholder for all plugins not explicitly named. diff --git a/config/sphinx.yml b/config/sphinx.yml deleted file mode 100644 index 0445643b..00000000 --- a/config/sphinx.yml +++ /dev/null @@ -1,10 +0,0 @@ -development: - -test: - -staging: - -production: - enable_star: 1 - min_infix_len: 1 - diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index 1b4fb834..1ea63211 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -4,15 +4,29 @@ require 'fakes-rspec' describe CommentsController do let(:sut) { CommentsController.new } + describe 'when commenting on a creation' do - let(:comment) { fake } + let(:flash_hash) { {} } + let(:payload){ {:creation_id=>88, :comment => {:body => 'blah'}} } + let(:command){ fake } + + before(:each) do + sut.stub(:params).and_return(payload) + sut.stub(:flash).and_return(flash_hash) + sut.stub(:redirect_to).and_return(nil) + sut.stub(:command_for).with(CommentOnCreationCommand).and_return(command) + + sut.create + end it "should save the new comment" do - #comment.should have_receieved(:save) + command.should have_received(:run, payload) end - before(:each) do - #Comment.stub(:build_from).and_return(comment) - #sut.create + it "should display a message indicated that the comment was saved" do + flash_hash[:notice].should_not be_nil + end + it "should redirect to the creation#show page" do + end end end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb new file mode 100644 index 00000000..a9e9d739 --- /dev/null +++ b/spec/models/comment_spec.rb @@ -0,0 +1,18 @@ +require_relative '../../app/models/comment.rb' + +describe Comment do + describe "when creating a new comment" do + it "should return a comment with the correct body" do + @result.body.should == "blah" + end + it "should track the user id" do + @result.user_id.should == user.id + end + before(:each) do + @result = Comment.create_for(user, creation, 'blah') + end + + let(:user) { fake } + let(:creation) { Creation.new } + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index bbe56d4e..f5de1b19 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -2,13 +2,13 @@ describe User do #describe "when a user already likes a creation" do - #it "should not let the user like it again" do - #creation = FactoryGirl.create(:creation) - #user = FactoryGirl.create(:user) - #user.add_favorite(creation) - #user.add_favorite(creation) - #creation.favorites.length.should eq(1) - #end + #it "should not let the user like it again" do + #creation = FactoryGirl.create(:creation) + #user = FactoryGirl.create(:user) + #user.add_favorite(creation) + #user.add_favorite(creation) + #creation.favorites.length.should eq(1) + #end #end describe "when a website url is supplied" do describe "when the url is valid" do @@ -36,4 +36,17 @@ describe User do end end end + describe "when commenting on a creation" do + let(:comment) { fake } + let(:creation) { fake } + let(:sut) { User.new } + + it "should create a new comment" do + @result.should == comment + end + before(:each) do + Comment.stub(:create_for).with(sut, creation, "cool cake").and_return(comment) + @result = sut.comment_on(creation, "cool cake") + end + end end diff --git a/spec/services/command_on_creation_command_spec.rb b/spec/services/command_on_creation_command_spec.rb new file mode 100644 index 00000000..b3486c36 --- /dev/null +++ b/spec/services/command_on_creation_command_spec.rb @@ -0,0 +1,20 @@ +describe CommentOnCreationCommand do + let(:sut) { CommentOnCreationCommand.new(user) } + let(:user) { fake } + describe "when run" do + let(:creation) { fake } + let(:comment) { fake } + it "should add the new comment to the creation" do + user.should have_received(:comment_on, creation, "wow") + end + it "should save the comment" do + comment.should have_received(:save) + end + before(:each) do + params = {:creation_id => 8, :comment => { :body => "wow" }} + Creation.stub(:find).with(8).and_return(creation) + user.stub(:comment_on).with(creation, "wow").and_return(comment) + sut.run(params) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a6c8b0dd..5820bccb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,43 +1,16 @@ require 'rubygems' require 'spork' -#uncomment the following line to use spork with the debugger -#require 'spork/ext/ruby-debug' Spork.prefork do - # Loading more in this block will cause your tests to run faster. However, - # if you change any configuration or code from libraries loaded here, you'll - # need to restart spork for it take effect. - # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'capybara/rspec' - # Requires supporting ruby files with custom matchers and macros, etc, - # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} - RSpec.configure do |config| - # == Mock Framework - # - # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: - # - # config.mock_with :mocha - # config.mock_with :flexmock - # config.mock_with :rr config.mock_with :rspec - - # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures - # config.fixture_path = "#{::Rails.root}/spec/fixtures" - - # If you're not using ActiveRecord, or you'd prefer not to run each of your - # examples within a transaction, remove the following line or assign false - # instead of true. config.use_transactional_fixtures = true - - # If true, the base class of anonymous controllers will be inferred - # automatically. This will be the default behavior in future versions of - # rspec-rails. config.infer_base_class_for_anonymous_controllers = false end end @@ -47,32 +20,3 @@ Spork.each_run do Devise.stretches = 1 Rails.logger.level = 4 end - -# --- Instructions --- -# Sort the contents of this file into a Spork.prefork and a Spork.each_run -# block. -# -# The Spork.prefork block is run only once when the spork server is started. -# You typically want to place most of your (slow) initializer code in here, in -# particular, require'ing any 3rd-party gems that you don't normally modify -# during development. -# -# The Spork.each_run block is run each time you run your specs. In case you -# need to load files that tend to change during development, require them here. -# With Rails, your application modules are loaded automatically, so sometimes -# this block can remain empty. -# -# Note: You can modify files loaded *from* the Spork.each_run block without -# restarting the spork server. However, this file itself will not be reloaded, -# so if you change any of the code inside the each_run block, you still need to -# restart the server. In general, if you have non-trivial code in this file, -# it's advisable to move it into a separate file so you can easily edit it -# without restarting spork. (For example, with RSpec, you could move -# non-trivial code into a file spec/support/my_helper.rb, making sure that the -# spec/support/* files are require'd from inside the each_run block.) -# -# Any code that is left outside the two blocks will be run during preforking -# *and* during each_run -- that's probably not what you want. -# -# These instructions should self-destruct in 10 seconds. If they don't, feel -# free to delete them. |
