April 20th
Covering:
Git Refresher
GIT EARLY AND OFTEN
To add commit:
git add .(or select individual files to stage)git commit -m "[message here]
To push to remote (i.e. GitHub):
git remote add origin [repo location]git push -u origin master [branch1] [branch1] [branch3]to push master and branches to remote
To show branches:
git branch
To swap between branches:
git checkout [branch you want to go to]
To create new branch then checkout to branch:
git checkout -b [branch name]
To merge a branch with master:
- go to master branch
git merge devise
Creating an Instagram Clone with Pundit
ON MASTER BRANCH
rails new instarails_pundit -Tto create a new rails app without testing suitegem 'rspec-rails', '~> 3.7'Add Rspec to Gemfile for testinggem 'dotenv-rails'Add Dotenv to Gemfile for environment variables- Create
.env.testfile in main folder and add:# use and verify fake email address USER_EMAIL=name@example.com USER_PASSWORD=password123 - Add to .gitignore:
# Environment variables .env.* .env - Run
bundlefor Rspec and required gems rails g rspec:installto install Rspec- Commit
- Checkout to devise branch using
git checkout -b devise
ON DEVISE BRANCH
- Add Devise to gemfile using
gem 'devise' bundlefor Deviserails g devise:installto install Deviserails g devise Userto create User model and migration- Run
rails db:migrate - Commit
- Add to
seeds.rb:User.create!({ email: ENV.fetch('USER_EMAIL'), password: ENV.fetch('USER_PASSWORD') }) { |u| p u.encrypted_password }To verify with testing in
spec > models > user_spec.rbadd:it 'should have matching email' do user = User.new user.email = ENV.fetch('USER_EMAIL') expect(user.email).to eq('name@example.com') endthen run
bundle exec rspecto run all tests in spec file - Run
rails db:seed - In
application_controller.rbatapp > controllersaddbefore_action :authenticate_user! - Commit
- Once devise is installed and complete
git checkout masterto go back to master branchON MASTER BRANCH
- Run
git merge deviseto merge devise with master - Push repo to remote (see Git Refresher for command)
git checkout -b pagesto create a new pages branch and checkoutON PAGES BRANCH
rails g controller Pages home contact- In
routes.rbadd: ``` root ‘pages#home’
get ‘/contact’, to: ‘pages#contact’
- Local website should now be working!
- Checkout back to master branch
### ON MASTER BRANCH
- Merge pages branch with `git merge pages`
- Make a new branch `git checkout -b images`
### ON IMAGES BRANCH
- `rails g scaffold Image user:references image_data:text description:text` to create scaffolding required for Image model
- `rails db:migrate` to run migration
> Creating an `info.md` file and running `history >> info.md` in terminal will append terminal command history to file. > will re-write file with newest data.
- Add `validates :user, presence: true` to the `image.rb` model file
- Add `@image.user = current_user` to the Create action in `images_controller.rb` file
- Add `has_many :images` in `user.rb` model file
- Edit `_form.html.erb` to remove User ID field
- In `seeds.rb` add:
```ruby
user = User.create!({
email: "example@example.com",
password: "password123",
}) { |u| p u.encrypted_password }
Image.create!([
image_data: "http://static.wixstatic.com/media/9059c663ff406ee7a8cc4b8b2048c783.jpg/v1/fill/w_784,h_523,al_c,q_90,usm_0.66_1.00_0.01/9059c663ff406ee7a8cc4b8b2048c783.webp",
description: "A Siberian Husky",
user: user
]) { |i| p i.user }
- Run
rails db:seed - In
show.html.erbadd<%= image_tag @image.image_data %>to show the actual image instead of the image data - Add pundit to gemfile with
gem 'pundit', '~> 1.1' - Run
bundle rails g pundit:installto install Pundit- Create
image_policy.rbfile inapp > policiesfolder then populate with the following policies:class ImagePolicy < ApplicationPolicy def create? user == record.user end def update? create? end end - In
application_controller.rbaddinclude Pundit - In
images_controller.rb, at top before all actions addbefore_action :auth_actions, only: [:update, :create] - At bottom of
images_controller.rbinprivateadd:def auth_actions authorize @image end - To authenticate user before showing Edit link on pages, in
show.html.erbandindex.html.erbreplace Edit links with: ```ruby
<% if policy(@image).update? %> <%= link_to 'Edit', edit_image_path(@image) %> | <% end %>
- In the `images_controller.rb` file at the top add`authorize @images` to `index` action as shown below:
```ruby
# GET /images
# GET /images.json
def index
@images = Image.all
authorize @images
end
- Run
rails g migration AddAdminToUser admin:booleanto generate migration file that will add a new column named admin to the user table with value boolean (true or false) rails db:migrateto run migration file
This is where we finished the day.
Pundit names policies after actions. If action is called elephant, the pundit policy will be called elephant?.