KudosX / feed

Activity Feed from scratch, also using Devise and creating Admin pages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Activity Feed from scratch with Devise

  • New App generated by my template.rb (github.com/KudosX/template.rb):
  • Gemset ruby-2.3.0@rails5.0.0.beta4
  • Ruby 2.3.0
  • Rails 5.0.0.beta4
  • rails db:create to setup postgresql db to get app to work when start rails server

Step 1 - setup devise and create

  • setup devise rails g devise:install
  • execute steps 1,2,3 and 5 from devise messages in terminal
  • create devise model rails g devise User
  • run rails db:migrate

Step 2 - add action to not serve up pages until user authenticated:

  • add to application_controller just before end
  • before_action :authenticate_user!, :except => [:index, :about, :contact, :faq]

Step 3: to view routes

rails routes existing routes

  • localhost:3000/users/sign_in
  • localhost:3000/users/sign_up

Step 4 - update model with first and last name:

  • stop server, run rails g migration AddColumnsToUsers username
  • add :default => nil to :username column in migration
  • then run rails db:migrate

Step 5 - add fields first_name, last_name to devise views

  • add this code to views/devise/registrations/new.html.erb
  • add just below <div class="form-inputs">
<%= f.input :username, required: true, autofocus: true %>
  • add this code to devise/registrations/edit.htm.erb
<div class="form-inputs">
    <%= f.input :username, required: true, autofocus: true %>
  </div>

Step 6 - tell the devise engine to permit newly created column

  • add logic to controllers/application_controller.rb just after before_action :authenticate_user!
before_action :configure_permitted_parameters, if: :devise_controller?
protected 
def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end

Step 7 - add testing spec/features/signing_up_spec.rb

require "rails_helper"
RSpec.feature "Users can sign up" do
  scenario "when providing valid details" do
    visit "/"
    click_link "Sign up"
    fill_in "Email", with: "test@example.com"
    fill_in "user_password", with: "password"
    fill_in "Password confirmation", with: "password"
    click_button "Sign up"
    expect(page).to have_content("You have signed up successfully.")
  end
end
  • run rspec spec/features/signing_up_spec.rb
  • add signup link to views/layouts/_header.html.erb
    <nav>
      <ul class="nav navbar-nav navbar-left">
        <li class="<%= "active" if current_page?("/users/sign_up") %>">
          <%= link_to "Sign up", new_user_registration_path %>
        </li>
      </ul>
    </nav>
  • run rspec to see list of tests to complete

Step 9 - add testing to spec/features/signing_in_spec.rb

require "rails_helper"
RSpec.feature "Users can sign in" do
  let!(:user) { FactoryGirl.create(:user) }
  scenario "with valid credentials" do
    visit "/"
    click_link "Sign in"
    fill_in "Email", with: user.email
    fill_in "Password", with: "password"
    click_button "Sign in"
    expect(page).to have_content "Signed in successfully."
    expect(page).to have_content "Signed in as #{user.email}"
  end
  scenario "unless they are archived" do
    user.archive
    visit "/"
    click_link "Sign in"
    fill_in "Email", with: user.email
    fill_in "Password", with: "password"
    click_button "Sign in"
    expect(page).to have_content "Your account has been archived."
  end
end
  • run rspec spec/features/signing_in_spec.rb, won't show this again, it will be presumptive
  • add this to spec/factories/user_factory.rb
FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "test#{n}@example.com" }
    password "password"

    trait :admin do
      admin true
    end
  end
end
  • change /layouts/_header.html.erb to look like the following
<nav>
      <ul class="nav navbar-nav navbar-left">
      <% unless user_signed_in? %>
          <li class="<%= "active" if current_page?("/users/sign_up") %>">
            <%= link_to "Sign up", new_user_registration_path %>
          </li>
          <li class="<%= "active" if current_page?("/users/sign_in") %>">
            <%= link_to "Sign in", new_user_session_path %>
          </li>
      <% end %>
      <% if user_signed_in? %>
          <div class="navbar-left">
            <p class="navbar-text">
              Signed in as <%= current_user.email %>
            </p>
            <ul class="nav navbar-nav navbar-left">
              <li><%= link_to "Sign out", destroy_user_session_path,
                              method: :delete %>
              </li>
            </ul>
          </div>
      <% end %>
      </ul>
    </nav>
  • add this code to spec/factories/user_factory.rb
FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "test#{n}@example.com" }
    password "password"
  end
end

Step 10 - adding testing to spec/features/signing_out_spec.rb

  • already added sign out code in step 9
require "rails_helper"
RSpec.feature "Signed-in users can sign out" do
  let!(:user) { FactoryGirl.create(:user) }
  before do
    login_as(user)
  end
  scenario do
    visit "/"
    click_link "Sign out"
    expect(page).to have_content "Signed out successfully."
  end
end
  • add the following code to spec/rails_helper.rb
    config.include Warden::Test::Helpers, type: :feature
    config.after(type: :feature) { Warden.test_reset! }

Step 11 - adding some bootstrap style

  • change the code in views/devise/registrations/new.html.erb to add blue Sign up link
<div class="form-actions">
    <%= f.button :submit, "Sign up", class: "btn btn-primary" %>
</div>
  • change the code to views/devise/sessions/new.html.erb to add blue to Log in link
<div class="form-actions">
    <%= f.button :submit, "Log in", class: "btn btn-primary" %>
</div>
  • change the code to views/devise/registrations/edit.html.erb to add blue to Update link
<div class="form-actions">
     <%= f.button :submit, "Update", class: "btn btn-primary" %>
</div>

Step 12 -

About

Activity Feed from scratch, also using Devise and creating Admin pages


Languages

Language:Ruby 80.8%Language:HTML 14.8%Language:CSS 3.6%Language:CoffeeScript 0.7%Language:JavaScript 0.2%