From 6cdcd2511c336f235a2f04856e452a342569ee61 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 12 Dec 2013 12:52:08 +0100 Subject: [PATCH] stuff --- app/models/user.rb | 6 ++++- app/views/layouts/application.html.erb | 19 +++++++------- app/views/roles/index.html.erb | 18 ++++++-------- app/views/tours/index.html.erb | 27 +++++++++++--------- config/routes.rb | 1 + spec/factories.rb | 4 +-- spec/models/user_spec.rb | 14 ++++++----- spec/views/roles/index.html.erb_spec.rb | 6 +++++ spec/views/tours/index.html.erb_spec.rb | 33 +++++++++++++++++++++++-- 9 files changed, 87 insertions(+), 41 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 2e33eae..7ee720b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,7 +10,11 @@ class User < ActiveRecord::Base has_and_belongs_to_many :roles before_create :create_remember_token - before_save { self.email = email.downcase } + before_save do + pp email + self.email = email.downcase + pp self.email + end validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+.\-]+@[a-z\d.\-]+[\w][^\.]\.[a-z]{2,}/i validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ed8a980..f510a80 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,19 +8,20 @@ <%= csrf_meta_tags %> -
- <% flash.each do |key, value| %> - <%= content_tag(:div, value, class: "alert alert-#{key}") %> - <% end %> +
    - <% if signed_in? %> -
  • <%= link_to "Sign out", destroy_user_session_path, html_options: {class: 'btn' }, method: "delete" %>
  • +
  • <%= link_to "Sign out", destroy_user_session_path, class: 'btn', method: "delete" %>
  • <% else %> -
  • <%= link_to "Sign up", new_user_registration_path, html_options: {class: 'btn'} %>
  • -
  • <%= link_to "Sign in", new_user_session_path, html_options: {class: 'btn' } %>
  • -
