Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'jbuilder', '~> 2.0'
gem 'bcrypt'

group :development, :test do
gem 'byebug'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GEM
arel (6.0.3)
autoprefixer-rails (6.3.3.1)
execjs
bcrypt (3.1.11)
better_errors (2.1.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
Expand Down Expand Up @@ -231,6 +232,7 @@ PLATFORMS
ruby

DEPENDENCIES
bcrypt
better_errors
bootstrap-sass
byebug
Expand Down
42 changes: 42 additions & 0 deletions app/assets/stylesheets/sessions.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;

& .form-signin-heading,
& .checkbox {
margin-bottom: 10px;
}

& .checkbox {
font-weight: normal;
}

& .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}

& .form-control:focus {
z-index: 2;
}

& input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}

& input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}

}
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
include SessionsHelper
end
10 changes: 10 additions & 0 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class EventsController < ApplicationController
def index
@events = Event.all
end

def show
@registration = Registration.new
@event = Event.first
end
end
30 changes: 30 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class SessionsController < ApplicationController
def new; end

def create
user = User.find_by(email: email)
if user.authenticate(password)
sign_in(user)
flash[:notice] = 'Welcome back!'
redirect_back_or root_path
else
flash[:error] = 'Wrong email or password!'
render :new
end
end

private

def email
session_params[:email]
end

def password
session_params[:password]
end

def session_params
params.require(:session).permit(:email, :password)
end

end
7 changes: 7 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class UsersController < ApplicationController
before_action :authenticate_user!

def show
@events = current_user.events
end
end
25 changes: 25 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module SessionsHelper
def sign_in(user)
session[:user_id] = user.id
end

def current_user
@current_user ||= User.find_by(id: session[:user_id])
end

def authenticate_user!
if current_user.nil?
store_back_url
flash[:error] = 'You must log in to access this page.'
redirect_to new_session_path
end
end

def store_back_url
session[:back_url] = request.original_url
end

def redirect_back_or(url)
redirect_to session[:back_url] || url
end
end
11 changes: 7 additions & 4 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ def already_registered?(registration)
end
end

validates :phone_number, format: { with: /[0-9]{10}/ }, allow_blank: true
validates :email, presence: true, format: { with: /[\w.-]+@[a-zA-Z]+\.[a-zA-Z]+/ }
validates_with RegistrationEmailValidator
belongs_to :user
belongs_to :event

validates_presence_of :first_name, :last_name
# validates :phone_number, format: { with: /[0-9]{10}/ }, allow_blank: true
# validates :email, presence: true, format: { with: /[\w.-]+@[a-zA-Z]+\.[a-zA-Z]+/ }
# validates_with RegistrationEmailValidator

# validates_presence_of :first_name, :last_name

end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class User < ActiveRecord::Base
has_secure_password

has_many :registrations
has_many :events, through: :registrations
end
10 changes: 6 additions & 4 deletions app/views/layouts/_navigation.html.haml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
-# navigation styled for Bootstrap 3.0
%nav.navbar.navbar-default.navbar-fixed-top
%nav.navbar.navbar-inverse.navbar-fixed-top
.container
.navbar-header
%button.navbar-toggle{"data-target" => ".navbar-collapse", "data-toggle" => "collapse", :type => "button"}
%span.sr-only Toggle navigation
%span.icon-bar
%span.icon-bar
%span.icon-bar
= link_to 'Home', root_path, class: 'navbar-brand'
.collapse.navbar-collapse
= link_to 'RSVP', root_path, class: 'navbar-brand'
#navbar.collapse.navbar-collapse
%ul.nav.navbar-nav
= render 'layouts/navigation_links'
%ul.nav.navbar-nav.navbar-right
%li
= link_to 'Login', new_session_path
1 change: 0 additions & 1 deletion app/views/layouts/_navigation_links.html.erb

This file was deleted.

2 changes: 2 additions & 0 deletions app/views/layouts/_navigation_links.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
= link_to 'Events', events_path, class: 'navbar-brand'

2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
%body
%header
= render 'layouts/navigation'
%main{:role => "main"}
%main.container{:role => "main"}
= render 'layouts/messages'
= yield
17 changes: 17 additions & 0 deletions app/views/sessions/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
= form_for(:session, url: sessions_path, html: { class: 'form-signin' }) do |f|

