# Week 8 Into the Mines with Gems ---- # Project 1 Reminders It was due yesterday! ---- # Project 2 Team Checkoff * Make sure to submit Team & Plan Form * Sign up for a team checkoff timeslot * Might add more timeslots over the weekend because of midterms ---- # Today: Gem Showcase * Lab-centric lecture today! - We're going to be building **Instagram** with: - User Accounts with Devise - Likes with many-to-many relationships - Fake Likes (heh) with Faker - An admin portal with Rails Admin - File upload with Active Storage ![](https://i.imgur.com/GgQKinn.png) --- # Devise ---- # What Devise does - Set up user authentication associated with models - "Scaffold" a lot of the backend for login + signup - Makes our lives a lot easier ---- # Using Devise - After set up: give models authentication ```ruby rails generate devise MODEL ``` - We can use the following for advanced features of our app ```ruby user_signed_in? --> verify if a user is signed in current_user --> get the current user ``` --- # Getting set up --- # Faker - Lets us create fake data for our apps - Useful for testing and debugging - Powerful customization options ---- # Categories - With Faker we have a ton of categories of info to choose from! - Classic email, name, phone templates - Random "trivia" topics, e.g. Breaking Bad, HIMYM, Colors - ![](https://i.imgur.com/OAWw6wI.png) ---- # Formatting - Formatting: *Faker::[Category]* - Accessing information *Faker::[Category].[attribute] ```ruby Faker::Internet.email #=> "eliza@mann.net" Faker::Internet.username #=> "alexie" ``` ---- # Customizing information - Faker can also take in parameters to customize the output - Useful for forms with validations ```ruby Faker::Internet.email('Nancy') #=> "nancy@terry.biz" Faker::Internet.username(5..8) #=> a username of length min=5 and max=8 ``` ---- # Why is this useful - You can now use this library to create massive amounts of information - This is still in Ruby! - Create loops for tons of fake data ```ruby 10.times do |index| User.create!(first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, bio: Faker::HarryPotter.quote, email: Faker::Internet.email, password: Faker::Internet.password ) end ``` ---- # Go try it yourself! - Jump on the guides and implement Faker for fake User Info! --- # Advanced modeling: a review ---- # A review of relationships * One-to-one * ![](https://support.airtable.com/hc/en-us/article_attachments/206766488/Screen_Shot_2016-04-26_at_3.00.06_PM.png) ---- # A review of relationships * One-to-many * ![](https://support.airtable.com/hc/en-us/article_attachments/206766498/Screen_Shot_2016-04-26_at_3.02.38_PM.png) * A `Work` **`belongs_to`** a `Museum` * A `Museum` **`has_many`** `Work`s ---- # A review of relationships * Many-to-many * ![](https://support.airtable.com/hc/en-us/article_attachments/206808577/Screen_Shot_2016-04-26_at_3.02.52_PM.png) * An `Author` can have many `Book`s and a `Book` can have many `Author`s. ---- # What this looks like * A model that joins two other models together ```ruby class Authorship < ApplicationRecord belongs_to :author belongs_to :book end ``` <!-- .element: class="fragment" --> ---- # What this looks like ```ruby class Book < ApplicationRecord has_many :authorships has_many :authors, through: :authorships end ``` <!-- .element: class="fragment" --> ---- # Now you! * You'll be implementing _likes_ for your Instagram photos --- # Active Storage ---- # Active Storage * Makes file uploads very straightforward * Swappable adapters allow for us to specify different storage services * Local (disk strategy) * Amazon S3 (cloud storage) * Azure (cloud storage) * _... lots of other cloud storage providers_ ---- # Attaching files to models ```ruby class Student has_one_attached :profile_picture end ``` <!-- .element: class="fragment" --> * **_One line!_** ---- # Uploading files as attachments ```ruby <%= form_with model: @student do |form| %> <%= form.file_field :profile_picture, accept: "image/png,image/gif,image/jpeg" %> <% end %> ``` <!-- .element: class="fragment" --> * Puts a type of input inside of your form that says "Choose file" * Uploads a file to the `/students/create` route (or the `/students/update` route) ---- # Accessing files from models ```ruby Student.first.profile_picture.service_url ``` <!-- .element: class="fragment" --> * Returns a public URL for the image uploaded ---- # Now you! * Time to try uploading files yourself! --- # Rails Admin --- # Faraday