+
  • <%= link_to "Sign up", new_user_registration_path, class: 'btn' %>
  • +
  • <%= link_to "Sign in", new_user_session_path, class: 'btn' %>
  • <% end %> + +
    +
    + <% flash.each do |key, value| %> + <%= content_tag(:div, value, class: "alert alert-#{key}") %> + <% end %> <%= yield %> <%= debug(params) if Rails.env.development? %>
    diff --git a/app/views/roles/index.html.erb b/app/views/roles/index.html.erb index 3553149..7520ff2 100644 --- a/app/views/roles/index.html.erb +++ b/app/views/roles/index.html.erb @@ -1,14 +1,12 @@

    Role index

    -
    -<% @roles.each do |role| %> -
    -

    <%= role.name %> - <%= link_to "delete", role, method: :delete, +

    +
    + <%= link_to "Create Role", new_role_path %>
    + <% @roles.each do |role| %> +

    <%= role.name %> + <%= link_to "delete", role, method: :delete, data: {confirm: "Are you sure?"}, - title: role.name %> - -

    + title: role.name %>

    + <% end %>
    -<% end %> -<%= link_to "Create Role", new_role_path %>
    diff --git a/app/views/tours/index.html.erb b/app/views/tours/index.html.erb index 1465b7b..1cf90f1 100644 --- a/app/views/tours/index.html.erb +++ b/app/views/tours/index.html.erb @@ -1,15 +1,20 @@

    Tour index

    -
    -<% @tours.each do |tour| %> -
    -

    <%= tour.name %> <%= link_to "Buchen", new_wish_path %> + +

    +
    <% if user_signed_in? && current_user.has_role?('admin') %> - <%= link_to "delete", tour, method: :delete, - data: {confirm: "Are you sure?"}, - title: tour.name %> + <%= link_to "Create tour", new_tour_path %> + <% end %> + <% @tours.each do |tour| %> +
    +

    <%= tour.name %> <%= link_to "Buchen", new_wish_path(tour: tour.name) %> + <% if user_signed_in? && current_user.has_role?('admin') %> + <%= link_to "delete", tour, method: :delete, + data: {confirm: "Are you sure?"}, + title: tour.name %> + <% end %> +

    +
    <% end %> -

    -<% end %> -<%= link_to "Create tour", new_tour_path %> -
    +
    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 2f2ae2a..5f05202 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ get "users/show" get "users/index" + resources :wishes, only: [:new] resources :roles, only: [:new, :create, :index, :destroy] resources :tours, only: [:new, :create, :destroy, :index, :show] devise_for :users, :controllers => { registrations: 'users/registrations'} diff --git a/spec/factories.rb b/spec/factories.rb index 781bc02..56c5ac5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,7 +1,7 @@ FactoryGirl.define do factory :user do name "Example User" - sequence(:email) { |n| "user-#{n}@example.com" } + email "user-80@example.com" password "foobar" password_confirmation "foobar" @@ -57,7 +57,7 @@ end factory :tour do - name "First Tour" + sequence(:name) { |n| "Tour #{n}" } end factory :booking_status do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 637b961..dca8d84 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -79,7 +79,11 @@ describe "email address with mixed case" do mixed_case_email = "MaRTIn@ExAMPle.CoM" it "should be saved as all lower-case" do - funny_user = FactoryGirl.create(:user, email: mixed_case_email) + funny_user = FactoryGirl.create(:user) + funny_user.email = mixed_case_email + #require 'pry'; binding.pry + funny_user.confirmed_at = Time.now + funny_user.save expect(funny_user.reload.email).to eq mixed_case_email.downcase end end @@ -94,11 +98,9 @@ specify { expect(admin.roles).to eq [Role.find_by(name: "admin")] } end - it "FactoryGirl can create aa Guide at will" do - guide = FactoryGirl.create(:user, :guide) - pp guide.roles - expect(guide.has_role?("guide")).to eq true - + it "FactoryGirl can create a Guide at will" do + guide = FactoryGirl.create(:user, :guide) + expect(guide.has_role?("guide")).to eq true end describe "unsetting a role" do diff --git a/spec/views/roles/index.html.erb_spec.rb b/spec/views/roles/index.html.erb_spec.rb index 6af37c8..d8dfe73 100644 --- a/spec/views/roles/index.html.erb_spec.rb +++ b/spec/views/roles/index.html.erb_spec.rb @@ -26,6 +26,12 @@ expect(page).to have_link("Create Role") end + it "should not have bootstrap columns without bootstrap rows as parents" do + expect(page).not_to have_selector(:xpath, \ + "//div[contains(@class, 'col') and parent::*[not(contains(@class, 'row'))]]") + end + + describe "clicking a delete link" do it "should decrease the role count by 1" do diff --git a/spec/views/tours/index.html.erb_spec.rb b/spec/views/tours/index.html.erb_spec.rb index f7844f3..1134ac6 100644 --- a/spec/views/tours/index.html.erb_spec.rb +++ b/spec/views/tours/index.html.erb_spec.rb @@ -2,11 +2,12 @@ describe "roles/index.html.erb" do before do - FactoryGirl.create(:tour) + 5.times { FactoryGirl.create(:tour) } visit tours_path end it "should have the word tour index" do + pp Capybara::default_driver expect(page).to have_content("Tour index") end @@ -18,6 +19,34 @@ expect(page).to have_link("Buchen") end + it "should not have bootstrap columns without bootstrap rows as parents" do + expect(page).not_to have_selector(:xpath, \ + "//div[contains(@class, 'col') and parent::*[not(contains(@class, 'row'))]]") + end + + describe "all the links" do + # extract the urls from the elements found by "find" + let(:links) do + page.all(:xpath, "//div[@class='well']/p/a").map do |link_object| + link_object[:href] + end + end + + let(:tournames) { Tour.all.map { |tour| CGI.escape(tour.name) } } + + it "should be exactly as many as there are tours" do + expect(links.length).to eq tournames.length + end + + it "each tour should have an associated link" do + result = true + tournames.each do |tourname| + result &= links.any? { |link| link[tourname] } + end + expect(result).to eq true + end + end + describe "signed in as admin" do before do @@ -27,7 +56,7 @@ end it "should have one tour" do - expect(Tour.count).to eq 1 + expect(Tour.count).to eq 5 end it "should have delete links" do