Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teaching PR#1: Implement devise and has_many relationship #2

Open
wants to merge 2 commits into
base: save_commit_0c4713e_for_PR
Choose a base branch
from
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 @@ -39,6 +39,7 @@ gem 'spring', group: :development
# gem 'debugger', group: [:development, :test]

gem 'bootstrap-sass', '~> 3.3.6'
gem 'devise', '~> 3.5.6'

group :development, :test do
gem 'pry-rails'
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GEM
arel (5.0.1.20140414130214)
autoprefixer-rails (6.3.5)
execjs
bcrypt (3.1.11)
bootstrap-sass (3.3.6)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
Expand All @@ -43,6 +44,13 @@ GEM
execjs
coffee-script-source (1.10.0)
concurrent-ruby (1.0.1)
devise (3.5.6)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
responders
thread_safe (~> 0.1)
warden (~> 1.2.3)
erubis (2.7.0)
execjs (2.6.0)
i18n (0.7.0)
Expand All @@ -61,6 +69,7 @@ GEM
mime-types-data (3.2016.0221)
minitest (5.8.4)
multi_json (1.11.2)
orm_adapter (0.5.0)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
Expand Down Expand Up @@ -88,6 +97,8 @@ GEM
rake (11.1.2)
rdoc (4.2.2)
json (~> 1.4)
responders (1.1.2)
railties (>= 3.2, < 4.2)
sass (3.4.22)
sass-rails (5.0.4)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -117,13 +128,16 @@ GEM
thread_safe (~> 0.1)
uglifier (3.0.0)
execjs (>= 0.3.0, < 3)
warden (1.2.6)
rack (>= 1.0)

PLATFORMS
ruby

DEPENDENCIES
bootstrap-sass (~> 3.3.6)
coffee-rails (~> 4.0.0)
devise (~> 3.5.6)
jbuilder (~> 2.0)
jquery-rails
pry-rails
Expand Down
11 changes: 10 additions & 1 deletion app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,14 @@ body {
}

.bold {
font-style: bold;
font-weight: bold;
}

.italic {
font-style: italic;
}

.header {
font-size: 20px;
font-weight: bold;
}
35 changes: 0 additions & 35 deletions app/assets/stylesheets/post.css.scss

This file was deleted.

8 changes: 8 additions & 0 deletions app/assets/stylesheets/post/post_common.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.post-box {
border-style: solid;
border-width: 1px;
box-shadow: 5px 5px;
width: 400px;
padding: 10px;
margin: 10px;
}
19 changes: 19 additions & 0 deletions app/assets/stylesheets/post/post_index.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Place all the styles related to the Post controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

.post-details {
background-color: #d3d3d3;
padding-right: 5px;
padding-left: 5px;
border-radius: 10px;
}

.post-details div {
display: inline;
}

.post-time {
float: right;
}

12 changes: 12 additions & 0 deletions app/assets/stylesheets/post/post_new.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.new-post-form textarea {
width: 100%;
}

.new-post-form .form-group {
overflow: auto;
}

.new-post-form .form-group .btn {
margin-top: 5px;
float: right;
}
11 changes: 11 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ 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


# Whitelist name parameter for signup
# http://www.sitepoint.com/devise-authentication-in-depth/
before_action :configure_permitted_params, if: :devise_controller?

protected
def configure_permitted_params
devise_parameter_sanitizer.for(:sign_up) << :name
devise_parameter_sanitizer.for(:account_update) << :name
end
end
7 changes: 4 additions & 3 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
class PostsController < ApplicationController
before_action :authenticate_user!, except: [ :index ]

# Return all posts
def index
@posts = Post.order(created_at: :desc)
end

def new
@post = Post.new
@post = current_user.posts.build
end

def create
Post.new(post_params).save!
current_user.posts.create(post_params)

redirect_to root_path
end

private
def post_params
params.require(:post).permit(:creator, :body)
params.require(:post).permit(:body)
end
end
4 changes: 4 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ class Post < ActiveRecord::Base
validates :creator, presence: true
validates :body, presence: true

# I have to specify the foreign key because I wanted my posts
# to refer to their user as 'creator'
belongs_to :creator, foreign_key: 'creator_id', class_name: 'User'

def friendly_time
created_at.in_time_zone('Eastern Time (US & Canada)').strftime('%F %H:%M')
end
Expand Down
13 changes: 13 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

validates :name, presence: true, allow_blank: false

# I have to specify the foreign key because I wanted my posts
# to refer to their user as 'creator'
# dependent: :destory means that if a user is deleted, so are their posts
has_many :posts, foreign_key: 'creator_id', class_name: 'Post', dependent: :destroy
end
16 changes: 16 additions & 0 deletions app/views/devise/confirmations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Resend confirmation instructions</h2>

<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
</div>

<div class="actions">
<%= f.submit "Resend confirmation instructions" %>
</div>
<% end %>

<%= render "devise/shared/links" %>
5 changes: 5 additions & 0 deletions app/views/devise/mailer/confirmation_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
3 changes: 3 additions & 0 deletions app/views/devise/mailer/password_change.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>Hello <%= @resource.email %>!</p>

<p>We're contacting you to notify you that your password has been changed.</p>
8 changes: 8 additions & 0 deletions app/views/devise/mailer/reset_password_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>Hello <%= @resource.email %>!</p>

<p>Someone has requested a link to change your password. You can do this through the link below.</p>

<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
7 changes: 7 additions & 0 deletions app/views/devise/mailer/unlock_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p>Hello <%= @resource.email %>!</p>

<p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p>

<p>Click the link below to unlock your account:</p>

<p><%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %></p>
25 changes: 25 additions & 0 deletions app/views/devise/passwords/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>Change your password</h2>

<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>

<div class="field">
<%= f.label :password, "New password" %><br />
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em><br />
<% end %>
<%= f.password_field :password, autofocus: true, autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="actions">
<%= f.submit "Change my password" %>
</div>
<% end %>

<%= render "devise/shared/links" %>
16 changes: 16 additions & 0 deletions app/views/devise/passwords/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Forgot your password?</h2>

<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>

<div class="actions">
<%= f.submit "Send me reset password instructions" %>
</div>
<% end %>

<%= render "devise/shared/links" %>
39 changes: 39 additions & 0 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>

<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>

<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>

<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>

<h3>Cancel my account</h3>

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>

<%= link_to "Back", :back %>
35 changes: 35 additions & 0 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
</div>


<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>

<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>

<%= render "devise/shared/links" %>
Loading