%h2.form-signin-heading
Please sign in

= f.label :email, class: 'sr-only'
= f.email_field :email, id: 'inputEmail', class: 'form-control', placeholder: 'Email', required: true

= f.label :password, class: 'sr-only'
= f.password_field :password, id: 'inputPassword', class: 'form-control', placeholder: 'Password', required: true

-# .checkbox
-# %label
-# = f.checkbox_field value: 'remember-me'

= f.submit 'Sign in', class: "btn btn-lg btn-primary btn-block"

3 changes: 3 additions & 0 deletions app/views/users/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

- @events.each do |event|
= event.name
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Rails.application.routes.draw do
root 'event_registrations#index'
root 'events#index'

resources :events, only: [:index, :show] do
resources :registrations, only: [:create]
end

resources :event_registrations, only: [:index]
resources :users, only: [:show]
resources :sessions, only: [:new, :create]

end
13 changes: 13 additions & 0 deletions db/migrate/20160715235318_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :first_name
t.string :last_name
t.string :phone_number
t.string :password_digest

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class RemoveEmailFirstNameLastNamePhoneNumberFromRegistrations < ActiveRecord::Migration
def change
remove_column :registrations, :email
remove_column :registrations, :first_name
remove_column :registrations, :last_name
remove_column :registrations, :phone_number

change_table :registrations do |t|
t.references :user
end
end
end
17 changes: 12 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160505235503) do
ActiveRecord::Schema.define(version: 20160716000520) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -26,13 +26,20 @@
end

create_table "registrations", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "event_id"
t.integer "user_id"
end

create_table "users", force: :cascade do |t|
t.string "email"
t.string "phone_number"
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "event_id"
t.string "phone_number"
t.string "password_digest"
t.datetime "created_at"
t.datetime "updated_at"
end

end
6 changes: 6 additions & 0 deletions lib/tasks/migrate_registration_data.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
task migrate_registration_data: :environment do
Registration.all.each do |registration|
user_params = registration.attributes.slice( *%w(email first_name last_name phone_number) )
User.create(user_params)
end
end
32 changes: 32 additions & 0 deletions spec/controller/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe SessionsController, type: :controller do
describe '#new' do
it 'is successfull' do
get :new
expect(response.status).to eq(200)
end
end

describe '#create' do
let!(:user){ FactoryGirl.create(:user, email: '[email protected]', password: 'changeme')}

context 'with valid credentials' do

it 'should log me in' do
post :create, session: { email: '[email protected]', password: 'changeme' }
expect(response.status).to eq(302)
expect(flash[:notice]).to eql('Welcome back!')
end
end

context 'with invalid credentials' do
it "should not log me in" do
post :create, session: { email: '[email protected]', password: 'wrongpass' }
expect(response.status).to eq(200)
expect(flash[:error]).to eql('Wrong email or password!')
end
end
end

end
32 changes: 32 additions & 0 deletions spec/controller/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe UsersController, type: :controller do
describe '#show' do
let(:user) { FactoryGirl.create(:user) }

context 'when the user is logged in' do
let!(:registration) do
FactoryGirl.create(:registration, user_id: user.id)
end

before do
session[:user_id] = user.id
end
it "is succesfull" do
# TODO: change the action used for this test.
# we are refering to something like a profile.
get :show, id: user.id
expect(response.status).to eq(200)
expect(assigns(:events).ids).to eq([registration.event_id])
end
end

context "when the user is not logged_in" do
it 'redirects to the login page' do
get :show, id: user.id
expect(response.status).to eq(302)
expect(flash[:error]).to eq('You must log in to access this page.')
end
end
end
end
8 changes: 4 additions & 4 deletions spec/factories/registrations.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FactoryGirl.define do
factory :registration do
sequence(:email) { |n| "josh#{n}@josh.com" }
phone_number "8097635455"
first_name "Josue"
last_name "Abreu"
# sequence(:email) { |n| "josh#{n}@josh.com" }
# phone_number "8097635455"
# first_name "Josue"
# last_name "Abreu"
event
end
end
Loading