diff --git a/spec/dummy/app/assets/javascripts/application.js b/spec/dummy/app/assets/javascripts/application.js deleted file mode 100644 index 15ebed9..0000000 --- a/spec/dummy/app/assets/javascripts/application.js +++ /dev/null @@ -1,13 +0,0 @@ -// This is a manifest file that'll be compiled into application.js, which will include all the files -// listed below. -// -// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, -// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. -// -// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the -// the compiled file. -// -// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD -// GO AFTER THE REQUIRES BELOW. -// -//= require_tree . diff --git a/spec/dummy/app/assets/stylesheets/application.css b/spec/dummy/app/assets/stylesheets/application.css deleted file mode 100644 index 3192ec8..0000000 --- a/spec/dummy/app/assets/stylesheets/application.css +++ /dev/null @@ -1,13 +0,0 @@ -/* - * This is a manifest file that'll be compiled into application.css, which will include all the files - * listed below. - * - * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, - * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. - * - * You're free to add application-wide styles to this file and they'll appear at the top of the - * compiled file, but it's generally better to create a new file per style scope. - * - *= require_self - *= require_tree . - */ diff --git a/spec/dummy/app/controllers/application_controller.rb b/spec/dummy/app/controllers/application_controller.rb index c3bb2fc..bbe1d38 100644 --- a/spec/dummy/app/controllers/application_controller.rb +++ b/spec/dummy/app/controllers/application_controller.rb @@ -1,7 +1,4 @@ class ApplicationController < ActionController::Base + include LocalizedUrls protect_from_forgery - - def default_url_options(options = {}) - {locale: I18n.locale, host: "www.example.com", port: nil} - end end diff --git a/spec/dummy/app/controllers/localized_urls.rb b/spec/dummy/app/controllers/localized_urls.rb new file mode 100644 index 0000000..1fd349e --- /dev/null +++ b/spec/dummy/app/controllers/localized_urls.rb @@ -0,0 +1,5 @@ +module LocalizedUrls + def default_url_options(options = {}) + {locale: I18n.locale, host: "www.example.com", port: nil} + end +end diff --git a/spec/dummy/app/controllers/posts_controller.rb b/spec/dummy/app/controllers/posts_controller.rb index ddc929c..e6e9aaf 100644 --- a/spec/dummy/app/controllers/posts_controller.rb +++ b/spec/dummy/app/controllers/posts_controller.rb @@ -1,5 +1,17 @@ class PostsController < ApplicationController def show + fetch_post + end + + def mail + fetch_post + email = PostMailer.decorated_email(@post).deliver + render text: email.body + end + + private + + def fetch_post @post = Post.find(params[:id]).decorate end end diff --git a/spec/dummy/app/mailers/.gitkeep b/spec/dummy/app/mailers/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/spec/dummy/app/mailers/application_mailer.rb b/spec/dummy/app/mailers/application_mailer.rb new file mode 100644 index 0000000..1fffe38 --- /dev/null +++ b/spec/dummy/app/mailers/application_mailer.rb @@ -0,0 +1,3 @@ +class ApplicationMailer < ActionMailer::Base + include LocalizedUrls +end diff --git a/spec/dummy/app/mailers/post_mailer.rb b/spec/dummy/app/mailers/post_mailer.rb new file mode 100644 index 0000000..da6f258 --- /dev/null +++ b/spec/dummy/app/mailers/post_mailer.rb @@ -0,0 +1,9 @@ +class PostMailer < ApplicationMailer + default from: "from@example.com" + layout "application" + + def decorated_email(post) + @post = post.decorate + mail to: "to@example.com", subject: "A decorated post" + end +end diff --git a/spec/dummy/app/models/.gitkeep b/spec/dummy/app/models/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/spec/dummy/app/views/layouts/application.html.erb b/spec/dummy/app/views/layouts/application.html.erb index 4cab268..9bc7c74 100644 --- a/spec/dummy/app/views/layouts/application.html.erb +++ b/spec/dummy/app/views/layouts/application.html.erb @@ -2,9 +2,6 @@ Dummy - <%= stylesheet_link_tag "application", :media => "all" %> - <%= javascript_include_tag "application" %> - <%= csrf_meta_tags %> diff --git a/spec/dummy/app/views/post_mailer/decorated_email.html.erb b/spec/dummy/app/views/post_mailer/decorated_email.html.erb new file mode 100644 index 0000000..83969c2 --- /dev/null +++ b/spec/dummy/app/views/post_mailer/decorated_email.html.erb @@ -0,0 +1 @@ +<%= render @post %> diff --git a/spec/dummy/app/views/posts/_post.html.erb b/spec/dummy/app/views/posts/_post.html.erb new file mode 100644 index 0000000..ba0387e --- /dev/null +++ b/spec/dummy/app/views/posts/_post.html.erb @@ -0,0 +1,19 @@ +
+
Environment:
+
<%= Rails.env %>
+ +
Posted:
+
<%= post.posted_date %>
+ +
Path with model:
+
<%= post.path_with_model %>
+ +
Path with id:
+
<%= post.path_with_id %>
+ +
URL with model:
+
<%= post.url_with_model %>
+ +
URL with id:
+
<%= post.url_with_id %>
+
diff --git a/spec/dummy/app/views/posts/show.html.erb b/spec/dummy/app/views/posts/show.html.erb index 71a3653..83969c2 100644 --- a/spec/dummy/app/views/posts/show.html.erb +++ b/spec/dummy/app/views/posts/show.html.erb @@ -1,19 +1 @@ -
-
Environment:
-
<%= Rails.env %>
- -
Posted:
-
<%= @post.posted_date %>
- -
Path with model:
-
<%= @post.path_with_model %>
- -
Path with id:
-
<%= @post.path_with_id %>
- -
URL with model:
-
<%= @post.url_with_model %>
- -
URL with id:
-
<%= @post.url_with_id %>
-
+<%= render @post %> diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index 26eda12..6c219a8 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -54,6 +54,11 @@ module Dummy # Version of your assets, change this if you want to expire all your assets config.assets.version = '1.0' + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test end end diff --git a/spec/dummy/config/environments/development.rb b/spec/dummy/config/environments/development.rb index 82c74d1..e58bfbb 100644 --- a/spec/dummy/config/environments/development.rb +++ b/spec/dummy/config/environments/development.rb @@ -13,9 +13,6 @@ Dummy::Application.configure do config.consider_all_requests_local = true config.action_controller.perform_caching = false - # Don't care if the mailer can't send - config.action_mailer.raise_delivery_errors = false - # Print deprecation notices to the Rails logger config.active_support.deprecation = :log diff --git a/spec/dummy/config/environments/production.rb b/spec/dummy/config/environments/production.rb index 5ce2e0f..238a57d 100644 --- a/spec/dummy/config/environments/production.rb +++ b/spec/dummy/config/environments/production.rb @@ -39,9 +39,6 @@ Dummy::Application.configure do # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) # config.assets.precompile += %w( search.js ) - # Disable delivery errors, bad email addresses will be ignored - # config.action_mailer.raise_delivery_errors = false - # Enable threaded mode # config.threadsafe! diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index f1a4814..0adb49d 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -24,11 +24,6 @@ Dummy::Application.configure do # Disable request forgery protection in test environment config.action_controller.allow_forgery_protection = false - # Tell Action Mailer not to deliver emails to the real world. - # The :test delivery method accumulates sent emails in the - # ActionMailer::Base.deliveries array. - config.action_mailer.delivery_method = :test - # Raise exception on mass assignment protection for Active Record models config.active_record.mass_assignment_sanitizer = :strict diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 4c10f8f..a01f682 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -1,5 +1,7 @@ Dummy::Application.routes.draw do scope "(:locale)", locale: /en|zh/ do - resources :posts, only: [:show] + resources :posts, only: [:show] do + get "mail", on: :member + end end end diff --git a/spec/dummy/spec/mailers/post_mailer_spec.rb b/spec/dummy/spec/mailers/post_mailer_spec.rb new file mode 100644 index 0000000..2e435ba --- /dev/null +++ b/spec/dummy/spec/mailers/post_mailer_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe PostMailer do + describe "#decorated_email" do + subject { Capybara.string(email.body.to_s) } + let(:email) { PostMailer.decorated_email(post).deliver } + let(:post) { Post.create } + + it "decorates" do + subject.should have_content "Today" + end + + it "can use path helpers with a model" do + subject.should have_css "#path_with_model", text: "/en/posts/#{post.id}" + end + + it "can use path helpers with an id" do + subject.should have_css "#path_with_id", text: "/en/posts/#{post.id}" + end + + it "can use url helpers with a model" do + subject.should have_css "#url_with_model", text: "http://www.example.com/en/posts/#{post.id}" + end + + it "can use url helpers with an id" do + subject.should have_css "#url_with_id", text: "http://www.example.com/en/posts/#{post.id}" + end + end +end diff --git a/spec/integration/integration_spec.rb b/spec/integration/integration_spec.rb index 62e3a64..e6a8ee4 100644 --- a/spec/integration/integration_spec.rb +++ b/spec/integration/integration_spec.rb @@ -1,48 +1,49 @@ require 'spec_helper' require 'support/dummy_app' +shared_examples_for "a decorator in a view" do + it "works" do + # it runs in the correct environment + page.should have_css "#environment", text: environment + + # it can use path helpers with a model + page.should have_css "#path_with_model", text: "/en/posts/1" + + # it can use path helpers with an id + page.should have_css "#path_with_id", text: "/en/posts/1" + + # it can use url helpers with a model + page.should have_css "#url_with_model", text: "http://www.example.com/en/posts/1" + + # it can use url helpers with an id + page.should have_css "#url_with_id", text: "http://www.example.com/en/posts/1" + end +end + describe "integration" do include Capybara::DSL - environment = ENV["RAILS_ENV"].to_s - raise ArgumentError, "RAILS_ENV must be development or production" unless ["development", "production"].include?(environment) + rails_env = ENV["RAILS_ENV"].to_s + raise ArgumentError, "RAILS_ENV must be development or production" unless ["development", "production"].include?(rails_env) - app = DummyApp.new(environment) + app = DummyApp.new(rails_env) app.start_server do - describe "in #{environment}" do + describe "in #{rails_env}" do + let(:environment) { rails_env } before { Capybara.app_host = app.url } - it "runs in the correct environment" do - visit("/posts/1") - page.should have_css "#environment", text: environment + context "in a view" do + before { visit("/posts/1") } + + it_behaves_like "a decorator in a view" end - it "decorates" do - visit("/posts/1") - page.should have_content "Today" - end + context "in a mailer" do + before { visit("/posts/1/mail") } - it "can use path helpers with a model" do - visit("/posts/1") - page.should have_css "#path_with_model", text: "/en/posts/1" + it_behaves_like "a decorator in a view" end - - it "can use path helpers with an id" do - visit("/posts/1") - page.should have_css "#path_with_id", text: "/en/posts/1" - end - - it "can use url helpers with a model" do - visit("/posts/1") - page.should have_css "#url_with_model", text: "http://www.example.com/en/posts/1" - end - - it "can use url helpers with an id" do - visit("/posts/1") - page.should have_css "#url_with_id", text: "http://www.example.com/en/posts/1" - end - end end