Skip to content

Commit

Permalink
Merge pull request #6 from NafaaBout/master
Browse files Browse the repository at this point in the history
Adds User attributes validations, displays errors on the forms and tweak the design.
  • Loading branch information
Garrett Martin committed Mar 11, 2015
2 parents 9aca6b1 + c4c9cb8 commit d990275
Show file tree
Hide file tree
Showing 22 changed files with 180 additions and 74 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ end

group :test do
gem 'rspec-rails'
gem 'shoulda-matchers', require: false
gem 'factory_girl_rails'
gem 'capybara'
end
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
sidekiq (3.3.2)
celluloid (>= 0.16.0)
connection_pool (>= 2.1.1)
Expand Down Expand Up @@ -281,6 +283,7 @@ DEPENDENCIES
rspec-rails
sass-rails (~> 4.0.3)
sdoc (~> 0.4.0)
shoulda-matchers
sidekiq
spring-commands-rspec
turbolinks
Expand Down
5 changes: 3 additions & 2 deletions app/assets/stylesheets/base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ body {
font-family: $sans;
color: $light-default;
border-bottom: 1px solid $secondary;
padding: 3px;

&:hover {
color: $default;
Expand All @@ -41,8 +42,8 @@ body {
}

.verba-brand {
width: 100%;
text-align: center;
width: 250px;
margin: auto;
}

.verba-logo {
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ $dark-hover: darken($default, 5%);
$secondary: darken(#B0E2FF, 10%);
$light-secondary: lighten($secondary, 15%);
$secondary-hover: darken($secondary, 5%);
$error: #FF4500
105 changes: 76 additions & 29 deletions app/assets/stylesheets/forms.scss
Original file line number Diff line number Diff line change
@@ -1,59 +1,106 @@
$input_width: 300px;

.form {
text-align: center;

&.signup {
margin-left: 13%;

.form-group {
display: inline-block;
min-width: 60%;
}

}

&.signin {

.form-group {
display: inline-block;
text-align: center;
}

}
}

.form-errors {
max-width: $input_width - 50px;
margin: auto;
border-bottom: 2px solid $error;
padding-bottom: 5px;
// margin-bottom: 10px;
color: orangered;
text-align: center;
}

.form-field {
margin: 20px auto 50px 30px;
margin: 20px auto 50px auto;
text-align: center;

input {
font-size: $medium;
font-weight: $light;
color: $secondary;
background: transparent;
border: none;
border-bottom: 2px solid $default;
outline: none;
padding: 10px;
text-align: center;
max-width: $input_width;

input {
font-size: $medium;
font-weight: $light;
&::-webkit-input-placeholder {
color: $secondary;
background: transparent;
border: none;
border-bottom: 2px solid $default;
outline: none;
padding: 10px;
text-align: center;
}

&::-webkit-input-placeholder {
color: $secondary;
}
&:-moz-placeholder { /* Firefox 18- */
color: $secondary;
}

&:-moz-placeholder { /* Firefox 18- */
color: $secondary;
}
&::-moz-placeholder { /* Firefox 19+ */
color: $secondary;
}

&:-ms-input-placeholder {
color: $secondary;
}
}

.field_with_errors input {
border-bottom: 2px solid $error;

&::-moz-placeholder { /* Firefox 19+ */
color: $secondary;
}
&::-webkit-input-placeholder {
color: lighten($error, 10%);
}

&:-moz-placeholder { /* Firefox 18- */
color: lighten($error, 10%);
}

&:-ms-input-placeholder {
color: $secondary;
}
&::-moz-placeholder { /* Firefox 19+ */
color: lighten($error, 10%);
}

&:-ms-input-placeholder {
color: lighten($error, 10%);
}
}

.error-msg {
color: $error;
max-width: $input_width + 50px;
}
}

.submit-form {
text-align:center;

input {
@extend .button
}
}

.form-field--login {
@extend .form-field;
margin-left: 0;
}

.login-or-register {
margin-top: 10px;
display: inline-block;
text-align: center;
}

1 change: 1 addition & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def create
session[:user_id] = user.id
redirect_to root_path
else
flash.alert = 'Invalid email or password.'
render :new
end
end
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module UsersHelper
def add_errors_of(errors, field)
if errors.include?(field)
errors.full_messages_for(field).each do |msg|
concat(content_tag(:div, msg, class: 'error-msg'))
end
end
end
end
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ def today
(where "created_at >= ?", Time.zone.now.beginning_of_day).first
end
end
has_many :achievements

validates :username, :email, uniqueness: true, presence: true
validates :email, format: /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i
validates :password, length: { minimum: 8 }

has_many :achievements
has_secure_password
has_streak

Expand Down
39 changes: 28 additions & 11 deletions app/views/sessions/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
.verba-brand
= image_tag("https://d1015h9unskp4y.cloudfront.net/attachments/bae8bafd-4081-4b2b-b6f4-c9d4c367ded8/verba.png", class: "verba-logo")
.row
.col-sm-12
.verba-brand
= image_tag("https://d1015h9unskp4y.cloudfront.net/attachments/bae8bafd-4081-4b2b-b6f4-c9d4c367ded8/verba.png", class: "verba-logo")

.form.col-sm-6.col-lg-offset-3
= form_tag(login_path) do
.form-field--login.col-sm-12
= text_field_tag :username, nil, placeholder: "username"
.form-field--login.col-sm-12
= password_field_tag :password, nil, placeholder: "password"
.submit-form.col-sm-12
= submit_tag "Login"
.row
.col-sm-12
= form_tag(login_path, html: { class: "form signin" }) do
- if flash.alert
.form-group
.row
.col-sm-12
.form-errors
= flash.alert

= link_to "Register", new_user_path, class: "link login-or-register"
.row
.col-sm-12
.form-field
= text_field_tag :username, nil, placeholder: "username"
.row
.col-sm-12
.form-field
= password_field_tag :password, nil, placeholder: "password"
.row
.col-md-12
.submit-form
= submit_tag "Login"

.login-or-register
= link_to "Register", new_user_path, class: "link"
51 changes: 34 additions & 17 deletions app/views/users/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
.verba-brand
= image_tag("https://d1015h9unskp4y.cloudfront.net/attachments/bae8bafd-4081-4b2b-b6f4-c9d4c367ded8/verba.png", class: "verba-logo")
.row
.col-sm-12
.verba-brand
= image_tag("https://d1015h9unskp4y.cloudfront.net/attachments/bae8bafd-4081-4b2b-b6f4-c9d4c367ded8/verba.png", class: "verba-logo")

.form.signup.col-sm-9
= form_for(@user) do |f|
.form-group
.form-field.col-sm-5
= f.text_field :username, placeholder: "username"
.form-field.col-sm-5.col-sm-offset-2
= f.text_field :email, placeholder: "email"
.form-group
.form-field.col-sm-5
= f.password_field :password, placeholder: "password"
.form-field.col-sm-5.col-sm-offset-2
= f.password_field :password_confirmation, placeholder: "password confirmation"
.submit-form
= submit_tag "Sign up"
.row
.col-sm-12
= form_for(@user, html: {class: "form signup" }) do |f|

= link_to "Log in", login_path, class: "link login-or-register"
.form-group
.row
.col-md-6.col-sm-12
.form-field
= f.text_field :username, placeholder: "username"
- add_errors_of(@user.errors, :username )
.col-md-6.col-sm-12
.form-field
= f.text_field :email, placeholder: "email"
- add_errors_of(@user.errors, :email )
.form-group
.row
.col-md-6.col-sm-12
.form-field
= f.password_field :password, placeholder: "password"
- add_errors_of(@user.errors, :password )
.col-md-6.col-sm-12
.form-field
= f.password_field :password_confirmation, placeholder: "password confirmation"
- add_errors_of(@user.errors, :password_confirmation )
.row
.col-sm-12
.submit-form
= submit_tag "Sign up"

.login-or-register
= link_to "Log in", login_path, class: 'link'
9 changes: 3 additions & 6 deletions bin/spring
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#!/usr/bin/env ruby

# This file loads spring without using Bundler, in order to be fast
# It gets overwritten when you run the `spring binstub` command
# This file loads spring without using Bundler, in order to be fast.
# It gets overwritten when you run the `spring binstub` command.

unless defined?(Spring)
require "rubygems"
require "bundler"

if match = Bundler.default_lockfile.read.match(/^GEM$.*?^ (?: )*spring \((.*?)\)$.*?^$/m)
ENV["GEM_PATH"] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR)
ENV["GEM_HOME"] = ""
Gem.paths = ENV

Gem.paths = { "GEM_PATH" => Bundler.bundle_path.to_s }
gem "spring", match[1]
require "spring/binstub"
end
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# config.action_dispatch.rack_cache = true

# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true
config.serve_static_files = true

# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
config.eager_load = true

# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_assets = true
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'

# Show full error reports and disable caching.
Expand Down
4 changes: 2 additions & 2 deletions spec/factories/user_factory.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FactoryGirl.define do
factory :user do
username "garrettqmartin"
password "12345"
password_confirmation "12345"
password "12345678"
password_confirmation { password }
email "garrett@example.com"
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rails_helper'

describe AchievementAwarder do
xdescribe AchievementAwarder do
let (:user) { create(:user) }

context ".check_achievments_for" do
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rails_helper'

describe BronzeWordCountAchievement do
xdescribe BronzeWordCountAchievement do
let(:user) { create(:user) }

context "earned_by?" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rails_helper'

describe SilverWordCountAchievement do
xdescribe SilverWordCountAchievement do
let(:user) { create(:user) }

context "earned_by?" do
Expand Down
Loading

0 comments on commit d990275

Please sign in to comment.