Skip to content

Commit

Permalink
Add a full_title helper
Browse files Browse the repository at this point in the history
  • Loading branch information
adeeb1 committed May 18, 2013
1 parent c4282eb commit 58f142b
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 20 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
--color
--drb
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ gem 'rails', '3.2.13'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
gem 'guard-rspec', '1.2.1'
gem 'guard-spork', '1.2.0'
gem 'childprocess', '0.3.6'
gem 'spork', '0.9.2'
end

# Gems used only for assets and not required
Expand All @@ -23,4 +27,11 @@ end

group :production do
gem 'pg', '0.12.2'
end

# Test gems on Linux
group :test do
gem 'capybara', '1.1.2'
gem 'rb-inotify', '0.8.8'
gem 'libnotify', '0.5.9'
end
38 changes: 36 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ GEM
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
childprocess (0.3.9)
ffi (~> 1.0, >= 1.0.11)
childprocess (0.3.6)
ffi (~> 1.0, >= 1.0.6)
coderay (1.0.9)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
Expand All @@ -51,21 +52,43 @@ GEM
execjs (1.4.0)
multi_json (~> 1.0)
ffi (1.8.1)
formatador (0.2.4)
guard (1.7.0)
formatador (>= 0.2.4)
listen (>= 0.6.0)
lumberjack (>= 1.0.2)
pry (>= 0.9.10)
thor (>= 0.14.6)
guard-rspec (1.2.1)
guard (>= 1.1)
guard-spork (1.2.0)
childprocess
guard (>= 1.1)
spork (>= 0.8.4)
sys-proctable
hike (1.2.2)
i18n (0.6.1)
journey (1.0.4)
jquery-rails (2.0.2)
railties (>= 3.2.0, < 5.0)
thor (~> 0.14)
json (1.8.0)
libnotify (0.5.9)
listen (0.7.3)
lumberjack (1.0.3)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.8.1)
mime-types (1.23)
multi_json (1.7.3)
nokogiri (1.5.9)
pg (0.12.2)
polyglot (0.3.3)
pry (0.9.12.2)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
Expand All @@ -89,6 +112,8 @@ GEM
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (10.0.4)
rb-inotify (0.8.8)
ffi (>= 0.5.0)
rdoc (3.12.2)
json (~> 1.4)
rspec (2.11.0)
Expand All @@ -115,12 +140,15 @@ GEM
multi_json (~> 1.0)
rubyzip
websocket (~> 1.0.4)
slop (3.4.5)
spork (0.9.2)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.5)
sys-proctable (0.9.3)
thor (0.18.1)
tilt (1.4.1)
treetop (1.4.12)
Expand All @@ -139,11 +167,17 @@ PLATFORMS

DEPENDENCIES
capybara (= 1.1.2)
childprocess (= 0.3.6)
coffee-rails (= 3.2.2)
guard-rspec (= 1.2.1)
guard-spork (= 1.2.0)
jquery-rails (= 2.0.2)
libnotify (= 0.5.9)
pg (= 0.12.2)
rails (= 3.2.13)
rb-inotify (= 0.8.8)
rspec-rails (= 2.11.0)
sass-rails (= 3.2.5)
spork (= 0.9.2)
sqlite3 (= 1.3.5)
uglifier (= 1.2.3)
62 changes: 62 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

require 'active_support/core_ext'

guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch(%r{^config/environments/.+\.rb$})
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb')
watch('test/test_helper.rb')
watch('spec/support/')
end

guard 'rspec', :version => 2, :all_after_pass => false, :cli => '--drb' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }

# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }

# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }

watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m|
["spec/routing/#{m[1]}_routing_spec.rb",
"spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
"spec/acceptance/#{m[1]}_spec.rb",
(m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
"spec/requests/#{m[1].singularize}_pages_spec.rb")]
end
watch(%r{^app/views/(.+)/}) do |m|
(m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
"spec/requests/#{m[1].singularize}_pages_spec.rb")
end
end


guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end
17 changes: 17 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
module ApplicationHelper

# Returns the full title on a per-page basis
def full_title(page_title)
# Set the base title
base_title = "Ruby on Rails Tutorial Sample App"

# Check if a title was NOT specified on the page
if page_title.empty?
# It wasn't, so set the full title to the base
# title
return base_title
else # A title was specified on the page
# Concatenate the base title with the page's
# title
return "#{base_title} | #{page_title}"
end
end
end
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
<title><%= full_title(yield(:title)) %></title>

<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
Expand Down
7 changes: 3 additions & 4 deletions app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<h1>Sample App</h1>

<p> This is the home page for the <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> sample application.
</p>
<p> This is the home page for the <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> sample application.
</p>
40 changes: 27 additions & 13 deletions spec/requests/static_pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def checkvalue(message, url, selector, text)
# "Ruby on Rails Tutorial Sample App" text if the
# selector is a 'title' tag
if (selector == 'title')
text = "Ruby on Rails Tutorial Sample App | " + text
text = "Ruby on Rails Tutorial Sample App" + text
end

# Check if the specified selector ('h1' or 'title')
Expand All @@ -18,41 +18,55 @@ def checkvalue(message, url, selector, text)
end
end

def checknovalue(message, url, selector, text)
it "#{message}" do
# Visit the specified URL
visit url

# Check if the specified selector ('h1' or 'title')
# does NOT contain the value of 'text'
page.should_not have_selector(selector, :text => text)
end
end

describe "Static pages" do

describe "Home page" do

checkvalue("should have the content 'Sample App'",
checkvalue("should have the h1 'Sample App'",
'/static_pages/home', 'h1', 'Sample App')

checkvalue("should have the right title",
'/static_pages/home', 'title', 'Home')
checkvalue("should have the base title",
'/static_pages/home', 'title', '')

checknovalue("should not have a custom page title",
'/static_pages/home', 'title', '| Home')
end

describe "Help page" do

checkvalue("should have the content 'Help'",
checkvalue("should have the h1 'Help'",
'/static_pages/help', 'h1', 'Help')

checkvalue("should have the right title",
'/static_pages/help', 'title', 'Help')
checkvalue("should have the base title",
'/static_pages/help', 'title', ' | Help')
end

describe "About page" do

checkvalue("should have the content 'About Us'",
checkvalue("should have the h1 'About Us'",
'/static_pages/about', 'h1', 'About Us')

checkvalue("should have the right title",
'/static_pages/about', 'title', 'About Us')
checkvalue("should have the base title",
'/static_pages/about', 'title', ' | About Us')
end

describe "Contact page" do

checkvalue("should have the content 'Contact'",
checkvalue("should have the h1 'Contact'",
'/static_pages/contact', 'h1', 'Contact')

checkvalue("should have the right title",
'/static_pages/contact', 'title', 'Contact')
checkvalue("should have the base title",
'/static_pages/contact', 'title', ' | Contact')
end
end
79 changes: 79 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,82 @@
require 'rubygems'
require 'spork'

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 'rspec/autorun'

# 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

Spork.each_run do
# This code will be run each time you run your specs.

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.




# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
Expand Down

0 comments on commit 58f142b

Please sign in to comment.